Convert HH:MM time in a sensor to a timestamp format

Hi guys,

I have a sensor which pulls a time from another sensor as follows:

      elm_isha:
        friendly_name: 'Isha'
        value_template: '{{ state_attr("sensor.prayer_times","isha") }}'
        icon_template: mdi:clock

The sensor value appears, for example, as: “17:54” i.e. as an HH:MM.

I need to use this sensor as a time trigger for an automation, however Home Assistant requires the sensor to be formatted as a device_class: timestamp before it can be used as a time trigger.

I therefore need to format the HH:MM it currently appears as to the ISO 8601 format (meaning it has the “T” separator between the date and time portion).

It should therefore format as: [today’s date as YYYY-MM-DD]T[HH:MM from the “sensor.prayer_times”,“isha” sensor].

Example: 2023-11-20T17:54:03:49.253717+00:00`

I would be really grateful if someone could kindly help code the value_template to convert it.

Many thanks

      elm_isha:
        friendly_name: 'Isha'
        value_template: '{{ (today_at(state_attr("sensor.prayer_times","isha"))).isoformat() }}'
        icon_template: mdi:clock
        device_class: timestamp 

EDIT

Correction. Added missing closing parenthesis.

1 Like

Thank you so much @123
Works perfectly as intended, just missing a parenthesis towards the end (for anyone reading this thread in the future). You’ve saved me a few valuable hours trying to figure it out, it’s much appreciated.

1 Like