Using a variable for a timer in an automation

Hi folks,

I’m trying to convert one of my automations to be a little more flexible but I just don’t seem to be able to get my brain to wrap around what on the surface looks simple. I have an automation for a motion light. Turning on is fine, as is turning off after 20 seconds of no motion as defined below. What I’m trying to do is to instead use an input number in Lovelace to make the timer less static.

type: no_motion
platform: device
device_id: c4e873a598754e1da2e8f13cd7abfc95
entity_id: binary_sensor.tech_pir_1
domain: binary_sensor
for:
  hours: 0
  minutes: 0
  seconds: 20

I instead swapped out the seconds line for the one below, having checked the syntax is OK with the template view.

  seconds: {{ states('input_number.tech_cupboard_is_occupied') | int }}

I’ve wrapped the variable in single and double quotes, I’ve specified just the seconds value. I’ve tried just “for: {{ states(‘input_number.tech_cupboard_is_occupied’) | int }}” (with and without single and double quotes) but whatever I try the UI wont let me save this, or any combination I can think to try and I’m met with a Message malformed error.

I know I’m missing something really simple here but I just can’t seem to see what.

wrap the template in double quotes or use multiline notation

  seconds: "{{ states('input_number.tech_cupboard_is_occupied') | int }}"

or

  seconds: >
    {{ states('input_number.tech_cupboard_is_occupied') | int }}

Hi petro, thanks for the quick reply.

I’ve tried both options but have the same error when saving. To take the GUI out of the loop, I’ve manually edited automations.yaml and tried both options. Both options pass a GUI syntax check but when I restart the automations I get a notification for an “OrderedDict” error.

You can’t use the GUI to do templates. You have to edit it with yaml and keep it yaml.

Also note that the error message says that the field requires a float, but your template returns an int - don’t know if the new typed templates are that finicky :man_shrugging:

That’s a bit of a bind, but not the end of the world. I’m OK with editing the yaml files directly though, I’ve done that for long enough, but was working to move as much to the GUI as possible as more is available.

However, even updating automations.yaml with either of your two options, I can pass both the GUI config check as well as “ha core check” but as I restart the automations I’m still getting an error.

if I use

  seconds: >
    {{ states('input_number.tech_cupboard_is_occupied') | int }} 
invalid key: "OrderedDict([("states('input_number.tech_cupboard_is_occupied') | int", None)])" in "/config/automations.yaml", line 264, column 0

or, if I use…

  seconds: "{{ states('input_number.tech_cupboard_is_occupied') | int }}"
Invalid config for [automation]: expected float for dictionary value @ data['for']['seconds']. Got None. (See /config/configuration.yaml, line 73).

Sorry, I’m sure it’s something really simple but I just can’t see what I’m missing.

What version are you on?

I’m currently running 0.117.6

Can you please show the whole automation code you got currently?

I assume you left the device_id, domain etc. part as it was from the GUI editor and just adjusted it to add a template for the for: part. And I assume you can’t mix device triggers with templates.

Sure, no problem.

Yep, I’d created the initial automation using the GUI and that’s where my initial code section is from. I have been replacing the seconds section but below is the complete automation lifted from automations.yaml. I’ve just swapped back to the static seconds value to confirm that’s still working OK, and it is.

- id: '1604140034206'                           
  alias: '[State] Tech cupboard - Motion Off'                  
  description: ''                               
  trigger:                                       
  - type: no_motion                              
    platform: device                             
    device_id: c4e873a598754e1da2e8f13cd7abfc95  
    entity_id: binary_sensor.tech_pir_1       
    domain: binary_sensor                        
    for:                                                
      hours: 0                                                       
      minutes: 0                                                     
      seconds: 20                                                    
#      seconds: >                                                    
#        {{ states('input_number.tech_cupboard_is_occupied') | int }}
  condition: []                                                      
  action:                                                            
  - service: script.turn_on                                          
    data: {}                                                         
    entity_id: script.tech_cupboard_lights_turn_off                  
  mode: single 

So, then I assume it’s not working due to the mix of device trigger and templates. Try changing the trigger to this:

trigger:                                       
  - platform: state
    entity_id: binary_sensor.tech_pir_1       
    to: 'off'                    
    for:                                                
      hours: 0                                                       
      minutes: 0                                                                                                        
      seconds: >                                                    
        {{ states('input_number.tech_cupboard_is_occupied') | int }}
2 Likes

I think the device triggers don’t accept templates as they are exclusively designed to be used in the ui.

Try…

- id: '1604140034206'                           
  alias: '[State] Tech cupboard - Motion Off' 
  trigger:                                       
  - platform: state                          
    entity_id: binary_sensor.tech_pir_1       
    to: 'off' 
    for:                                                                      
      seconds: "{{ states('input_number.tech_cupboard_is_occupied') | int }}"                      
  action:                                                            
  - service: script.turn_on                                                                      
    entity_id: script.tech_cupboard_lights_turn_off                  
  mode: single

/ninja’d :laughing:

4 Likes

Sorry for the late reply but I can confirm you nailed it. It looks like the device trigger and template mix was to blame. Switching to the regular state trigger lets me plug in the template value for seconds, and it’s even happy to render it correctly in the GUI editor as well so that bodes well for many more places where I can already think to use the same format for variability.

Thanks again for all the pointers @Burningstone, @anon43302295 and @petro

sorry for the super late bump here, just out of curiosity were you able to ‘monitor’ the time countdown like you would with a timer? or does this method essentially create a background timer that can’t be tracked?