Help with automation with wait_template

Hello guys!

I cannot figure out what is going on here… This is an automation for turning lights on and off depending on a motion sensor, actions and time:

  - alias: Luzes da casa - Quarto
    trigger:
      platform: state
      entity_id: binary_sensor.movimento_quarto
      to: 'on'
    condition:
      condition: state
      entity_id: light.luzes_principais_quarto
      state: 'off'
    action:
      - service: light.turn_on
        entity_id: light.luzes_principais_quarto
      - delay: 00:00:02
      - wait_template: "{{ (is_state('light.luzes_principais_quarto' , 'off')) or (((now() - states.binary_sensor.movimento_quarto.last_updated).seconds /60) | int >= states.input_number.timmer_quarto.state |int and is_state('binary_sensor.movimento_quarto' , 'off')) }}" 
      - condition: state
        entity_id: light.luzes_principais_quarto
        state: 'on'
      - service: light.turn_off
        entity_id: light.luzes_principais_quarto

I idea is if lights are turned off manually, just finish automation until next trigger or wait for an input_number number of minutes and for the sensor to be off to turn lights off.

The automation triggers, lights turns on but nothing else happens. I get true when tested with template testing on dev tools, very odd.

Is it possible to have multiple wait conditions on the same line?

Thanks a lot!

the cause is most likely the now() in your template. Templates do not update if your time changes when using now(). Templates only update when entities inside the equation update. So if nothing in the equation is updating… then it will never continue. If you use the datetime sensor instead of now(), it may rectify the problem.

1 Like

here’s the datetime sensor:

1 Like

Amazing stuff sir! Thanks a lot! I will try it tomorrow morning an let you know the result. Cheers!

1 Like

Hello @petro! So sorry to bother you again my friend…

I’ve managed to set up the Time and Date component successfully but could not figure how to calculate the minutes between those two times.

Could you please help me out again? :slight_smile:

With the last updated state from the sensor I get this:

{{ states.binary_sensor.movimento_quarto.last_updated }} = 2018-08-06 16:13:36.411953+00:00

{{ states.binary_sensor.movimento_quarto.last_updated.strftime("%H:%M") }} = 16:13

{{ states.sensor.time_utc.state }} = 16:38

Could not make a calculation between them to get just the minutes…

Thanks a lot!

Ah ok. Well, you probably have to convert everything to seconds, then convert the seconds to minutes. Also, you need to use date_time, not utc. the utc sensor doesn’t give the date, and that means your automations may not work during the hours in which utc’s date and local’s date don’t match. The good news is that as_timestamp will convert it to utc for you.

{{ as_timestamp(states.binary_sensor.movimento_quarto.last_updated) }} 
{{  as_timestamp(strptime(states.sensor.date_time.state, '%Y-%m-%d, %H:%M')) }}

so, subtracting and turning into minutes…

{{ (as_timestamp(strptime(states.sensor.date_time.state, '%Y-%m-%d, %H:%M')) - as_timestamp(states.binary_sensor.movimento_quarto.last_updated) ) / 60 }}
2 Likes

Amazing job @petro , again, my friend!

You are like the sentinel of tricky automation here on the forum hahaha

It worked like a charm! Here is the final wait_template:

- wait_template: "{{ ((is_state('light.luzes_principais_quarto' , 'off')) or ( (((as_timestamp(strptime(states.sensor.date__time.state, '%Y-%m-%d, %H:%M')) - as_timestamp(states.binary_sensor.movimento_quarto.last_updated) ) / 60) | int) >= states.input_number.timmer_quarto.state | int and is_state('binary_sensor.movimento_quarto' , 'off'))) }}"

Will try it tonight to see if lights goes off automatically with this automation.

Thanks a lot mate! Cheers!

2 Likes

Hopefully it works! I’ve seen wait templates act weird before.

1 Like

Hello again!

Just tested the automation at home and can say that it works perfectly!

Here it is:

  - alias: Luzes da casa - Quarto
    trigger:
      platform: state
      entity_id: binary_sensor.movimento_quarto
      to: 'on'
    condition:
      condition: state
      entity_id: light.luzes_principais_quarto
      state: 'off'
    action:
      - service: light.turn_on
        entity_id: light.luzes_principais_quarto
      - delay: 00:00:05
      - wait_template: "{{ ((is_state('light.luzes_principais_quarto' , 'off')) or ((((as_timestamp(strptime(states.sensor.date__time.state, '%Y-%m-%d, %H:%M')) - as_timestamp(states.binary_sensor.movimento_quarto.last_updated) ) / 60) | int) >= states.input_number.timmer_quarto.state | int and is_state('binary_sensor.movimento_quarto' , 'off'))) }}"
      - condition: state
        entity_id: light.luzes_principais_quarto
        state: 'on'
      - service: light.turn_off
        entity_id: light.luzes_principais_quarto

Oddly, I had to put a delay: 00:00:05 just before the wait_template, if not, the automation hags just like before.

I’ve created a few input_number sliders so I can set the minutes I want the lights to go off:

image

Now I just need to discover how to set these sliders to be only these number available “1,2,3,4,5,10,20,30”, so it could be a bit smaller a easier to set.

Thanks a lot again mate! Have a great one!

not sure that is possible. Have you thought about input_datetimes?

Much easier to input a time.

1 Like

Never knew about this componente, very neat! This is very useful for creating an alarm clock system inside HA. I’ve never found a simple solution to do it. Very nice! Will take a look. Thanks!

1 Like

Hi Schneider,
I’m interested how do you create that timers’ list and how do you insert the timer into an automation.

I’m not strong with code so, please, could you explain me step by step?

My goal is to have a timer slide to set a delay in a automation’s action

Thank you in advance.

You can use a input_number as slider. And use it in the autoamtion as delay like it is descriped here https://www.home-assistant.io/docs/scripts/#delay

thank you for the answer
If I try to put this in the lovelace.yaml, the slider is not visible:

type:
input_number:
  minutes:
    name: test
    initial: 10
    min: 2
    max: 20
    step: 1
    unit_of_measurement: minutes

The preferred way to configure an input number is via the user interface at ConfigurationHelpers . Click the add button and then choose the Number option.

More Information to input_numbers: Input Number - Home Assistant

After adding the input_number add this to your lovelace.ymal:

type: entities
entities:
  - input_number.slidertest

But replace input_number.slidertest with the name you gave your input number.
More Information to the lovelace card are here. Entities Card - Home Assistant

In the automation the delay can be done by adding this.

delay: '{{states("input_number.slidertest")}}'

Hope it works

1 Like

FYI

It’s necessary to add hours and minuts on the string like this:

- delay: '00:{{states("input_number.movimento_marco") | int }}:00'

Thank you, thank you so much.

1 Like