Hey there, I’m in the process of switching from openHab to homeassistant. I want to configure the “Turn off kitchen light 10 minutes after last movement” example with an input_slider instead of a fixed 10min timeframe.
the automation rule looks like this:
# switch heating blanket off after X minutes
alias: heating blanket off after x minutes
trigger:
- platform: state
entity_id: switch.heating_blanket
to: 'on'
for:
minutes: '{{ states.input_slider.heating_blanket_safety_minutes.state }}'
#minutes: 90
action:
- service: switch.turn_off
entity_id:
- switch.heating_blanket
If I set the minutes manually (e.g. 90 minutes) it works. With the input_slider it does not.
(Yes, “input_slider.heating_blanket_safety_minutes” does exist)
Yeah - I can see that there could be issues with what @tillmannschatz has in the OP as an example. It’d be complicated to have a variable time comparison to the state. But there are lots of places where templates can be applied but aren’t yet.
There is other ways you can do the same thing though, @tillmannschatz… You could probably do a delay in your action instead of using for: in your trigger. Such as delay: '00:{{ states.input_slider.heating_blanket_safety_minutes.state | int }}:00'
before the light gets turned on. Although you will have to do a bit more templating to convert to hours and minutes if your slider goes above 59.
Or a safer route… call a script that does that delay + service, and kill the script before calling it again every time in case it is already running. Use this service: script.turn_off to cancel a script.
I know this is an old post. I ran into this problem recently and thought I’d share my solution for templating input_slider (or input_number in my case) values greater than 59 minutes in a delay statement.
delay: '{% if states.input_slider.heating_blanket_safety_minutes.state | int > 59 %}{{ states.input_slider.heating_blanket_safety_minutes.state | int // 60 }}{% else %}00{% endif %}:{% if states.input_slider.heating_blanket_safety_minutes.state | int > 59 %}{{states.input_slider.heating_blanket_safety_minutes.state | int % 60 }}{% else %}{{states.input_slider.heating_blanket_safety_minutes.state | int}}{% endif %}:00'