Automation Does not Trigger on Entity State Change

Running Home Assistant OS 5.10, core-2021.1.3 I have a set of three automations configured:

  1. When Garage sensors trip, turn on the light and update the last time lights were automated in the Garage.
  2. When the lights in the Garage are manually flipped, set the last time lights were automated in the Garage to now().max.
  3. When (1) has been triggered more than 10 minutes ago, turn off the Garage lights.

(1) and (2) trigger properly. However, (3) seems never to trigger. Here is the automation config for (3):

trigger:
  - platform: template
    value_template: >-
      states('sensor.automation_timestamp_trigger') | float -
      state_attr('input_datetime.last_automated_group_garage_lights',
      'timestamp') > states('input_number.automation_on_limit_in_seconds') |
      float
condition:
  - condition: state
    entity_id: group.garage_lights
    state: 'on'
action:
  - service: homeassistant.turn_off
    data: {}
    entity_id: group.garage_lights

Using Developer Tools Template I can see the value of the trigger switch from false to true at the 10 minute mark (± 60 seconds as the sensor.automation_timestamp_trigger only updates once every 60 seconds (by design)):

{{ states('sensor.automation_timestamp_trigger') | float }} - {{ state_attr('input_datetime.last_automated_group_garage_lights', 'timestamp') }} = {{ states('sensor.automation_timestamp_trigger') | float - state_attr('input_datetime.last_automated_group_garage_lights', 'timestamp') }} > {{ states('input_number.automation_on_limit_in_seconds') }}

image

To explain the trigger entities:
sensor.automation_timestamp_trigger = now() ± 60 seconds
input_datetime.last_automated_group_garage_lights = The last time the garage lights were automated to on
input_number.automation_on_limit_in_seconds = The timeout (600 seconds)

Having said all of the above, the automation simply does not trigger. Any assistance in troubleshooting this would be much appreciated.

(ideally I would rather use now() instead of sensor.automation_timestamp_trigger, that would mean an entity state change is not causing the trigger, so I am using a command_line sensor that reads date +"%s" every 60 seconds)

At a minimum you need to wrap the template in brackets:

trigger:
  - platform: template
    value_template: >-
      {{ states('sensor.automation_timestamp_trigger') | float -
      state_attr('input_datetime.last_automated_group_garage_lights',
      'timestamp') > states('input_number.automation_on_limit_in_seconds') |
      float }}

Other than that I haven’t verified any logic within the template. You might need to add parenthesis around the two subtracted elements tho.

But start there.

Thanks, not sure how I missed that. Apologies for the basic q.