Template Automation - Correct Trigger?

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?

Any help appreciated.

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.

~Cheers

Thanks - good points. Ill test this first.

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.

Yes that would also work and actually be the smarter choice here I think :slight_smile:

~Cheers

All working with the time as the trigger, condition if cert expiry within 7 days and notification message depending on if it has expired or not.

Thanks

1 Like

Thanks for reporting back!

~Cheers

Hi Eximo84, do you mind to share your script? I’m stuck in this. Thanks in advance. ^^

The automation i have is this.

- alias: SSL Expiry Notification
  trigger:
    platform: time
    at: '09:00:00'
  condition:
    condition: numeric_state
    entity_id: sensor.ssl_cert_expiry
    below: 7
  action:
    service: notify.ios_my_iphone
    data_template:
      message: 'SSL cert due to expire in {{ states.sensor.ssl_cert_expiry.state }} days'

For me sensor i use

- platform: command_line
  name: SSL Cert Expiry
  command: "ssl-cert-check -c /etc/letsencrypt/live/mysite/cert.pem | tail -c 39"
  scan_interval: 86400
  icon: mdi:certificate
  unit_of_measurement: days

although it isnt perfect

Same problem as in this thread, my summary of the problem/solution here:

Thanks Eximo84, this give me a good start. ^^

Hi Eximo84, may I confirm you wrote your script in automation.yaml file?

Yes although my automations are split across multiple yaml files