Sensor to show expiry date of SSL certificate

@anon43302295: thx a lot, this is working!
(it seems I need to work more on RTFM :slight_smile: ).

1 Like

Any information about it?

SO SO sO Simplay!!

im with this so long!! TX men.

image

1 Like

Anyone getting this error at all since 0.108?

image

Hi! In 2020.12.0 HA new version, the cert_expiry sensor is deprecated. We only have the cert_expiry_timestamp sensor available.

I have changed my automation for renewing the SSL certificate as follow:

- id: '1607213186417'
  mode: single
  trigger:
  - platform: template
    value_template: "{{ ((as_timestamp(states('sensor.cert_expiry_timestamp')) - now().timestamp()) / 86400) | int < 25 }}"
  condition: []
  action:
  - service: hassio.addon_start
    data:
      addon: core_letsencrypt

What are you thinking about?

Thanks!

2 Likes

This template (above) uses the ‘workaround’ that forces an update on a template with no entities changing (based on now() ) This is a workaround as I said and should (in my opinion anyway) be avoided.
I have a sensor set up which follows official templating rules and only updates 1/day rather than yours which will update 1440/day (1/min) so is more efficient too
You may need to adjust the entity_id (depending on how you added the integration and the naming of your HA Instance or IP address etc.)
This is a sensor which can either be displayed or used in automations like yours (note: putting all that effort into an automation means it’s likely to be repeated again elsewhere) : -

  - platform: template
    sensors:
      s_cert_exiry_d:
        friendly_name: 'Duck DNS SSL Cert Expiry'
        value_template: >
          {{ ((as_timestamp(states('sensor.cert_expiry_timestamp_home_assistant_io')) - as_timestamp(states.sensor.date.last_updated)) / 86400) | int }}
        icon_template: >-
          {% if states('sensor.s_cert_exiry_d') | int > 0 %}
            mdi:certificate
          {% else %}
            mdi:certificate-outline
          {% endif %}
        unit_of_measurement: 'days'

As of HA version 2020.12

3 Likes

I used your code and changed the certificate expiry timestamp sensor for mine and left the rest of the code the same.

I rebooted and the new sensor shows as unavailable.

When I put the template into the developer tools, I get this error - UndefinedError: ‘None’ has no attribute ‘last_updated’

When I look at the state of the new expiry sensor it shows both last changed and last updated so not really sure what I am doing wrong?

This is the code I used

- platform: template
    sensors:
      ssl_cert_expiry:
        friendly_name: 'Duck DNS SSL Cert Expiry for hass-pj'
        value_template: >
          {{ ((as_timestamp(states('sensor.cert_expiry_timestamp_my_sslduckdns_org')) - as_timestamp(states.sensor.date.last_updated)) / 86400) | int }}
        icon_template: >-
          {% if states('sensor.cert_expiry_timestamp_my_ssl_duckdns_org') | int > 0 %}
            mdi:certificate
          {% else %}
            mdi:certificate-outline
          {% endif %}
        unit_of_measurement: 'days' 

Okay, two questions : -

  1. Do you have your time and date sensors set up (post your config for that) ?
  2. What do you get when you print your sensor state (as you defined it above) into the template editor ?

I have these sensors set up:

  - platform: template
    sensors:
      day_of_week:
        value_template: >
          {{ now().strftime("%a") }}
        entity_id:
          - sensor.day_of_week

  - platform: template
    sensors:
      day_date:
        value_template: >
          {{ now().strftime("%A, %b %d, %Y") }}
        entity_id:
          - sensor.day_date

When I print the SSL cert sensor state I get:
SSL expiry 2

When I print the date last updated state I get nothing
{{states.sensor.date.last_updated}}

I have fixed the sensor.

I hadn’t configured the time_date sensor, as per your last message.

So I added the platform time_date and rebooted and then the sensor started working :grinning:

Thanks for your help

1 Like

If you do much with templates (especially time/date elements), it’s really useful to have these set,
Well done
:+1:

Can you explain that please ?

Explain what ?
It follows ‘the rules’
And in the template bit ; -

((as_timestamp(states('sensor.cert_expiry_timestamp_my_sslduckdns_org')) - as_timestamp(states.sensor.date.last_updated)) / 86400) | int }}

it has only two states that ever change to update the template
The cert expiry updates only every 6 months or so (you can pretty much forget about that) and sensor.date only changes 1 / day

The other template (with the work around for now() ) updates every minute

Less Time Processing, Less resources consumed. More efficient !

1 Like

Yes thanks, sorry for that. I mean what is this: sensor.date ? Or how it’s create ?

Ok i think it is that:

sensor:
  - platform: time_date

Note to ALL :
HA treats all sensors the same, there are (currently) no sensors that are set up by default.
So any sensor needs to be set up. These time and date sensors mean that even the most ardent GUI fan needs to get their hands dirty with a bit of yaml at some point.

1 Like

Hey Thanks for the input! merged anything into a package and added some alarm automations!

Screenshot:

package and lovelace yaml is found here:
Gist

Hi All,
I’ve implemented this cert_expiry timestamp sensor, but it shows 26 weeks till expiration instead of 90 days.
exactly 3 months more than written in Let’s Encrypt’s logs.

Do you have any suggestion how to fix it?

Thanks!

maybe you should post your code. sounds like an error in the template.