Why does my template sensor use the wrong timezone?

My template sensor uses this

- name: "Aktuelle Stunde"
      unique_id: "aktuelle_stunde"
      device_class: timestamp
      state: >
        {{ today_at(now().hour | string + ":00").isoformat() }}

and will return

2024-09-13T00:00:00+00:00

However, if I test the same template in the developer tool tab, it results in

2024-09-13T02:00:00+02:00

I’m well aware that these are two ways to write the same time, the first one being UTC and the second one being my own timezone.

However, I need the sensor to return my own timezone.

Why does the template sensor use UTC? Can I change this behaviour?

Remove the device class designation and it will return a datetime string in your local timezone.

2 Likes

Thank you, this did the trick.

Sorry for the follow-up question, but can you point me to the explanation for this? What would should be the proper device_class for this sensor?

The explanation is that HA stores all timestamps in UTC. When you set up a sensor with the device class timestamp it’s value is converted to UTC.

Sensor: Device Class
Dev. Docs

None. This is the default and doesn’t need to be set.

1 Like

Thank you for explaining this!