State change persistent through reboots?

I have a question about some automations.

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 :slight_smile:

Thanks

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.

2 Likes

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.

I am fairly new to HA. Any suggestions or examples on how to adjust the automation?

Thanks!

I need to add a condition don’t I? That the condition has to be “it was running” before it can say it’s not?

The answer to your original question is: no.

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.

I didn’t mention a timer (but if we are talking about timers, active timers don’t survive a restart).

Post the automation so we can better understand how it’s affected by a restart.

- id: '1634228461864'
  alias: Washer Idle
  description: ''
  trigger:
  - type: power
    platform: device
    device_id: aea5e5d58d5ef27df994d55b6071fe50
    entity_id: sensor.sonoffs31_1_energy_power
    domain: sensor
    below: 5
    for:
      hours: 0
      minutes: 4
      seconds: 0
      milliseconds: 0
  condition:
  - condition: state
    entity_id: input_text.washer_state
    state: Washing
  action:
  - service: input_text.set_value
    target:
      entity_id: input_text.washer_state
    data:
      value: Idle
  - service: notify.mobile_app_iphone
    data:
      message: Washer is Idle
      title: Home Notice

I added the “condition” block today to see if that would help.

Thanks for taking a look! :slight_smile:

This doesn’t survive a restart/reload:

    for:
      hours: 0
      minutes: 4
      seconds: 0
      milliseconds: 0

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

I don’t think it should.

the only time it should trigger is if the value was above 5 and then went below 5 and stayed there for 4 minutes.

if HA is restarted during the 4 minute delay then the automation trigger would be reset and there would be no action taken.

In your scenario nothing should happen at all.

The trigger is going from above to below 5 for 4 minutes.

I’m not sure why that would be triggered after a HA restart if the value was already below 5 when HA was restarted?

Unless power device triggers work completely differently than other “state changed” device triggers?

if so it’s not an issue with the “for:”, it’s an issue with the power trigger.

@brunkj

Could you look at the automation trace to see what is actually triggering the automation?

I don’t know if it will help but maybe the sensor is in some state after restart that HA interprets as being “above 5”.

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.

2 Likes

Ill run a test and see if i can get a trace and get some info.

Here is the screenshot from the trace.

I have the json file as well. Looks like it’s triggered by numeric state change.

Looks like the “changed variables” the state was “unknown” then changed.

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

Which confirms my prediction:

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?

  1. MQTT Sensor via Tasmota integration
  2. MQTT Sensor via MQTT Discovery
  3. MQTT Sensor via manual configuration

it’s mqtt tasmota integration.

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”

Thanks :slight_smile: I think with that info I can make it work

Do you mean the first one in the list: Tasmota integration?

That’s the one where your device is configured with SetOption19 0 and you install the Tasmota integration in Home Assistant.

correct. thats the one