Self.listen_state(...., duration=xx) callback not firing

Hi guys,
I want to automate my bathroom ventilation. I want t keep it simple and just have a after-run time and static humidity values.

For the after-run part, I tried to utilize the duration parameter, but I do not get it working. The callback is not executed after the duration is expired.

apps.yaml snipped:

lueftersteuerung_duschbad:
  module: lueftersteuerung_dusche
  class: lueftersteuerung
  luefter_nachlauf: 60
  luefter_nachlauf_max: 2400
  luftfeuchte: 60

lueftersteuerung_dusche.py:

import appdaemon.plugins.hass.hassapi as hass
import datetime
 
class lueftersteuerung(hass.Hass):
 
  def initialize(self):
   self.listen_state(self.fan_timeout,"switch.luefter_dusche_90", old="off", new="on", duration=int(self.args["luefter_nachlauf"]))
    
  def minute_task(self, kwargs):
    fan = self.get_state("switch.luefter_dusche_90")
    light = self.get_state("switch.licht_dusche_89")
    humidity = float(self.get_state("sensor.feuchte_dusche_93"))
    act = datetime.datetime.replace(datetime.datetime.now(datetime.timezone.utc)).timestamp()
    fan_last_change = int(act - datetime.datetime.replace(self.convert_utc(self.entities.switch.luefter_dusche_90.last_changed)).timestamp())
    light_last_change = int(act - datetime.datetime.replace(self.convert_utc(self.entities.switch.copy_licht_dusche.last_changed)).timestamp())
    self.log("LĂĽfter letzte Ă„nderung vor {}s".format(fan_last_change))
    self.log("Licht letzte Ă„nderung vor {}s".format(light_last_change))
    self.log("rH%: {}".format(humidity))
    if (fan=="on" and light=="off" and (((humidity<int(self.args["luftfeuchte"])) and (light_last_change>int(self.args["luefter_nachlauf"]))) or (light_last_change>int(self.args["luefter_nachlauf_max"])))):
      self.turn_off("switch.luefter_dusche_90")
      self.cancel_timer(self.minutely)
      self.log('Luefter abgeschaltet')

  def fan_timeout(self, entity, attribute, old, new, kwargs):
    self.log('Nachlauf abgelaufen')
    time = datetime.time(0, 0, 0)
    self.minutely = self.run_minutely(self.minute_task, time)

Without the duration parameter, the callback is immediately firing and working as expected.
What am I doing wrong?

Thanks Dirk

Did you try duration=60 instead of using the argument? Maybe you could also try without the int() function.

Do you get any error in the log?

I just tried your app in my test box, and the callback is called correctly after 60s there.

the code itself seems correct.
if it works without the duration and not with the duration, the only thing that i can think of is that the state is not continously “on” for 60 seconds.

if there is anything else that changes the state from the switch before the 60 seconds are over, it will be reset.

there is 1 other thing that can be happening and that is that because you made mistakes during developing AD is in a strange state. i have seen before in other situations that people had strange results untill they have restarted AD. so make sure you do that also.

Hi,

thanks, that was a very good hint.
I use a Vera Plus as a Z-Wave hub, and that seems to alter the entity state although the physical state has not changed. As a workaround I have created Template Switches for the Fan and Light switch and use them in the code above.
Now it works!

Thanks Dirk

2 Likes