Template value in "for" (time since trigger)

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.

I have looked at a number of posts here, like: Slider to set timer (minutes)

Here’s my best go so far :slight_smile:

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

I’ve also tried:

for: "00:{{states.input_slider.night_light_time.state | int}}:00"

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.

Please help me know what to do here, thanks!

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.

Thanks.
a) So, is there a way to do what I want (with a template trigger or otherwise)?
b) How do we find out that something is or is not possible? :slight_smile:

Yes to a)

To b) I say: good question

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.

2 Likes

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…

color values in lights can be templated.

Should be perfectly possible with service_template no?

~Cheers

1 Like

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.

If you don’t have a Plex Pass already you should totally ditch Plex for emby cough it’s opensource and stuff :stuck_out_tongue:

Getting OT again. Sorry :frowning:

~Cheers

2 Likes

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.

1 Like

Thanks for the reply.
This sounds like it’s not worth the effort :slight_smile:
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.

Template triggers were made for this kind of stuff.

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.

I gave up on trying to do this. I’d still be happy to see someone else’s solution.

It’s can be done like @ih8gates already said:

automation:
  - alias: Turn on light on motion
    trigger:
      - platform: YOUR TRIGGER
    action:
      - service: TURN ON LIGHT

  - alias: Turn off light after motion
    trigger:
      - platform: template
        value_template: "{% if (now() - states.script.motion_timer.attributes.last_triggered).seconds /60) | int > states.input_slider.YOURSLIDER.state and states.YOURLIGHT.state == 'on' %}true{% endif %}"
    action:
      - service: 

script:
  motion_timer:
    sequence:
      - delay:
          seconds: 1

Remember you only need this script if your motion sensor does not provide a last triggered attribute!

~Cheers

1 Like

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.

image

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

Actually i got it working!

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 %}"

And here is the results:

last updated (minutes): 1.75

slider hours: 1.0
hours converted to minutes: 60
slider minutes: 5.0

slider hours and minutes added together: 65.0

fan state: on

Automation part, check if the fan is on for more then the slider time:

(Automation result in false)

2 Likes

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?

Thanks.

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.

~Cheers

Ok thanks!