Convert command_line to sensor time date

I am needing to convert the String that gives the following line of code to date and time in a sensor

- platform: command_line
  unique_id: ssl-homeassistant
  name: Home Assistant SSL Expiration
  command: '../ssl/openssl x509 -enddate -noout -in ../ssl/fullchain.pem | cut -c10-30'
  scan_interval: 86400

The date format that this gives is “Jun 2 00:39:56 2023”

value_template: "{{ strptime(value, '%b %d %H:%M:%S %Y', default='unknown') }}"

strptime is described here: Templating - Home Assistant

It seems to deal with the non-zero-padded day number. Incidentally, please format responses like that as code: your post hides the fact that there are two spaces when it’s a single-digit date number.

image

Your cutting-down of the openssl output removes any time zone information, but you probably don’t care to that level of precision.

Hi @Troon
Forgive my ignorance but I have little programming and with what you are giving me it is not working for me or I am not placing the code correctly

Does the sensor return the date string already without my additional line? Can HA run the openssl binary?

It’s an extra line in the sensor definition:

- platform: command_line
  unique_id: ssl-homeassistant
  name: Home Assistant SSL Expiration
  command: '../ssl/openssl x509 -enddate -noout -in ../ssl/fullchain.pem | cut -c10-30'
  scan_interval: 86400
  value_template: "{{ strptime(value, '%b %d %H:%M:%S %Y', default='unknown') }}"

You’ll then need to restart or reload:

image

Thanks It works!!

1 Like

This is what works for me

Sensor group: Date

sensor:
name: Date
command: Date_COMMAND
value_template: >
{% set months = [“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”] %}
{% set month = months[now().strftime(’%m’) | int -1] %}
{{ now().strftime(’%d’) + ’ ’ + month + ’ ‘+ now().strftime(’%Y’) }}
scan_interval: 60

Please format your posts correctly.

Your template simply returns the current date/time, ignoring the command, and is identical to:

{{ now().strftime('%d %B %Y') }}

If you thought you’d be helpful by using ChatGPT to attempt to answer a question (as your answer looks like the sort of thing it would produce), please see here: