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?
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.
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.
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:
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.
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!
The preferred way to configure an input number is via the user interface at Configuration → Helpers . Click the add button and then choose the Number option.
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.