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.
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.
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.