Template automation trigger, time based

Hello, I’m trying to figure out why this automation isn’t triggering, I found this post, and tried implementing the suggestion of adding {% set x = states('sensor.time') %} to my trigger template, but no joy - the automation doesn’t trigger:

- alias: person_extended_away
  trigger:
    - platform: template
      value_template: >-
        {% set x = states("sensor.time") %}
        {% set now_datetime = (now()).strftime ("%Y-%m-%d %H:%M:%S") %}
        {% set away_since_datetime = states("input_datetime.person1_away_since") %}
        {{ away_since_datetime < now_datetime and
           ((now_datetime | as_datetime) - (away_since_datetime | as_datetime)) > timedelta(hours = 24) }}
      id: person1
    - platform: template
      value_template: >-
        {% set x = states("sensor.time") %}
        {% set now_datetime = (now()).strftime ("%Y-%m-%d %H:%M:%S") %}
        {% set away_since_datetime = states("input_datetime.person2_away_since") %}
        {{ away_since_datetime < now_datetime and
           ((now_datetime | as_datetime) - (away_since_datetime | as_datetime)) > timedelta(hours = 24) }}
      id: person2
  mode: parallel
  condition:
    - '{{ is_state("input_boolean." + trigger.id + "_currently_away", "on") }}'
    - '{{ is_state("input_boolean." + trigger.id + "_extended_away", "off") }}'
  action:
    - service: input_select.select_option
      data:
        entity_id: 'input_select.{{ trigger.id }}_status_dropdown'
        option: Extended away
    - service: input_boolean.turn_on
      data:
        entity_id: 'input_boolean.{{ trigger.id }}_extended_away'

When I check the template in Developer Tools, it’s showing as true:

{% set x = states("sensor.time") %}
{% set now_datetime = (now()).strftime ("%Y-%m-%d %H:%M:%S") %}
{% set away_since_datetime = states("input_datetime.person1_away_since") %}
{{ away_since_datetime < now_datetime and
   ((now_datetime | as_datetime) - (away_since_datetime | as_datetime)) > timedelta(hours = 24) }}

Thanks for any assistance!

Changing from false to true will trigger it. It won’t trigger if it is true already (or always true, I did not check your logic).

Thanks, I’ve tried making the template false then true, but the automation doesn’t trigger.

I thought that having {% set x = states('sensor.time') %} in the template means there’s a state change every minute, so the automation should trigger… that’s not currently happening obviously and/or I’ve misunderstood.

You are comparing strings here, not times.

And “100” is less than “2” when using strings.

Try using datetime objects or timestamps instead.

Thanks re: comparing strings I updated the line you pulled out to:

as_datetime(away_since_datetime) < as_datetime(now_datetime)

Is that correct now?

I ended up changing direction to get this working:

# Mark a person as extended away
- alias: person_extended_away
  trigger:
    - platform: time_pattern
      hours: '*'
      minutes: '/10'
  action:
    - variables:
        peops_list: '{{ ["person1", "person2"] | list }}'
    - repeat:
        for_each: '{{ peops_list }}'
        sequence:
          - condition: >-
              {{ is_state("person." + repeat.item, "not_home") and
                 has_value("input_text." + repeat.item + "_away_since") and
                 is_state("input_boolean." + repeat.item + "_extended_away", "off") }}
          - condition: >-
              {% set now_datetime = (now()).strftime ("%Y-%m-%d %H:%M:%S") %}
              {% set away_since_datetime = states("input_text." + repeat.item + "_away_since") %}
              {{ as_datetime(away_since_datetime) < as_datetime(now_datetime) and
                 (as_datetime(now_datetime) - as_datetime(away_since_datetime)) > timedelta(hours = 24) }}
          - service: input_boolean.turn_on
            data:
              entity_id: 'input_boolean.{{ repeat.item }}_extended_away'
          - service: input_select.select_option
            data:
              entity_id: 'input_select.{{ repeat.item }}_status_dropdown'
              option: Extended away

I made that post three years ago. Its suggestion is no longer applicable today due to changes in how templates are evaluated (made over a year ago). Specifically, the inclusion of now() in a template will cause it to be evaluated at least once a minute (it didn’t do that three years ago).

The problem with your Template Trigger is what tom_l has already identified; it is incorrectly comparing datetime strings.

Thanks 123 and tom_l, re: incorrectly comparing datetime strings, I updated below - is this now correct? Or am I still missing something?

{% set away_since_datetime = states("input_text." ~ repeat.item ~ "_away_since") | as_datetime %}
{{ away_since_datetime < now() and
  (now() - away_since_datetime) > timedelta(hours = 24) }}

Thank you :grinning: