The problem with using Jinja2 is that those templates are easy to make mistake in (HA doesnt do a proper validation of jinja2 code) and hard to debug, particularly if we’re not waiting for just one state, but for example 5-6 states.
Wait for trigger is very similar to the above, but it has one massive downside - it actually needs a trigger so the state needs to change at least one before HA even starts counting the time
This has existed for a long long time now. Available in yaml and UI.
- trigger: state
entity_id: binary_sensor.occupancy_downstairs
to: 'on'
for:
minutes: 5
and there’s a condition version too, which only checks the past instantaneously
- condition: state
entity_id: binary_sensor.occupancy_downstairs
state: 'on'
for:
minutes: 5
lastly, wait for trigger is retroactive. I.e. It will trigger if you set it to 5 minutes and 4 of the minutes already occurred when you start waiting for the trigger. So this statement is false:
To replicate your jinja above with wait_for_trigger (or wait_template), from the UI:
wait_for_trigger:
- trigger: state
entity_id: binary_sensor.occupancy_downstairs
to: 'on'
- trigger: state
entity_id: binary_sensor.occupancy_downstairs
to: ~
for:
minutes: 5
Ive been experimenting with wait_for_trigger but went back to using wait_template this morning, as wait_for_trigger wasnt working as you describe.
Lets say i want an automation which triggers at 5am and runs the vacuum if nobody is present downstairs (occupancy_downstairs is off), but if someone gets up and sits downstairs at 5am (occupancy_downstairs is on), it will pause and wait for everyone to go away before turning the vacuum on.
Trigger 5am is simple, i want the automation to run only once per day. But if i use wait_for_trigger, then the vacuum will never actually kick in at 5, because wait_for_trigger waits for the trigger to happen (binary_sensor.occupancy_downstairs to change state)