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
self.testlogger.info("- 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")