Hi.
I want to use the integer value of an input_slider as the amount of time in a trigger for how long it’s been since a motion sensor triggered. So I have a configurable delay before the action (which turns the lights off). I also want to learn how these templates work in this case!
Is this possible?
I’ve tried lots of versions but always get errors like: [automation]: expected int for dictionary value @ data['trigger'][0]['for']['minutes] Got None.
- alias: Night lights off after delay (Living)
trigger:
- platform: state
entity_id: binary_sensor.motion_living
to: 'off'
for:
minutes: "{{ states.input_slider.night_light_time.state | int }}"
action:
...
When I put {{ states.input_slider.night_light_time.state | int }}" into the front end dev tools template tester, I do see the current value of my slider.
and got offset 00:{{states.input_slider.night_light_time.state | int}}:00 should be format 'HH:MM' or 'HH:MM:SS' for dictionary value @ data['trigger'][0]['for']. Got None.
I think that the only way you can use a template in a trigger is by using a template trigger. I don’t believe that the “minutes” value in “for” is template-able.
I’ve only been able to figure this out by trial and error. I haven’t seen a place where all template-able fields are listed. I’m not even sure what the best method would be to convey this info.
I’ve only been able to figure this out by trial and error. I haven’t seen a place where all template-able fields are listed. I’m not even sure what the best method would be to convey this info.
Agreed. I’m still trying to find a definitive answer on whether color values in lights can be templated. Everything I have tried or researched says no even though in threads other users insist they can do it - but none of those threads ever yields a solution.
I’m inclined to believe the only way to do it is with AppDaemon by constructing the values externally and sending a “compiled” JSON string to the API…
I’m not sure if that’s been tried; I know I haven’t tried it as I only learned about service templates this week - pretty sure it was from you! All I wanted to do was to set up a random color on my landscaping lights when they fired each evening and I ended up going down the rabbit hole.
I suppose I can take a look at this, but I’m kind of in the middle of a number of beta projects for future release stuff here (Ring Doorbell support, a replacement for the Plex media player and the new version of HA Dashboard among others) so unfortunately I won’t be able to look at it for a bit.
I’d love to hear how to do this.
I realised after going to bed late again last night that the payoff is pretty low for this particular “slider → light delay time” property, but I do want to learn for the future.
Create a template trigger that compares the current time to the last_tripped state of your motion. If your motion doesn’t have one, you might need to cheat and create a script that simply acts as a marker - it definitely has a last_triggered. Check the alarm clock thread for techniques. Or my recent Google calendar example - it does some time math with timestamps.
Thanks for the reply.
This sounds like it’s not worth the effort
What I mean is… if Home Assistant can’t do this simply, it should be made so. I’m happy to contribute and have posted about this in the dev category.
I think it would be good to make a collection of desired automations that should be much easier to achieve, so we can start making them possible.
Hi all,
Sorry for the up of this relatively old topic, but I want to do exactly the same thing. Has this issue been solved by any means ?
Basically I have motion sensors that turn_on lights and I want these lights to be turn_off after X seconds, X being defined using an input slider. Same goes for a second input slider that will change the delay in a script.
Soo… I want to automaticly turn off and on my sonoff switch powered good old standing fan.
The fan is on all night and it would be preferable to be turned on for only a couple of minutes every hour.
Current sliders means, it will turn off after 30minutes.
- alias: 'Bedroom Fan -Timer'
hide_entity: true
trigger:
platform: state
entity_id: switch.bedroomfan_timer
to: "on"
condition:
- condition: state
entity_id: switch.sonoff__room_fan
state: 'on'
for:
hours: "{{ states.input_slider.bedroomfan_timer_hours.state }}"
minutes:
value_template: "{{ states.input_slider.bedroomfan_timer_minutes.state }}"
action:
- service: switch.turn_off
entity_id: switch.sonoff__room_fan
Got stuck at putting that 30minutes into the:
for:
hours:
minutes:
Is this the same problem/wall you guys faced in this topics?
This is the template editor sketch if anybody wants to see step by step or debug your own stuff.
switch last updated: {{((as_timestamp(now())-as_timestamp(states.switch.sonoff__room_fan.last_updated)) | int /60)}}
slider hours: {{states.input_slider.bedroomfan_timer_hours.state}}
hours converted to minutes: {{states.input_slider.bedroomfan_timer_hours.state |int * 60}}
slider minutes: {{states.input_slider.bedroomfan_timer_minutes.state}}
slider hours and minutes added together: {{states.input_slider.bedroomfan_timer_minutes.state | int + states.input_slider.bedroomfan_timer_hours.state |int * 60 }}
fan state: {{states.switch.sonoff__room_fan.state}}
Automation part, check if the fan is on for more then the slider time:
trigger:
- platform: template
value_template: "{% if((as_timestamp(now())-as_timestamp(states.switch.sonoff__room_fan.last_updated)) | int /60) > states.input_slider.bedroomfan_timer_minutes.state | int + states.input_slider.bedroomfan_timer_hours.state |int * 60 | int and states.switch.sonoff__room_fan.state == 'on' -%}true{% endif %}"
Hi @PhyberApex, how does the trigger based on platform template work when we’re testing time. Does home assistant check this trigger every second to check the validity of the expression in the value_template?
Honestly I am not sure right now as the documenation states “Template triggers work by evaluating a template on every state change for all of the recognized entities.” but it does not say what those recognized entities actually are sorry. My guess would be that renders that template every second if you use the datetime sensor.