I have a sensor which grabs the enddate of my SSL cert. The output for this is something like this: 2017-06-08 05:29:00
I want an automation to notify me 7 days before the date passes, i want the output to be different based on if the cert still has life or has expired. I am casting the value into UNIX time and performing some calculations to get the 7 days before todays date.
- alias: SSL Expiry Notification
trigger:
platform: template
value_template: "{% if as_timestamp(states.sensor.ssl_cert_expiry.state) < as_timestamp(now())-604800 %}true{% endif %}"
action:
service: notify.ios_my_iphone
data_template:
message: >
{% if as_timestamp(states.sensor.ssl_cert_expiry.state) < as_timestamp(now()) %}
SSL certificate expired on {{ states.sensor.ssl_cert_expiry.state }}
{% else %}
SSL is due to expire on {{ states.sensor.ssl_cert_expiry.state }}
{% endif %}
–
As a test i have changed the trigger to be greater than the now date - 7 days, If i use the template editor in the front end with the trigger as listed above it shows as true
{% if as_timestamp(states.sensor.ssl_cert_expiry.state) > as_timestamp(now())-604800 %}true{% endif %}
However my notification isnt coming through.
Is my syntax wrong?
Can i use data_template and then filter the response based on a template like this?
First step is to determine where the problem actually lies. Is your notification working at all? To check this just change the trigger to something that is already working (e.g. from another automation). If you get a notification you know the error is in the template. If you don’t there is an error in your action and MAYBE also in your trigger. But get the notification working first with a known working trigger.
On a side note: Your action gets called ANY TIME ANY STATE is changed in your HA. From the docs:
Template triggers work by evaluating a [template] on each state change.
You should really implement a condition to this if you do not want to be flooded with notifications.
In regards the condition, i might be actually better having this automation trigger once a day at a set time and have the condition on if the date is less than the now date - 7 days.