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.
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’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.
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
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.
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.
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.
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.
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?