I have a simple automation setup that monitors the washing machine. If the power usage on that smart plug is < X for 4 minutes, it sends a notice saying the washing machine is done. This works, except in one scenario.
If I restart homeassistant. Is there anyway for that kind of state to persist through reboots? So that notices about the washing machine being done aren’t sent when the washer isn’t running
Your problem is from a lack of definition of your automation. The definition of the washer being finished is not that it doesn’t use power for 4 minutes, it is that it has been using power and then stops using power.
I use an input_select for that with my washing machine.
When the machine is started the select is set to “washing” and when the power usage is low again and the state is “washing” I know that the washing cycle is completed.
If you restart Home Assistant, or Reload Automations, any for, delay, wait_template, wait_for_trigger, repeat, etc that is in progress is cancelled. So if you have a State Trigger with a for: '00:04:00' and it’s in progress, it will be cancelled and reset after the restart/reload.
I don’t think there is a timer running. In this scenario the washer is off. I restart homeassistant. 4 minutes after the restart i get the notice saying that the washer is idle. But it was already idle. So I think when HA starts, the power rating comes in show < X watts. It triggers that 4 minute timer.
Assume the trigger has detected that the sensor’s value has been below 5 for the past 3 minutes and 45 seconds. It only has 15 more seconds for the count to reach 4 minutes and then it will trigger the automation. However, if you restart Home Assistant, or Reload Automations, that 3 minutes and 45 seconds is discarded and reset to zero on restart/reload. Effectively, the automation and its trigger are reset and that’s why you observed this behavior:
I restart homeassistant. 4 minutes after the restart i get the notice saying that the washer is idle
It depends on the behavior of sensor.sonoffs31_1_energy_power. If its state value is momentarily unknown, or unavailable, upon startup and then changes to a value below 5, its my understanding that the transition from either of those states to a value below the threshold of 5 is sufficient to trigger the Device Numeric Trigger. (It’s challenging to devise an experiment to confirm this behavior.)
On the other hand, if on startup its state value is instantly restored to a value below 5 then, I agree with you, it wouldn’t cause it to trigger because it’s not crossing the threshold value.
I also agree with you that the best way to discover what actually happens shortly after startup is to examine the automation’s trace. However, concerning the OP’s original question, an active for is toast upon restart/reload.
This would explain what’s happening. I believe (please point me in the right direction if im wrong) but HA doesn’t have a “persist” feature like OH and some others have where at start up, all entities are set to their “last recorded” value. I read through a number of forum threads about this. Seems the MQTT retain is the best way to do that, but it will still track a state change. So in this scenario that would still be the case.
I believe adding the condition to my automation of after 4 minutes with power < 5watt send the notification ONLY if the previous state was “washing”.
I tested that and that seems to work. Not 100% what I was looking for, but in the end I believe that would mitigate the extra notices.
As mentioned, pretty new to HA. So if there are better or other ways to go about it I am all ears! lol
If its state value is momentarily unknown, or unavailable, upon startup and then changes to a value below 5, its my understanding that the transition from either of those states to a value below the threshold of 5 is sufficient to trigger the Device Numeric Trigger.
I believe this is the PR that implemented this behavior:
Other than Helpers (whose values are “persisted”), on startup an entity’s value is set by its integration.
What are you using for sensor.sonoffs31_1_energy_power?
The helper thing could be the answer since i have a helper already set to “idle” or “washing” That’s what i was testing with the condition of only do it if the previous state was “washing”