Need help to put a condition with "for:" attribute into action sequence

I need to detect my washingmachine being in standby after doing washing to initiate an alert.
A pattern of power consumption was found. Basically: if washingmachine consumed >50W, then <50W, check if it consumes <30W for 5 minutes after the first trigger.

I used the following:

- alias: Wachingmachine just off
  trigger:
    - platform: state
      entity_id: binary_sensor.washingmachine_highpower
      from: 'on'
      to: 'off'
      for: '00:00:15' # "delay" to make sure the power readings are accurate and not delayed.
  condition: # check if washingmachine was "on" prior - might not be needed. works.
    condition: state
    entity_id: input_select.washingmachine_power_state
    state: 'an'
  action:
   - condition: state
      entity_id: binary_sensor.washingmachine_midpower # check if power is < threshold 30W
      state: 'off'
      for: '00:05:00'
    - service: input_select.select_option # Switch state to "Standby after on".
      data_template:
        entity_id: input_select.washingmachine_power_state
        option: 'gerade aus'
    - service: homeassistant.turn_on # audio notification to take out laundry from washingmachine
      entity_id: script.play_alert_washingmachine

Trigger works fine. I added a delay, as I was not sure that the power detection fires in the correct order. This is for stability.
I debugged each step of the action and figured that the condition statement is not respecting the “for:” attribute. I tried

for: '00:05:00'

and

for:
  hours: 0
  minutes: 5
  seconds: 0

the condition is never waiting, the action after the condition is triggered immediately.
I already filed a bug, but I need a solution.

I thought I could do the following:

  • change the trigger to “power<30W” for 5 minutes
  • put a condition into the action to check “if power>50W 00:05:10 minutes ago”

This seems not necessarily reliable.

Anybody an idea how to solve this properly?

Thanks!

It’s not firing because your spacing is wrong. You falsely submitted a bug.

     |
     v
   - condition: state
      entity_id: binary_sensor.washingmachine_midpower # check if power is < threshold 30W
      state: 'off'
      for: '00:05:00'
      ^
      |

Change that to this:

     |
     v
   - condition: state
     entity_id: binary_sensor.washingmachine_midpower # check if power is < threshold 30W
     state: 'off'
     for: '00:05:00'
     ^
     |

EDIT: Everything after that condition has the wrong spacing too. Even your services.

Sorry, the spacing issue came from pasting here - didnt check properly. I had all spacing correct… I happened as I removed all debugging notifications from the automation… Sorry for this.

Original state without wrong spacing (as used):

- alias: Wachingmachine just off
  trigger:
    - platform: state
      entity_id: binary_sensor.washingmachine_highpower
      from: 'on'
      to: 'off'
      for: '00:00:15' # "delay" to make sure the power readings are accurate and not delayed.
  condition: # check if washingmachine was "on" prior - might not be needed. works.
    condition: state
    entity_id: input_select.washingmachine_power_state
    state: 'an'
  action:
    - condition: state
      entity_id: binary_sensor.washingmachine_midpower # check if power is < threshold 30W
      state: 'off'
      for: '00:05:00'
    - service: input_select.select_option # Switch state to "Standby after on".
      data_template:
        entity_id: input_select.washingmachine_power_state
        option: 'gerade aus'
    - service: homeassistant.turn_on # audio notification to take out laundry from washingmachine
      entity_id: script.play_alert_washingmachine

I coudlnt get it to work and changed to the following, which is now working. But I hate that I have to change too time variables in case my first guess is not optimal… (I haev to change the last_updated time difference according to the trigger duration):

# Detect when washingmachine goes to standby (just off) from on 
- alias: Wachingmachine just off
  trigger:
    - platform: state
      entity_id: binary_sensor.washingmachine_midpower
      from: 'on'
      to: 'off'
      for: '00:05:00' # if changed, change condition time threshold in action sequence as well
  condition:
    condition: state
    entity_id: input_select.washingmachine_power_state
    state: 'an'
  action:
    - condition: and
      conditions:
        - condition: state
          entity_id: input_select.washingmachine_power_state
          state: 'an'
        - condition: template
          value_template: >
            {{ as_timestamp(now()) - as_timestamp(states.binary_sensor.washingmachine_highpower.last_updated) < 310 }}
    - service: input_select.select_option
      data_template:
        entity_id: input_select.washingmachine_power_state
        option: 'gerade aus'
    - service: script.play_alert_washingmachine
    - service: persistent_notification.create
      data:
        title: 'Waschmaschine fertig!'
        message: '{{now().strftime("%d.%m.%Y %H:%M:%S")}}: Die Waschmaschine ist soeben fertig geworden. Bitte entladen!'

Sorry for the multiple edits - I have copy and paste problems. Above looks now as I used it. The autoamtion fired, all things got executed, but the waiting (for) was missing. The condition passed always either immediately or not at all - as stated: the “for” part was neglected.

I felt free to submit a bug (I tested heavily): https://github.com/home-assistant/home-assistant/issues/22762
The bug description contains the full not working script including the debugging notifications - only full copy and paste without alterations to make it better readable here.

Ah ok, I’m starting to see what you consider an issue. You expect the condition to wait for 5 minutes. In actuality, that’s not how they work. The conditions check to see if they have been on for 5 minutes and that’s it.

If you want to get the simulation of it being on for 5 minutes and then checking. You need a 5 minute delay in combination with the condition.

EDIT: I also see that you arelooking at 2 different power levels. You’re triggering off high power, and then checking mid power. Do you have these sensors readily available and can you prove that they have been off less than 5 minutes?

your first automation triggers off highpower, then checks if midpower has been off for 5 minutes.

no… the idea is as follow:

  1. a trigger
  2. after trigger check if another condition is true/false for the whole period of 5 minutes. The condition should not change within these 5 minutes

I checked “delay” - it works fine, but doesnt solve my case… The power consumption is changing and the 30W state might be on only for 10 or 20s, but this would indicate the washingmachine is not done yet… Thus I need the binary_sensor to be off for whole 5 minutes…

I solved the issue above in a reverse way:

  1. trigger if the condition <30W is valid for 5 minutes
  2. then check if 05:10min ago the condition of the highpower sensor (>50W) was on… or in this case: updated. The latter is not ideal, but works well as overall it still only fires when >50 goes from true to false. IF it would go from false to true, the trigger of <30W for 5 minutes would fail.

Then you should have 2 automations with a timeout in the first.

- alias: Wachingmachine just off
  trigger:
    - platform: state
      entity_id: binary_sensor.washingmachine_highpower
      from: 'on'
      to: 'off'
      for: '00:00:15' # "delay" to make sure the power readings are accurate and not delayed.
  condition:
    condition: state
    entity_id: input_select.washingmachine_power_state
    state: 'an'
  action:
    - service: automation.turn_on
      entity_id: automation.washingmachinecheck
    - wait_template: "{{ is_state('automation.washingmachinecheck', 'off') }}"
      timeout: '01:00:00'
    - service: automation.turn_off
      entity_id: automation.washingmachinecheck
- alias: washingmachinecheck
  trigger:
    - platform: state
      entity_id: binary_sensor.washingmachine_midpower
      from: 'on'
      to: 'off'
      for: '00:05:00' # if changed, change condition time threshold in action sequence as well
  condition:
    condition: state
    entity_id: input_select.washingmachine_power_state
    state: 'an'
  action:
    - service: input_select.select_option
      data_template:
        entity_id: input_select.washingmachine_power_state
        option: 'gerade aus'
    - service: script.play_alert_washingmachine
    - service: persistent_notification.create
      data:
        title: 'Waschmaschine fertig!'
        message: '{{now().strftime("%d.%m.%Y %H:%M:%S")}}: Die Waschmaschine ist soeben fertig geworden. Bitte entladen!'
    - service: automation.turn_off
      entity_id: automation.washingmachinecheck

Some more background:
Overall this is a state machine with 4 states: on, off, just on, just off - just off indicating readiness to take out laundry. State is represented in the input_select entity (not sure if this is the best way - events might also work).

Your solution works and I like it better than mine!

Nevertheless the bug I think is still valid. The condition within the “action” sequence should also respect the “for” attribute, right?

Thanks for the help!!! And especially the very fast help!!! Thread can be closed.

They do respect the for. But it doesn’t sit there and wait. It executes immediately. Has this item been on/off for the last 5 minutes? Not lets wait for 5 minutes and continue when 5 minutes has been met.

I just had an idea, why the solution wouldnt work as desired, but some tinkering should help:

I need the “<30W for 5 minutes” check (nearly) directly after the trigger <50W triggered. I cannot have the check happen 5 minutes later.
-> I would need to alter your timeout to cancel the second automation at 5:10minutes, then it should work…

Well the sequence in action area should be executed one after the other according to what I read… I never thought that then the condition in the second or third step would already started to be checked when the trigger fires (assume the first step is a delay)
This seems not logical, when a delay is used…

And the item was not off for 5 minutes. If >50 was on prior, also >30W was on prior.
If >50W turns to falls, >30W is still on and for sure not off…
So the for didnt get checked for sure!

You’re thinking of it wrong. It’s an immediate check. And it looks in the past 5 minutes. Not future 5 minutes.

The only reason it works with a trigger is because it’s looking in the past 5 minutes and then triggers.

that explains. thanks. I guess then the bug can be deleted. I will do this.

I think Petro has made it abundantly clear how for works but I’ll throw in one more explanation for anyone who may still be unclear on the concept.

Think of for as a question with a YES/NO answer.

If you have this:

      from: 'on'
      to: 'off'
      for: '00:05:00' 

It means “Has it been off for, at least, 5 minutes?”

  • YES: continues executing the automation. If trigger then it is triggered, if condition then the condition is true.
  • NO: exits the automation. If trigger then it is not triggered, if condition then the condition is false.

If the answer is NO then it will not wait there until the answer is YES.

and to add one more bit of info to this:

if you really want it to wait for something to be true before continuing the actions then you can use a “wait_template:”

https://www.home-assistant.io/docs/scripts/#wait

That is cool - didnt think about the wait template. You can check for state “off” and last_updated 5 minutes ago… This is exactly what is needed and feels better than the 2 script solution.
will try this on the weekend!

Thanks a bunch!!! I hope I can give something back to the community soon!!!