Having trouble with Template Trigger

I’m trying to set up an action that triggers 15 minutes (900 seconds) before the end of a Google Calendar entry. It’s on a calendar that I don’t have the ability to change entries on, so I’m not able to use the offset_reached attribute.

I can watch my value_template in the template dev tool and see it become ‘true’ at the correct time. But the action never occurs. This is my first template trigger - am I doing something wrong?

Do I need an entity ID somewhere in that trigger so that the expression gets evaluated periodically?

- alias: Tech Shop End
  trigger: 
    - platform: template
      value_template: '{% if states.calendar.kids_school.attributes.message == "Tech shop" and as_timestamp(states.calendar.kids_school.attributes.end_time) - as_timestamp(now()) < 900  %}true{% endif %}'
  action: 
    - service: notify.scott_notifier
      data: 
        message: "Tech Shop ends in fifteen minutes"
        title: "Time to go!"

I also tried a value_template that would evaluate True (without the if statement)

{{ states.calendar.kids_school.attributes.message == “Tech shop” and as_timestamp(states.calendar.kids_school.attributes.end_time) - as_timestamp(now()) < 900 }}

I’m gonna bump this one last time to see if anyone has any ideas.

I ended up implementing this by creating a sensor with the EXACT same value_template and I watch for that sensor to become true.

I’m mostly curious to see if I just misunderstand something fundamental about how template triggers work.

Hi, did you ever solve this? Trying something similar.

Nope. Like I said - I did a workaround, but I’d like to understand what went wrong.

Don’t know why, but for me a template trigger never work as I want.
You can try to use the ‘time’ trigger which runs an automation every minute, for example.
And check your template in condition section.

I’m quite glad I came upon this thread! I have been trying (unsucessfully!) for more than a few hours to get this working with a template sensor. ih8gates - I had the same thing where the template sensor evaluated in the dev tool but when I put it in an automation/sensor it would not update automatically.

In the end it was omeasire’s idea that worked the trick. Check the template value every minute to see if it’s true or not! My code below for turning on my hot water heater the day before an airbnb booking begins (calendar is automatically generated/imported from airbnb so modifying that was out of the question):

- alias: Hot Water On day before
    hide_entity: False
    trigger:
      platform: time
      minutes: '/1'
      seconds: 00
    condition:
      condition: and
      conditions:
        - condition: template
          value_template: '{{ as_timestamp(now()) | int > (as_timestamp(states.calendar.airbnb.attributes.start_time)-79220) | int }}' 
        - condition: template
          value_template: '{{ as_timestamp(now()) | int < (as_timestamp(states.calendar.airbnb.attributes.start_time)-79180) | int }}' 
    action:
      service: notify.notify
      data:
        message: "Hot Water On day before"   

So for this example it checks if the time is greater than 11:59:40 and less than 12:00:20, giving a buffer for the time trigger on the minute to make sure both are satisfied. For the sake of the example, the notification will turn into a MQTT trigger to trigger a relay.

I’m not sure how ‘computationally intensive’ checking every minute is, but the same idea could work with checking every 10 minutes.

Thanks for your help ih8gates and omeasire!

2 Likes