Appdaemon listen_state() numeric value

Hi appdaemon specialists,

last week I spend quite some time to convert some of my YAML automations into appdaemon, so far so good, but I couldn’t find a decent solution to convert the below code. Plan is to find out if the power measured by the “sensor.nodeid_52_power” is between 2 and 420 Watt.

Did look at the “listen_state()” but could not find how to activate the callback on a value between defined limits. I’m afraid it doesn’t exist…

But how to implement this functionality in AD?

Thanks for reading

  - platform: template
    scan_interval: 5
          minutes: 5
        value_template: >
          {% if states('sensor.nodeid_52_power') | float < 420 and
                states('sensor.nodeid_52_power') | float > 2     %}
          {% else %}

Just test it in your callback. Something like:

if float(new) < 420 and float(new) > 2:
    do sonething...

Or in Python, you can simplify it as:

if 2 < float(new) < 420:
     do something...

No, that would be too easy :rofl: must have been one of this late night non-sense coming out of my mouth.

1 Like

Thanks for showing me the proper direction !

I implemented as below, activate a callback when the power did change and set a binary_sensor according the actual power.

With the binary_sensors I have the possibility to use the duration=xxx.

Its already working for some time exactly as intended…

class kelder(hass.Hass):

    def initialize(self):

        self.set_state("binary_sensor.power_dh_done", state = False)        # 400 Watt
        self.set_state("binary_sensor.power_dh_operating", state = False)   # 600 Watt
        self.set_state("binary_sensor.power_dh_waterfull", state = False)   # 2.5 Watt
        self.set_state("binary_sensor.power_dh_250", state = False)         # >250 Watt
        self.set_state("binary_sensor.dehumidifyer_water_full", state = False)

        # init logger"- Dehumidifyer Kelder() init")

        self.listen_state(self.nodeid_52_power_changed, "sensor.nodeid_52_power", attribute = "state")

        self.listen_state(self.dehumidifyer_done,       "binary_sensor.power_dh_done", attribute="state", new="on", duration=600 )
        self.listen_state(self.dehumidifyer_waterfull,  "binary_sensor.power_dh_waterfull", attribute="state", new="on", duration=180 )

        # forced switch off when dehumidifyer operating >= 1 hour
        self.listen_state(self.dehumidifyer_forced_off,  "binary_sensor.power_dh_operating", attribute="state", new="on", duration=3600 )

        # clear waterfull flag when operating at power >250Watt
        self.listen_state(self.dehumidifyer_clear_waterfull,  "binary_sensor.power_dh_250", attribute="state", new="on", duration=180 )

    # update the power binary sensors
    def nodeid_52_power_changed(self, entity, attribute, old, new, kwargs)  :
        power = float(self.get_state("sensor.nodeid_52_power") )

        if 0.5 <= power < 5 :
            self.set_state("binary_sensor.power_dh_waterfull", state = "on")
        else :
            self.set_state("binary_sensor.power_dh_waterfull", state = "off")

        if 5 <= power < 420 :
            self.set_state("binary_sensor.power_dh_done", state = "on")
        else :
            self.set_state("binary_sensor.power_dh_done", state = "off")

        if power > 0 :
            self.set_state("binary_sensor.power_dh_operating", state = "on")
        else :
            self.set_state("binary_sensor.power_dh_operating", state = "off")

        # normal operating, needed to clear waterfull flag
        if power > 250 :
            self.set_state("binary_sensor.power_dh_250", state = "on")
        else :
            self.set_state("binary_sensor.power_dh_250", state = "off")