juham-automation 0.0.17__py3-none-any.whl → 0.0.26__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- juham_automation/__init__.py +42 -38
- juham_automation/automation/__init__.py +23 -21
- juham_automation/automation/energybalancer.py +158 -0
- juham_automation/automation/energycostcalculator.py +267 -266
- juham_automation/automation/{hotwateroptimizer.py → heatingoptimizer.py} +539 -581
- juham_automation/automation/powermeter_simulator.py +139 -139
- juham_automation/automation/spothintafi.py +140 -140
- juham_automation/automation/watercirculator.py +159 -159
- juham_automation/japp.py +53 -49
- juham_automation/ts/__init__.py +27 -25
- juham_automation/ts/electricityprice_ts.py +51 -51
- juham_automation/ts/energybalancer_ts.py +47 -0
- juham_automation/ts/energycostcalculator_ts.py +43 -43
- juham_automation/ts/forecast_ts.py +97 -97
- juham_automation/ts/log_ts.py +57 -57
- juham_automation/ts/power_ts.py +49 -49
- juham_automation/ts/powermeter_ts.py +67 -70
- juham_automation/ts/powerplan_ts.py +45 -45
- juham_automation-0.0.26.dist-info/METADATA +152 -0
- juham_automation-0.0.26.dist-info/RECORD +25 -0
- {juham_automation-0.0.17.dist-info → juham_automation-0.0.26.dist-info}/entry_points.txt +3 -1
- {juham_automation-0.0.17.dist-info → juham_automation-0.0.26.dist-info}/licenses/LICENSE.rst +25 -25
- juham_automation-0.0.17.dist-info/METADATA +0 -106
- juham_automation-0.0.17.dist-info/RECORD +0 -23
- {juham_automation-0.0.17.dist-info → juham_automation-0.0.26.dist-info}/WHEEL +0 -0
- {juham_automation-0.0.17.dist-info → juham_automation-0.0.26.dist-info}/top_level.txt +0 -0
@@ -1,159 +1,159 @@
|
|
1
|
-
from typing import Any
|
2
|
-
from typing_extensions import override
|
3
|
-
import json
|
4
|
-
|
5
|
-
from masterpiece.mqtt import MqttMsg
|
6
|
-
from juham_core import Juham
|
7
|
-
from juham_core.timeutils import timestamp
|
8
|
-
|
9
|
-
|
10
|
-
class WaterCirculator(Juham):
|
11
|
-
"""Hot Water Circulation Automation
|
12
|
-
|
13
|
-
This system monitors motion sensor data to detect home occupancy.
|
14
|
-
|
15
|
-
- **When motion is detected**: The water circulator pump is activated, ensuring hot water is
|
16
|
-
instantly available when the tap is turned on.
|
17
|
-
- **When no motion is detected for a specified period (in seconds)**: The pump automatically
|
18
|
-
switches off to conserve energy.
|
19
|
-
|
20
|
-
Future improvement idea
|
21
|
-
------------------------
|
22
|
-
|
23
|
-
In cold countries, such as Finland, energy conservation during the winter season may not be a priority.
|
24
|
-
In this case, an additional temperature sensor measuring the outside temperature could be used to determine whether
|
25
|
-
the circulator should be switched off at all. The circulating water could potentially act as an additional heating radiator.
|
26
|
-
|
27
|
-
Points to consider
|
28
|
-
------------------
|
29
|
-
|
30
|
-
- Switching the pump on and off may affect its lifetime.
|
31
|
-
- Keeping the pump running with hot water could impact the lifespan of the pipes, potentially causing
|
32
|
-
corrosion due to constant hot water flow.
|
33
|
-
|
34
|
-
"""
|
35
|
-
|
36
|
-
uptime = 60 * 60 # one hour
|
37
|
-
min_temperature = 37
|
38
|
-
|
39
|
-
def __init__(self, name: str, temperature_sensor: str) -> None:
|
40
|
-
super().__init__(name)
|
41
|
-
|
42
|
-
# input topics
|
43
|
-
self.motion_topic = self.make_topic_name("motion") # motion detection
|
44
|
-
self.temperature_topic = self.make_topic_name(temperature_sensor)
|
45
|
-
|
46
|
-
# relay to be controlled
|
47
|
-
self.topic_power = self.make_topic_name("power")
|
48
|
-
|
49
|
-
# for the pump controlling logic
|
50
|
-
self.current_motion: bool = False
|
51
|
-
self.relay_started_ts: float = 0
|
52
|
-
self.water_temperature: float = 0
|
53
|
-
self.water_temperature_updated: float = 0
|
54
|
-
self.initialized = False
|
55
|
-
|
56
|
-
@override
|
57
|
-
def on_connect(self, client: object, userdata: Any, flags: int, rc: int) -> None:
|
58
|
-
super().on_connect(client, userdata, flags, rc)
|
59
|
-
if rc == 0:
|
60
|
-
self.subscribe(self.motion_topic)
|
61
|
-
self.subscribe(self.temperature_topic)
|
62
|
-
# reset the relay to make sure the initial state matches the state of us
|
63
|
-
self.publish_relay_state(0)
|
64
|
-
|
65
|
-
@override
|
66
|
-
def on_message(self, client: object, userdata: Any, msg: MqttMsg) -> None:
|
67
|
-
if msg.topic == self.temperature_topic:
|
68
|
-
m = json.loads(msg.payload.decode())
|
69
|
-
self.on_temperature_sensor(m, timestamp())
|
70
|
-
elif msg.topic == self.motion_topic:
|
71
|
-
m = json.loads(msg.payload.decode())
|
72
|
-
self.on_motion_sensor(m, timestamp())
|
73
|
-
else:
|
74
|
-
super().on_message(client, userdata, msg)
|
75
|
-
|
76
|
-
def on_temperature_sensor(self, m: dict[str, Any], ts_utc_now: float) -> None:
|
77
|
-
"""Handle message from the hot water pipe temperature sensor.
|
78
|
-
Records the temperature and updates the water_temperature_updated attribute.
|
79
|
-
|
80
|
-
Args:
|
81
|
-
m (dict): temperature reading from the hot water blump sensor
|
82
|
-
ts_utc_now (float): _current utc time
|
83
|
-
"""
|
84
|
-
|
85
|
-
self.water_temperature = m["temperature"]
|
86
|
-
self.water_temperature_updated = ts_utc_now
|
87
|
-
# self.info(
|
88
|
-
# f"Temperature of circulating water updated to {self.water_temperature} C"
|
89
|
-
# )
|
90
|
-
|
91
|
-
def on_motion_sensor(self, m: dict[str, dict[str, Any]], ts_utc_now: float) -> None:
|
92
|
-
"""Control the water cirulator bump.
|
93
|
-
|
94
|
-
Given message from the motion sensor consider switching the
|
95
|
-
circulator bump on.
|
96
|
-
|
97
|
-
Args:
|
98
|
-
msg (dict): directionary holding motion sensor data
|
99
|
-
ts_utc_now (float): current time stamp
|
100
|
-
"""
|
101
|
-
sensor = m["sensor"]
|
102
|
-
vibration: bool = bool(m["vibration"])
|
103
|
-
motion: bool = bool(m["motion"])
|
104
|
-
|
105
|
-
if motion or vibration:
|
106
|
-
# self.debug(f"Life form detected in {sensor}")
|
107
|
-
# honey I'm home
|
108
|
-
if not self.current_motion:
|
109
|
-
if self.water_temperature > self.min_temperature:
|
110
|
-
self.publish_relay_state(0)
|
111
|
-
# self.debug(
|
112
|
-
# f"Circulator: motion detected but water warm already {self.water_temperature} > {self.min_temperature} C"
|
113
|
-
# )
|
114
|
-
else:
|
115
|
-
self.current_motion = True
|
116
|
-
self.relay_started_ts = ts_utc_now
|
117
|
-
self.publish_relay_state(1)
|
118
|
-
self.initialized = True
|
119
|
-
self.info(
|
120
|
-
f"Circulator pump started, will run for {int(self.uptime / 60)} minutes "
|
121
|
-
)
|
122
|
-
else:
|
123
|
-
self.publish_relay_state(1)
|
124
|
-
self.relay_started_ts = ts_utc_now
|
125
|
-
# self.debug(
|
126
|
-
# f"Circulator pump has been running for {int(ts_utc_now - self.relay_started_ts)/60} minutes",
|
127
|
-
# " ",
|
128
|
-
# )
|
129
|
-
else:
|
130
|
-
if self.current_motion or not self.initialized:
|
131
|
-
elapsed: float = ts_utc_now - self.relay_started_ts
|
132
|
-
if elapsed > self.uptime:
|
133
|
-
self.publish_relay_state(0)
|
134
|
-
self.info(
|
135
|
-
f"Circulator pump stopped, no motion in {int(elapsed/60)} minutes detected",
|
136
|
-
"",
|
137
|
-
)
|
138
|
-
self.current_motion = False
|
139
|
-
self.initialized = True
|
140
|
-
else:
|
141
|
-
self.publish_relay_state(1)
|
142
|
-
# self.debug(
|
143
|
-
# f"Circulator bump stop countdown {int(self.uptime - (ts_utc_now - self.relay_started_ts ))/60} min"
|
144
|
-
# )
|
145
|
-
else:
|
146
|
-
self.publish_relay_state(0)
|
147
|
-
# self.debug(
|
148
|
-
# f"Circulator bump off already, temperature {self.water_temperature} C",
|
149
|
-
# "",
|
150
|
-
# )
|
151
|
-
|
152
|
-
def publish_relay_state(self, state: int) -> None:
|
153
|
-
"""Publish power status.
|
154
|
-
|
155
|
-
Args:
|
156
|
-
state (int): 1 for on, 0 for off, as defined by Juham 'power' topic
|
157
|
-
"""
|
158
|
-
heat = {"Unit": self.name, "Timestamp": timestamp(), "State": state}
|
159
|
-
self.publish(self.topic_power, json.dumps(heat), 1, False)
|
1
|
+
from typing import Any
|
2
|
+
from typing_extensions import override
|
3
|
+
import json
|
4
|
+
|
5
|
+
from masterpiece.mqtt import MqttMsg
|
6
|
+
from juham_core import Juham
|
7
|
+
from juham_core.timeutils import timestamp
|
8
|
+
|
9
|
+
|
10
|
+
class WaterCirculator(Juham):
|
11
|
+
"""Hot Water Circulation Automation
|
12
|
+
|
13
|
+
This system monitors motion sensor data to detect home occupancy.
|
14
|
+
|
15
|
+
- **When motion is detected**: The water circulator pump is activated, ensuring hot water is
|
16
|
+
instantly available when the tap is turned on.
|
17
|
+
- **When no motion is detected for a specified period (in seconds)**: The pump automatically
|
18
|
+
switches off to conserve energy.
|
19
|
+
|
20
|
+
Future improvement idea
|
21
|
+
------------------------
|
22
|
+
|
23
|
+
In cold countries, such as Finland, energy conservation during the winter season may not be a priority.
|
24
|
+
In this case, an additional temperature sensor measuring the outside temperature could be used to determine whether
|
25
|
+
the circulator should be switched off at all. The circulating water could potentially act as an additional heating radiator.
|
26
|
+
|
27
|
+
Points to consider
|
28
|
+
------------------
|
29
|
+
|
30
|
+
- Switching the pump on and off may affect its lifetime.
|
31
|
+
- Keeping the pump running with hot water could impact the lifespan of the pipes, potentially causing
|
32
|
+
corrosion due to constant hot water flow.
|
33
|
+
|
34
|
+
"""
|
35
|
+
|
36
|
+
uptime = 60 * 60 # one hour
|
37
|
+
min_temperature = 37
|
38
|
+
|
39
|
+
def __init__(self, name: str, temperature_sensor: str) -> None:
|
40
|
+
super().__init__(name)
|
41
|
+
|
42
|
+
# input topics
|
43
|
+
self.motion_topic = self.make_topic_name("motion") # motion detection
|
44
|
+
self.temperature_topic = self.make_topic_name(temperature_sensor)
|
45
|
+
|
46
|
+
# relay to be controlled
|
47
|
+
self.topic_power = self.make_topic_name("power")
|
48
|
+
|
49
|
+
# for the pump controlling logic
|
50
|
+
self.current_motion: bool = False
|
51
|
+
self.relay_started_ts: float = 0
|
52
|
+
self.water_temperature: float = 0
|
53
|
+
self.water_temperature_updated: float = 0
|
54
|
+
self.initialized = False
|
55
|
+
|
56
|
+
@override
|
57
|
+
def on_connect(self, client: object, userdata: Any, flags: int, rc: int) -> None:
|
58
|
+
super().on_connect(client, userdata, flags, rc)
|
59
|
+
if rc == 0:
|
60
|
+
self.subscribe(self.motion_topic)
|
61
|
+
self.subscribe(self.temperature_topic)
|
62
|
+
# reset the relay to make sure the initial state matches the state of us
|
63
|
+
self.publish_relay_state(0)
|
64
|
+
|
65
|
+
@override
|
66
|
+
def on_message(self, client: object, userdata: Any, msg: MqttMsg) -> None:
|
67
|
+
if msg.topic == self.temperature_topic:
|
68
|
+
m = json.loads(msg.payload.decode())
|
69
|
+
self.on_temperature_sensor(m, timestamp())
|
70
|
+
elif msg.topic == self.motion_topic:
|
71
|
+
m = json.loads(msg.payload.decode())
|
72
|
+
self.on_motion_sensor(m, timestamp())
|
73
|
+
else:
|
74
|
+
super().on_message(client, userdata, msg)
|
75
|
+
|
76
|
+
def on_temperature_sensor(self, m: dict[str, Any], ts_utc_now: float) -> None:
|
77
|
+
"""Handle message from the hot water pipe temperature sensor.
|
78
|
+
Records the temperature and updates the water_temperature_updated attribute.
|
79
|
+
|
80
|
+
Args:
|
81
|
+
m (dict): temperature reading from the hot water blump sensor
|
82
|
+
ts_utc_now (float): _current utc time
|
83
|
+
"""
|
84
|
+
|
85
|
+
self.water_temperature = m["temperature"]
|
86
|
+
self.water_temperature_updated = ts_utc_now
|
87
|
+
# self.info(
|
88
|
+
# f"Temperature of circulating water updated to {self.water_temperature} C"
|
89
|
+
# )
|
90
|
+
|
91
|
+
def on_motion_sensor(self, m: dict[str, dict[str, Any]], ts_utc_now: float) -> None:
|
92
|
+
"""Control the water cirulator bump.
|
93
|
+
|
94
|
+
Given message from the motion sensor consider switching the
|
95
|
+
circulator bump on.
|
96
|
+
|
97
|
+
Args:
|
98
|
+
msg (dict): directionary holding motion sensor data
|
99
|
+
ts_utc_now (float): current time stamp
|
100
|
+
"""
|
101
|
+
sensor = m["sensor"]
|
102
|
+
vibration: bool = bool(m["vibration"])
|
103
|
+
motion: bool = bool(m["motion"])
|
104
|
+
|
105
|
+
if motion or vibration:
|
106
|
+
# self.debug(f"Life form detected in {sensor}")
|
107
|
+
# honey I'm home
|
108
|
+
if not self.current_motion:
|
109
|
+
if self.water_temperature > self.min_temperature:
|
110
|
+
self.publish_relay_state(0)
|
111
|
+
# self.debug(
|
112
|
+
# f"Circulator: motion detected but water warm already {self.water_temperature} > {self.min_temperature} C"
|
113
|
+
# )
|
114
|
+
else:
|
115
|
+
self.current_motion = True
|
116
|
+
self.relay_started_ts = ts_utc_now
|
117
|
+
self.publish_relay_state(1)
|
118
|
+
self.initialized = True
|
119
|
+
self.info(
|
120
|
+
f"Circulator pump started, will run for {int(self.uptime / 60)} minutes "
|
121
|
+
)
|
122
|
+
else:
|
123
|
+
self.publish_relay_state(1)
|
124
|
+
self.relay_started_ts = ts_utc_now
|
125
|
+
# self.debug(
|
126
|
+
# f"Circulator pump has been running for {int(ts_utc_now - self.relay_started_ts)/60} minutes",
|
127
|
+
# " ",
|
128
|
+
# )
|
129
|
+
else:
|
130
|
+
if self.current_motion or not self.initialized:
|
131
|
+
elapsed: float = ts_utc_now - self.relay_started_ts
|
132
|
+
if elapsed > self.uptime:
|
133
|
+
self.publish_relay_state(0)
|
134
|
+
self.info(
|
135
|
+
f"Circulator pump stopped, no motion in {int(elapsed/60)} minutes detected",
|
136
|
+
"",
|
137
|
+
)
|
138
|
+
self.current_motion = False
|
139
|
+
self.initialized = True
|
140
|
+
else:
|
141
|
+
self.publish_relay_state(1)
|
142
|
+
# self.debug(
|
143
|
+
# f"Circulator bump stop countdown {int(self.uptime - (ts_utc_now - self.relay_started_ts ))/60} min"
|
144
|
+
# )
|
145
|
+
else:
|
146
|
+
self.publish_relay_state(0)
|
147
|
+
# self.debug(
|
148
|
+
# f"Circulator bump off already, temperature {self.water_temperature} C",
|
149
|
+
# "",
|
150
|
+
# )
|
151
|
+
|
152
|
+
def publish_relay_state(self, state: int) -> None:
|
153
|
+
"""Publish power status.
|
154
|
+
|
155
|
+
Args:
|
156
|
+
state (int): 1 for on, 0 for off, as defined by Juham 'power' topic
|
157
|
+
"""
|
158
|
+
heat = {"Unit": self.name, "Timestamp": timestamp(), "State": state}
|
159
|
+
self.publish(self.topic_power, json.dumps(heat), 1, False)
|
juham_automation/japp.py
CHANGED
@@ -1,49 +1,53 @@
|
|
1
|
-
from
|
2
|
-
from
|
3
|
-
|
4
|
-
|
5
|
-
from .ts import
|
6
|
-
from .ts import
|
7
|
-
from .ts import
|
8
|
-
from .ts import
|
9
|
-
from .ts import
|
10
|
-
from .ts import
|
11
|
-
from .
|
12
|
-
from .
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
"""
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
self.add(
|
39
|
-
self.add(
|
40
|
-
self.add(
|
41
|
-
self.add(
|
42
|
-
self.add(
|
43
|
-
self.add(
|
44
|
-
self.add(
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
1
|
+
from juham_automation.automation.energybalancer import EnergyBalancer
|
2
|
+
from masterpiece import Application
|
3
|
+
from juham_core import Juham
|
4
|
+
|
5
|
+
from .ts import ForecastTs
|
6
|
+
from .ts import PowerTs
|
7
|
+
from .ts import PowerPlanTs
|
8
|
+
from .ts import PowerMeterTs
|
9
|
+
from .ts import EnergyBalancerTs
|
10
|
+
from .ts import LogTs
|
11
|
+
from .ts import EnergyCostCalculatorTs
|
12
|
+
from .ts import ElectricityPriceTs
|
13
|
+
from .automation import SpotHintaFi
|
14
|
+
from .automation import EnergyCostCalculator
|
15
|
+
|
16
|
+
|
17
|
+
class JApp(Application):
|
18
|
+
"""Juham home automation application base class. Registers new plugin
|
19
|
+
group 'juham' on which general purpose Juham plugins can be written on.
|
20
|
+
"""
|
21
|
+
|
22
|
+
def __init__(self, name: str) -> None:
|
23
|
+
"""Creates home automation application with the given name.
|
24
|
+
If --enable_plugins is False create hard coded configuration
|
25
|
+
by calling instantiate_classes() method.
|
26
|
+
|
27
|
+
Args:
|
28
|
+
name (str): name for the application
|
29
|
+
"""
|
30
|
+
super().__init__(name, Juham(name))
|
31
|
+
|
32
|
+
def instantiate_classes(self) -> None:
|
33
|
+
"""Instantiate automation classes .
|
34
|
+
|
35
|
+
Returns:
|
36
|
+
None
|
37
|
+
"""
|
38
|
+
self.add(ForecastTs())
|
39
|
+
self.add(PowerTs())
|
40
|
+
self.add(PowerPlanTs())
|
41
|
+
self.add(PowerMeterTs())
|
42
|
+
self.add(LogTs())
|
43
|
+
self.add(SpotHintaFi())
|
44
|
+
self.add(EnergyCostCalculator())
|
45
|
+
self.add(EnergyCostCalculatorTs())
|
46
|
+
self.add(ElectricityPriceTs())
|
47
|
+
self.add(EnergyBalancer())
|
48
|
+
self.add(EnergyBalancerTs())
|
49
|
+
|
50
|
+
@classmethod
|
51
|
+
def register(cls) -> None:
|
52
|
+
"""Register plugin group `juham`."""
|
53
|
+
Application.register_plugin_group("juham")
|
juham_automation/ts/__init__.py
CHANGED
@@ -1,25 +1,27 @@
|
|
1
|
-
"""
|
2
|
-
Description
|
3
|
-
===========
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
"""
|
8
|
-
|
9
|
-
from .energycostcalculator_ts import EnergyCostCalculatorTs
|
10
|
-
from .log_ts import LogTs
|
11
|
-
from .power_ts import PowerTs
|
12
|
-
from .powerplan_ts import PowerPlanTs
|
13
|
-
from .powermeter_ts import PowerMeterTs
|
14
|
-
from .electricityprice_ts import ElectricityPriceTs
|
15
|
-
from .forecast_ts import ForecastTs
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
"
|
20
|
-
"
|
21
|
-
"
|
22
|
-
"
|
23
|
-
"
|
24
|
-
"
|
25
|
-
|
1
|
+
"""
|
2
|
+
Description
|
3
|
+
===========
|
4
|
+
|
5
|
+
Time series recorders for Juha's Ultimate Home Automation classes.
|
6
|
+
|
7
|
+
"""
|
8
|
+
|
9
|
+
from .energycostcalculator_ts import EnergyCostCalculatorTs
|
10
|
+
from .log_ts import LogTs
|
11
|
+
from .power_ts import PowerTs
|
12
|
+
from .powerplan_ts import PowerPlanTs
|
13
|
+
from .powermeter_ts import PowerMeterTs
|
14
|
+
from .electricityprice_ts import ElectricityPriceTs
|
15
|
+
from .forecast_ts import ForecastTs
|
16
|
+
from .energybalancer_ts import EnergyBalancerTs
|
17
|
+
|
18
|
+
__all__ = [
|
19
|
+
"EnergyCostCalculatorTs",
|
20
|
+
"ForecastTs",
|
21
|
+
"LogTs",
|
22
|
+
"PowerTs",
|
23
|
+
"PowerPlanTs",
|
24
|
+
"PowerMeterTs",
|
25
|
+
"ElectricityPriceTs",
|
26
|
+
"EnergyBalancerTs",
|
27
|
+
]
|
@@ -1,51 +1,51 @@
|
|
1
|
-
from datetime import datetime
|
2
|
-
import time
|
3
|
-
import json
|
4
|
-
from typing import Any, Dict, Optional, cast
|
5
|
-
from typing_extensions import override
|
6
|
-
|
7
|
-
from masterpiece.mqtt import Mqtt, MqttMsg
|
8
|
-
from juham_core.timeutils import epoc2utc
|
9
|
-
from juham_core import JuhamTs
|
10
|
-
|
11
|
-
|
12
|
-
class ElectricityPriceTs(JuhamTs):
|
13
|
-
"""Spot electricity price for reading hourly electricity prices from"""
|
14
|
-
|
15
|
-
def __init__(self, name: str = "electricityprice_ts") -> None:
|
16
|
-
super().__init__(name)
|
17
|
-
|
18
|
-
self.spot_topic = self.make_topic_name("spot")
|
19
|
-
|
20
|
-
@override
|
21
|
-
def on_connect(self, client: object, userdata: Any, flags: int, rc: int) -> None:
|
22
|
-
super().on_connect(client, userdata, flags, rc)
|
23
|
-
if rc == 0:
|
24
|
-
self.subscribe(self.spot_topic)
|
25
|
-
|
26
|
-
@override
|
27
|
-
def on_message(self, client: object, userdata: Any, msg: MqttMsg) -> None:
|
28
|
-
if msg.topic == self.spot_topic:
|
29
|
-
em = json.loads(msg.payload.decode())
|
30
|
-
self.on_spot(em)
|
31
|
-
else:
|
32
|
-
super().on_message(client, userdata, msg)
|
33
|
-
|
34
|
-
def on_spot(self, m: dict[Any, Any]) -> None:
|
35
|
-
"""Write hourly spot electricity prices to time series database.
|
36
|
-
|
37
|
-
Args:
|
38
|
-
m (dict): holding hourlys spot electricity prices
|
39
|
-
"""
|
40
|
-
grid_cost : float
|
41
|
-
for h in m:
|
42
|
-
if "GridCost" in h:
|
43
|
-
grid_cost = h["GridCost"]
|
44
|
-
point = (
|
45
|
-
self.measurement("spot")
|
46
|
-
.tag("hour", h["Timestamp"])
|
47
|
-
.field("value", h["PriceWithTax"])
|
48
|
-
.field("grid", grid_cost)
|
49
|
-
.time(epoc2utc(h["Timestamp"]))
|
50
|
-
)
|
51
|
-
self.write(point)
|
1
|
+
from datetime import datetime
|
2
|
+
import time
|
3
|
+
import json
|
4
|
+
from typing import Any, Dict, Optional, cast
|
5
|
+
from typing_extensions import override
|
6
|
+
|
7
|
+
from masterpiece.mqtt import Mqtt, MqttMsg
|
8
|
+
from juham_core.timeutils import epoc2utc
|
9
|
+
from juham_core import JuhamTs
|
10
|
+
|
11
|
+
|
12
|
+
class ElectricityPriceTs(JuhamTs):
|
13
|
+
"""Spot electricity price for reading hourly electricity prices from"""
|
14
|
+
|
15
|
+
def __init__(self, name: str = "electricityprice_ts") -> None:
|
16
|
+
super().__init__(name)
|
17
|
+
|
18
|
+
self.spot_topic = self.make_topic_name("spot")
|
19
|
+
|
20
|
+
@override
|
21
|
+
def on_connect(self, client: object, userdata: Any, flags: int, rc: int) -> None:
|
22
|
+
super().on_connect(client, userdata, flags, rc)
|
23
|
+
if rc == 0:
|
24
|
+
self.subscribe(self.spot_topic)
|
25
|
+
|
26
|
+
@override
|
27
|
+
def on_message(self, client: object, userdata: Any, msg: MqttMsg) -> None:
|
28
|
+
if msg.topic == self.spot_topic:
|
29
|
+
em = json.loads(msg.payload.decode())
|
30
|
+
self.on_spot(em)
|
31
|
+
else:
|
32
|
+
super().on_message(client, userdata, msg)
|
33
|
+
|
34
|
+
def on_spot(self, m: dict[Any, Any]) -> None:
|
35
|
+
"""Write hourly spot electricity prices to time series database.
|
36
|
+
|
37
|
+
Args:
|
38
|
+
m (dict): holding hourlys spot electricity prices
|
39
|
+
"""
|
40
|
+
grid_cost : float
|
41
|
+
for h in m:
|
42
|
+
if "GridCost" in h:
|
43
|
+
grid_cost = h["GridCost"]
|
44
|
+
point = (
|
45
|
+
self.measurement("spot")
|
46
|
+
.tag("hour", h["Timestamp"])
|
47
|
+
.field("value", h["PriceWithTax"])
|
48
|
+
.field("grid", grid_cost)
|
49
|
+
.time(epoc2utc(h["Timestamp"]))
|
50
|
+
)
|
51
|
+
self.write(point)
|
@@ -0,0 +1,47 @@
|
|
1
|
+
import json
|
2
|
+
from typing import Any
|
3
|
+
from typing_extensions import override
|
4
|
+
|
5
|
+
from masterpiece.mqtt import MqttMsg
|
6
|
+
|
7
|
+
from juham_core import JuhamTs
|
8
|
+
from juham_core.timeutils import epoc2utc
|
9
|
+
|
10
|
+
|
11
|
+
class EnergyBalancerTs(JuhamTs):
|
12
|
+
"""Heating optimizer diagnosis.
|
13
|
+
|
14
|
+
This class listens the "energybalance" MQTT topic and records the
|
15
|
+
messages to time series database.
|
16
|
+
"""
|
17
|
+
|
18
|
+
def __init__(self, name: str = "energybalancer_ts") -> None:
|
19
|
+
"""Construct record object with the given name."""
|
20
|
+
|
21
|
+
super().__init__(name)
|
22
|
+
self.topic_name = self.make_topic_name("energybalance")
|
23
|
+
|
24
|
+
@override
|
25
|
+
def on_connect(self, client: object, userdata: Any, flags: int, rc: int) -> None:
|
26
|
+
super().on_connect(client, userdata, flags, rc)
|
27
|
+
self.subscribe(self.topic_name)
|
28
|
+
self.debug(f"Subscribed to {self.topic_name}")
|
29
|
+
|
30
|
+
@override
|
31
|
+
def on_message(self, client: object, userdata: Any, msg: MqttMsg) -> None:
|
32
|
+
"""Standard mqtt message notification method.
|
33
|
+
|
34
|
+
This method is called upon new arrived message.
|
35
|
+
"""
|
36
|
+
|
37
|
+
m = json.loads(msg.payload.decode())
|
38
|
+
point = (
|
39
|
+
self.measurement("energybalance")
|
40
|
+
.tag("Unit", m["Unit"])
|
41
|
+
.field("Mode", m["Mode"])
|
42
|
+
.field("Rc", m["Rc"])
|
43
|
+
.field("CurrentBalance", m["CurrentBalance"])
|
44
|
+
.field("NeededBalance", m["NeededBalance"])
|
45
|
+
.time(epoc2utc(m["Timestamp"]))
|
46
|
+
)
|
47
|
+
self.write(point)
|