Automation with trigger template won't run

Here’s a probably simple one.
The automation below will never run.

I have checked the Jinja in the template console and it does return true or false when it shall. Still the automation never executes.

This automation shall execute

  • at 16:00:00 if sunset does occur before 16:00:00
    or

  • at sunset if sunset does occur after 16:00:00

    - alias: "Some alias text"
      trigger:
        - platform: template
          value_template: >-
            {{ (now().strftime('%T') <= (states.sensor.sunset_time.state ~ ':00' ) 
            and now().strftime('%T') == "16:00:00" )
            or
            (now().strftime('%T') == (states.sensor.sunset_time.state ~ ':00' ) 
            and now().strftime('%T') >= "16:00:00") }}
      action:
         some code here
    

According to the documentation, I shall be able to use a template trigger as in my code above.

You can’t use now() in a trigger template because now() is only evaluated ‘at this moment’, use the time sensor.

1 Like

Just for the cause of documentation and for future readers, here’s how I solved this automation:

  - alias: "Some alias text"
    trigger:
      - platform: time
        at: '16:00:00'
      - platform: template
        value_template: '{{ states.sensor.sunset_time.state ~ ':00'  }}'
    condition:
      - condition: template
        value_template: >-
          {{  (now().strftime('%T') <= (states.sensor.sunset_time.state ~ ':00' ) 
              and now().strftime('%T') == "16:00:00" )
            or
            ( now().strftime('%T') == (states.sensor.sunset_time.state ~ ':00' ) 
              and now().strftime('%T') >= "16:00:00")  }}
    action:
     some code here