HASS.IO, Date/Time Templating

Running version 0.64.3 of HASS.IO, and I have a sensor that is reporting back in UTC date time. I would like to convert this to a custom local display – ideally accounting for daylight saving.
The original code for the template with an unformated time is,

  - platform: template
    sensors:
      lounge_motion_updated:
        friendly_name: Lounge last movement detected
        value_template: "{{states.sensor.lounge_motion_sensor.attributes.last_updated}}"

I have used the Template Editor to try a number of different formats. They don’t return an error, but don’t return the expected result either. No matter what I try most return the same UTC date time.
For example,

{{states.sensor.lounge_motion_sensor.attributes.last_updated }}

returns [β€˜2018-03-06’, β€˜04:17:28’]. So does the following three lines,

{{states.sensor.lounge_motion_sensor.attributes.last_updated | timestamp_local }}
{{states.sensor.lounge_motion_sensor.attributes.last_updated | timestamp_custom(β€œ%a:%d:%B:%H:%M:%p”) }}
{{states.sensor.lounge_motion_sensor.attributes.last_updated | timestamp_custom(β€œ%I:%M %p”) }}

But,

{{states.sensor.lounge_motion_sensor.attributes.last_updated | timestamp_local(β€œ%I:%M %p”) }}

returns nothing.

I figure if I can’t get the right result in the Template Editor it won’t matter about the configuration file entries. Is there something stupid I am doing wrong with the format??

OK - found the problem. the sensor was not returning the value in the date/time format, so any functions to manipulate that would not work. Using string functions allowed the values to be converted to the correct format.

In case anyone else has the problem the following may help,

  - platform: template
    sensors:
      lounge_motion_updated:
        friendly_name: Lounge last movement detected
        value_template: >
          {{ as_timestamp(states.sensor.lounge_motion_sensor.attributes.last_updated | replace("[","") | replace("', '", "T") | replace("]", "") | replace ("'", "") + "+00:00") | timestamp_local }}

TIL - The Template Editor is your friend! :grinning:

1 Like