How to check if a value remains for x min

So I wrote this up

import appdaemon.plugins.hass.hassapi as hass
import datetime

class announce(hass.Hass):

    def initialize(self):
        self.listen_state(self.washing_machine_done_function, "sensor.washing_machine_energy")
        self.media_player = 'media_player.living_room'



    def washing_machine_done_function(self, entity, attribute, old, new, kwargs):
        newfloat=float(new)
        if newfloat < 5:
            self.log("Announcing that the washing machine is now done")
            self.call_service("media_player/volume_set", entity_id=self.media_player, volume_level = 50)
            self.call_service("tts/google_say", entity_id=self.media_player, message = "The washing machine is done.  Time to go put your clothes in the dryer")

Which works amazing. But I got a false alert today while doing laundry. Right after the washer started it dropped below 5 for a moment while it was doing whatever it is its doing. How can I tell it to ok its below 5 but wait 5 mins to check if its still below 5.

Should I write a run_in in there and tell it to sleep for 5 mins then do a get_state and check it? Or is there a simpler method? Thanks.

This is what I’m going to run with :slight_smile:

import appdaemon.plugins.hass.hassapi as hass
import datetime

class announce(hass.Hass):

    def initialize(self):
        self.listen_state(self.washing_machine_first_check_cb, "sensor.washing_machine_energy")
        self.media_player = 'media_player.living_room'



# Washing Machine Functions
    def washing_machine_first_check_cb(self, entity, attribute, old, new, kwargs):
        newfloat=float(new)
        if newfloat < 5:
            self.run_in(washing_machine_done_function_cb, 900)

    def washing_machine_done_function_cb(self, kwargs):
        energy_reading = self.get_state("sensor.washing_machine_energy")
        newfloat=float(energy_reading)
        if newfloat < 5:
            self.log("Announcing that the washing machine is now done")
            self.call_service("media_player/volume_set", entity_id=self.media_player, volume_level = 50)
            self.call_service("tts/google_say", entity_id=self.media_player, message = "The washing machine is done.  Time to go put your clothes in the dryer")

Try adding the duration parameter

self.listen_state(self.washing_machine_first_check_cb, “sensor.washing_machine_energy”, duration = 900)
1 Like

what GP said or at least change it like this:

    def initialize(self):
        self.washer_handle = None
        self.listen_state(self.washing_machine_first_check_cb, "sensor.washing_machine_energy")
        self.media_player = 'media_player.living_room'

    def washing_machine_first_check_cb(self, entity, attribute, old, new, kwargs):
        newfloat=float(new)
        if newfloat < 5 and self.washer_handle == None:
            self.washer_handle = self.run_in(washing_machine_done_function_cb, 900)
        else:
            self.cancel_timer(self.washer_handle)

    def washing_machine_done_function_cb(self, kwargs):
            self.log("Announcing that the washing machine is now done")
            self.call_service("media_player/volume_set", entity_id=self.media_player, volume_level = 50)
            self.call_service("tts/google_say", entity_id=self.media_player, message = "The washing machine is done.  Time to go put your clothes in the dryer")

right now you keep starting timers and let them run out with no result, untill the changes are below 5. if there are more changes below 5 then you get a shipload of notifies after another.
with what i gave you, you stop the timer as soon as it changes to above 5 again, and you start the timer only once.

1 Like

@gpbenton I was sifting through the docs last night looking for a list of all the different CB constraints that I could use for this and something else and I couldn’t find the list. I was trying to make you proud :slight_smile: since you introduced those to me a few weeks ago.

@ReneTode Thank you once again for the education that you keep providing me. I’m sure you can see that I"m growing :slight_smile: I’m actually writing apps in here that do function. Now I just need to figure out how to better optimize my apps.

You all are so wonderful for helping this noob out.

i love it to see how you get better, thats thanks enough :wink:
and yeah i see it :wink: