Template value in "for" (time since trigger)

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?

I’m not sure, but start with debugging.
In the developer tools, template page, what do you get for just your templates, e.g.

{{ states('input_number.high_temp_sec') | int }}

(This one is supposed to be an int but is currently None according to the error.)

I’ve tested the template in the Templates page.
I’ve entered:
{{ states('input_number.oswietlenie_czas')|int }}
and it returned 90 - this is the value I have on my slider, but when I put the same string in automation I get the same error:

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

I’m using Home Assistant 0.95.4 and I’m not sure if template parsing for for should work in this version or is this new in recent version.

I’m not sure if this is a bug or am I doing something wrong. I’m using values from entities in automation, but this is the first time I want to use it in for.

EDIT:
I’m searching on GItHub for issues related to for, nos ture if this is related:
Add support for `for` to binary_sensor, light and switch device conditions by emontnemery · Pull Request #27153 · home-assistant/core · GitHub, but the docs (Automation Trigger - Home Assistant) clearly shows that we can use a template in for :disappointed_relieved:

I think I found the reason:
this PR: Add template support to template trigger's for option by pnbruckner · Pull Request #24810 · home-assistant/core · GitHub add support for template in for andwas reelased in 0.96.0 but I’m using 0.95.4.

The docs should be more clear on that :confused:

EDIT 2: I’ve created PR to clarify that in docs: Add version requirements to for option in trigger by Misiu · Pull Request #10649 · home-assistant/home-assistant.io · GitHub

Ah, good that you found the issue.
I knew that for didn’t support templates in the past, but that had been added in more recent versions.
It’s helpful to add your HA version in support requests if it’s a bit ‘old’ like this one :slight_smile:

I personally wanted this (first post) but found a way around it since it wasn’t available. I have yet to update my automations to try the new way.

I’ve opened the docs first and I didn’t found any info about supported version so I thought that it will work just fine, but I was wrong :slight_smile: As I mentioned I’ve created PR that adds info about compatible version.

I’m planning to use many integrations so if something won’t work I’ll first check on latest version of HA and if I’ll have problems I’ll try to add info to docs for others.