App #3: Smart Radiator

Hello!

Here is another simple app :slightly_smiling_face:

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!

App #3: Smart Radiator
Turn off the radiator of the LIVING_ROOM if one of its windows is opened.

Entities

binary_sensor.livingroom_window1
binary_sensor.livingroom_window2
switch.livingroom_radiator

groups.yaml

lr_windows:  
  name: LR Windows
  view: yes
  entities:
    - binary_sensor.livingroom_window1
    - binary_sensor.livingroom_window2

app.yaml

smart_radiator:
  module: smart_radiator
  class: SmartRadiator
  windows: group.lr_windows
  radiator: switch.livingroom_radiator

smart_radiator.py

import appdaemon.plugins.hass.hassapi as hass

class SmartRadiator(hass.Hass):

    def initialize(self):
        self.log('initializing ...')
        group = self.get_state(self.args["windows"], attribute = "all")
        sensors = group["attributes"]["entity_id"]
       
        for sensor in sensors:
            self.log(f'Subscribing to: {self.friendly_name(sensor)}')
            self.listen_state(self.on_windows_open, sensor, new="on")
        
    def on_windows_open(self, entity, attribute, old, new, kwargs):
        radiator = self.args["radiator"]
        if self.get_state(radiator) == "on":
            self.log(f'Turning off the {self.friendly_name(radiator)} ')
            self.turn_off(radiator)
        else: # only for debugging purposes
            self.log(f'The {self.friendly_name(radiator)} is off, enjoy the fresh air ...')
        

Note: This implementation is not generic, but that it’s on the pipeline :wink:

Happy coding!
Humberto

Edit #1 Avoid hardcode entities names in the automation code. Thanks @swiftlyfalling @Burningstone

Previous post: App #2: Smart Light
Next post: App #4: Boiler Alert

I think you missed some code in pasting. This app doesn’t DO anything. It’s logs something when it should do something, but doesn’t actually do it.

Also, you’re listening to each window, which is fine. But you’re not testing each window in “on_windows_open”. Since you probably only care about whether all the windows are closed or not, you can just listen for the state of the group and ignore each individual sensor. Or, optionally, you’ll need to cycle through each window again (as you did for listen_state()) and make sure they are ALL closed.

Hi @swiftlyfalling

Good catch! I probably removed self.turn_off("sensor.livingroom_radiator") by mistake :slightly_smiling_face:

In this example, I consider that if at least one window is open and the radiator is ON, it needs to be turned OFF.

I was trying to keep the demo simple. Indeed, my final automation should check that if ALL the windows are CLOSED and the radiator is scheduled to be ON, it should be turned ON. But I’m not there yet :frowning:. I want to have different schedules (by radiator) during the week, but I have to check how I can do it.

Thanks again for your comments :+1:

Ahh… I see now. You’re not turning the radiator ON, so you don’t need to check all the windows. You’re only turning it OFF. In which case, this works.

@swiftlyfalling yes. The Turn OFF part is work in progress :slightly_smiling_face:

@swiftlyfalling what do you think about this solution App #5: Smart Radiator (Generic)?