Jinja template: Get local time from statitics attributes

Hi,

I have a statistics sensor to get a min and max temperature over the last 24 hours. This working very fine:

platform: statistics
name: sts_terrassetemp
entity_id: sensor.netatmo_terrasse_temperature
sampling_size: 1440
precision: 1
max_age:
  hours: 24

Now the entity have two attributes to tell the min and max time for example:

min_age: 2019-10-07T11:05:42.037604+00:00
max_age: 2019-10-08T10:58:44.592072+00:00

How can I convert these attributes to my local time zone and a format like this: dd.mm. hh:mm to sho the timestamp with the min / max value at my frontend. Here ist the template config where I want to insert the timestamp:

platform: template
sensors:
  tmp_terrasse_temp_min:
    friendly_name_template: "Außen Min.-Temperatur 24h"
    entity_id:
      - sensor.sts_terrassetemp
    icon_template: mdi:thermometer
    unit_of_measurement: "°C"
    value_template: >-
      {{ states.sensor.sts_terrassetemp.attributes.min_value }}
  tmp_terrasse_temp_max:
    friendly_name_template: "Außen Maxi.-Temperatur 24h"
    entity_id:
      - sensor.sts_terrassetemp
    icon_template: mdi:thermometer
    unit_of_measurement: "°C"
    value_template: >-
      {{ states.sensor.sts_terrassetemp.attributes.max_value }}

Regards, Steffen

    value_template: >-
      {{ as_timestamp(states.sensor.sts_terrassetemp.attributes.min_value) | timestamp_custom('%d.%m %H:%M') }}

1 Like

Wow, thank you very much. You are the hero of the day …

I try something with string operations because I do not know the commands as_timestamp and timestamp.custom. My template is now:

platform: template
sensors:
  tmp_terrasse_temp_min:
    friendly_name_template: >-
      {{- "Außen 24h Min.-Temperatur: " -}}
      {{- as_timestamp(states.sensor.sts_terrassetemp.attributes.min_age) | timestamp_custom('%d.%m. %H:%M') -}}
    entity_id:
      - sensor.sts_terrassetemp
    icon_template: mdi:thermometer
    unit_of_measurement: "°C"
    value_template: >-
      {{ states.sensor.sts_terrassetemp.attributes.min_value }}
  tmp_terrasse_temp_max:
    friendly_name_template: >-
      {{- "Außen 24h Max.-Temperatur: " -}}
      {{- as_timestamp(states.sensor.sts_terrassetemp.attributes.max_age) | timestamp_custom('%d.%m. %H:%M') -}}
    entity_id:
      - sensor.sts_terrassetemp
    icon_template: mdi:thermometer
    unit_of_measurement: "°C"
    value_template: >-
      {{- states.sensor.sts_terrassetemp.attributes.max_value -}}

Feel free to optimize - I’m new to Jinja!

Steffen