Template value in "for" (time since trigger)

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!

I believe that now() doesn’t result in the trigger being re-evaluated, you’d need to use something else that changes regularly (like a dummy call to {# states('sun.sun') #} in the template) to do that.

1 Like

Yes sorry that was somewhat unclear in my statement. With " datetime sensor" I actually meant the one available under the template as follows

{{ states.sensor.date__time }}

but I just checked it and it seems this one only updates every minute for me.

~Cheers

Welp there’s always Jellyfin I guess…

1 Like

I know that this is an old topic, but I have the same problem. I’m pulling my hair out because of the docs shos that this should work just fine :crazy_face:

Here is my entity:

input_number:
  oswietlenie_czas:
    mode: slider
    initial: 30
    min: 10
    max: 300
    step: 10

and my automation:

- alias: Turn off after x minutes
  trigger:
    platform: state
    entity_id: binary_sensor.ruch
    to: 'off'
    for:
      minutes: "{{ states('input_number.oswietlenie_czas')|int }}"
  action:
    service: homeassistant.turn_off
    entity_id: switch.oswietlenie_korytarz

I’m using the sample from the documentation: Automation Trigger - Home Assistant

but I get this error:

Invalid config for [automation]: expected int for dictionary value @ data[‘trigger’][0][‘for’][‘seconds’]. Got None. (See /config/automations.yaml, line 73). Please check the docs at Automation - Home Assistant

In docs it clearly shows that I can do this:

You can also use templates in the for option.

automation:
  trigger:
    platform: numeric_state
    entity_id:
      - sensor.temperature_1
      - sensor.temperature_2
    above: 80
    for:
      minutes: "{{ states('input_number.high_temp_min')|int }}"
      seconds: "{{ states('input_number.high_temp_sec')|int }}"
  action:
    service: persistent_notification.create
    data_template:
      message: >
        {{ trigger.to_state.name }} too high for {{ trigger.for }}!

The for template(s) will be evaluated when an entity changes as specified.

What might be wrong?