Template time trigger

Trying to setup an automation based on time using templates. No clue to why this isn’t working:

- alias: 'Alarm (workdays)'
  trigger:
    platform: template
    value_template: '{{ now().strftime("%H:%M") == states.sensor.alarm.state }}'
  condition:
    - condition: time
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
  action:
    - service: switch.turn_on
      entity_id:
        - switch.alarm

states.sensor.alarm.state reports 05:00
now().strftime("%H:%M") is padded with zero, checked that.
No clue why this doesn’t trigger. What am I missing here? :thinking:

If you paste your template in the template dev panel what does it return? It needs to return a “True” for the trigger to fire. If you are getting a true/false value it should be working correctly. Can you manually trip your alarm to watch what happens?

Hmm… it returns true right now, only difference is that it normally says 05:00 vs now 16:25.
Very strange. It should be triggered. Going to test some more. Will report back.

Nope, can’t get it to trigger. Template value says true, but the automation says never triggered. Something’s off :confused:

Hmmm, should trigger.

Anything in the log?

For giggles try changing your ‘quotes’ to “quotes” around your value template.

Do you need a dash before platform?

On a PI I do the following…

value_template: ‘{{ now().time().strftime("%R") == “17:00” }}’

The %R works well on the PI.

Shouldn’t it be:

  condition:
    condition: time
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri

Anything in the log?

Nope, nothing.

For giggles try changing your ‘quotes’ to “quotes” around your value template.

Tried, no difference

Do you need a dash before platform?

Not doing that anywhere in my config like that.

value_template: ‘{{ now().time().strftime(“%R”) == “17:00” }}’

Just tried that, same result as %H:%M, just parses true or false, but doesn’t trigger

@treno
Also tried that, I noticed it in the docs, no trigger. Damn.

Ok, this has me bugged, it should be working. Last two things to try. Try moving the entity ID up in the action …

- alias: 'Alarm (workdays)'
  trigger:
    platform: template
    value_template: '{{ now().strftime("%H:%M") == states.sensor.alarm.state }}'
  condition:
    - condition: time
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
  action:
    - service: switch.turn_on
      entity_id: switch.alarm

And final hail mary attempt, either remove or comment out the conditions. They shouldn’t be an issue but lets make it as simple as possible and then if that works we know its the condition causing the issue.

Hi,
I’ve tried to use quie similar automation and it never worked.
I came to a conclusion that the function now ().strftime() will not be dynamically updated when it’s in a trigger for some unknown reason. I use sensor.time instead now.

2 Likes

Thanks. This is exactly what I started to suspect to. Changed to use sensor.time and it now works flawlessly. Problem solved :thumbsup:

1 Like