Automation Triggered by template

Hello folks,

I’am writing an automation that take as trigger type a template.

I’ve tested the template code using the development tool, and from there looks like everything is ok.
The template is returning TRUE or FALSE as boolean.

Here is the template:

value_template: >-
  {% set movieTimeAdded =
  ((state_attr("sensor.recently_added_movies","data")[1].airdate|as_timestamp) -
  (60*60)) | timestamp_custom("%H:%M") %}

  {% set movieReleaseAge = ( (now()| as_timestamp) -
  (state_attr("sensor.recently_added_movies","data")[1].airdate|as_timestamp)  -
  (60*60) ) | timestamp_custom("%M")|int %}

  {% set timeNotification = states('input_number.plex_minutes_notification')|int
  %}

  {{ 'true' if movieReleaseAge <= timeNotification else 'false' }}

I’m a new user of Home Assistant and I’m reading the documentation but I cant solve this issue.

Beside the fact that the template return TRUE or FALSE looks like its not triggering the automation.

remove the quotes around true and false in the last line.

You don’t need to explicitly return true/false. Let the template’s evaluation do that for you. The following template will return true if movieReleaseAge is less than or equal to timeNotification, otherwise it will return false.

{{ movieReleaseAge <= timeNotification }}
2 Likes

I’ve tried with bot solution but looks like it is no triggering the automation.

in the development tool I got the update but in the realiti it didn’t trigger.

Paste a screenshot of what you see in the Template Editor.

On the right hand side of the Template Editor, it shows all the entities that will be used to update the template. If I’m not mistaken, this template will be updated whenever the following entities change state:

sensor.recently_added_movies
input_number.plex_minutes_notification

and, if you are using version 0.117, every minute because the template also includes now(). It won’t update more frequently than that.

Here the screenshot of DevTool

Here is what is reported under the result, was cutted from the screenshot, sorry

This template updates at the start of each minute.

This template listens for the following state changed events:

* **Entity** : input_number.plex_minutes_notification
* **Entity** : sensor.recently_added_movies

Home Assistant is updated to the last version: Home Assistant 0.117.1

Are you using version 0.115 or higher? If you are, then your screenshot isn’t showing the information I wanted to see which is underneath the right hand pane of the Template Editor. It reports which entities it listens to. Here’s an example of what I’m looking for:

Screenshot from 2020-11-01 09-11-27

It indicates the template will be updated whenever any weather entity changes state and when sun.sun changes state.

Ive updated with all the information you needed

Perfect, thank you. As expected, it listens to the two entities used in the template. Although it doesn’t say it, it also uses now() to update the template every minute.

From 0.117’s release notes:

Templates - Auto-updating now()
The template will automatically be updated when:

  • A referenced entity changes state.
  • At the start of each minute when now() or utcnow() is present in the template.

Ok, So if I understood correctly, when now() or utcnow() is used in the script, it doesn’t matter when a state is changing because it is overridden by now() or utcnow().
This is probably to avoid to trigger every change of “time”.

So it is not a good practice to use it when scripting. I was reading the documentation and this is the first script/template I’m writing and I missed that part.

I will look for a better validation to avoid it. Probably the script/template it is not working because triggering every minute there are times where the result it is already FALSE.

Thank you for the clarification.

I’m not sure if I’m misreading what you wrote or if you misunderstood how it works.

Your template will be evaluated every minute (because it includes now) and whenever either of the two entities input_number.plex_minutes_notification and sensor.recently_added_movies change state.

So if neither of the two entities ever change state, you can be assured that the template will be updated at least every minute because it uses now().

If you do not want it to be updated every minute, then you must remove now() and use something else to perform the time calculation.