Automation with template sensor / not sure if it will ever trigger

I have a sensor being provided from one of my integrations that is a timestamp.

I have an automation that I would like to fire when the days between now and that time stamp drops below 5.

Here is my trigger code

 {% set timestamp = states('sensor.my_spa_rinse_filter_due') %}
          {% set timestamp_date = as_datetime(timestamp) %}
          {% set today = now() %}
          {% set days_diff = (timestamp_date - today).days  %}
         {% if days_diff < states('input_number.settings_spa_chore_due_days')|int %}
            true
          {% else %}
            false
          {% endif %}

Now, based on this. I don’t know if this will ever actually trigger, because the timestamp isn’t changing, and the calculation works, but the trigger would have to fire before it would actually run the calculation.

Not sure if I should move this to a condition and do a different trigger? or if maybe I should go my other route I was thinking, but decided to normalize it into just the automation, but I had thought of creating a template sensor that returns just the “days till rinse filter” that way it gets updated as the date changes. Then trigger off that. Just figured maybe i could skip that helper and do it in the trigger automation, but not sure what/if anything would trigger this.

Any suggestions? Thanks!

Hello Jason - Cranky Coder,

That template uses the state machine, so it is not a limited template, and cannot be resolved in a trigger. The information you are templating on does not exist until after the trigger (in other words).
If you want to use that logic in that automation you will have to trigger more generally:

Like trigger on a change to that sensor, then put your template in the condition to sort things out specifically.

Can you show us the template in situ? The description provided is a bit confusing.

If you’re just using it as a trigger, I would do something like:

triggers:
  - trigger: template
    value_template: |
      {% set days_delta = states('input_number.settings_spa_chore_due_days')|int(0) %} 
      {% set dt = states('sensor.my_spa_rinse_filter_due') | as_datetime | as_local %}
      {{ now() >= dt - timedelta(days = days_delta) }}

Downside though is that sensor doesn’t change until I rinse the filter. It’s a timestamp. But I get what you mean.

This is essentially the same as my current. It wouldn’t get triggered because the conditional statement isn’t checked until the the trigger has fired already and the calculation is done. So I think I am going to have to trigger this with a template sensor that is calculated and then trigger on that change, or add a trigger like “once an hour / once a day” and put my calculation as a condition.

A template that uses now() will be evaluated at the start of every minute.

1 Like

Oh nice!! I’ll give it a shot. That might be just what I need.

Don’t understand what you mean by this, as template triggers are not restricted to limited templates only…?