Hi guys!
In this example, I extend the code of my previous post: App #3: Smart Radiator to turn OFF the radiator of any room of my apartment if one of its windows is open.
In other to make generic this automation, I use a particular naming pattern to associate related items (e.g., livingroom_radiator
, livingroom_window
). In other words, by doing some string manipulation (see __get_radiator
) I can extract the room
where the opened window belongs.
This automation is not final or complete, and probably there is a way to make more generic. My aim is to share examples that maybe can help new members starting with HA+AppDaemon.
Suggestions or recommendations to improve the implementation are welcome!
Entities
binary_sensor.bedroom_window
binary_sensor.kitchen_window
switch.bedroom_radiator
switch.kitchen_radiator
groups.yaml
windows:
name: Windows
view: no
entities:
- binary_sensor.bedroom_window
- binary_sensor.kitchen_window
...
app.yaml
gen_smart_radiator:
module: gen_smart_radiator
class: GenSmartRadiator
group: group.windows
gen_smart_radiator.py
import appdaemon.plugins.hass.hassapi as hass
class GenSmartRadiator(hass.Hass):
def initialize(self):
self.log('initializing ...')
group = self.get_state((self.args["group"], attribute = "all")
sensors = group["attributes"]["entity_id"]
for sensor in sensors:
self.log('subscribing to: ' + str(sensor))
self.listen_state(self.on_windows_open, sensor, new="on")
def on_windows_open(self, entity, attribute, old, new, kwargs):
radiator = self.__get_radiator(entity)
if self.get_state(radiator) == "on":
self.log(f'Turning off {radiator}')
self.turn_off(radiator)
else: # only for debugging purposes
self.log(f'The radiator {radiator} is off, enjoy the fresh air ...')
def __get_radiator(self, entity):
_device, entity_name = self.split_entity(entity)
room, _window = entity_name.split('_')
return f'switch.{room}_radiator'
If you have solved a similar problem using a better approach, please share it!
Happy coding!
Humberto
Edit #1 Avoid hardcode entities names in the automation code. Thanks @swiftlyfalling @Burningstone
Previous post: App #4: Boiler Alert
Next post: App #6: Window Alert