Format sensor value string as a pretty date?

I have this sensor in an entity card:

  - entity: sensor.hp_lastlegionellaactivationtime
    name: Senaste legionella körning
    icon: mdi:water-boiler
    secondary_info: last-updated

Which simply outputs a string value, “2023-02-18T12:03:00Z”

How can I format this into another dateformat and possible timezone?

Most core cards, such as the Entities card you showed, do not allow you to make those modifications. You will need to use a card that allows templating, one popular choice would be a template card from the Mushroom custom cards.

If you put the following in the sensor’s config it will display relative time assuming the state is formatted correctly (yours appears to be):

device_class: timestamp

If you can’t control the config, you can possibly also put it in “customization.yaml” (not tested). If you do it in customizations, restart HA to be sure as it normally requires a state change to “kick in”.

You can also use “format” on the entities card to change to a different format as shown here.

Aha, nice, I do indeed control the sensor config. I changed to timestamp and added format: datetime to my entity config and it shows up perfectly! Thanks!

Is it possible to set the correct timezon?

My local time is 11:00 and UTC is 10:00 but the time shown is 12:00,1 hour wrong.

It works for me - if I put in “2023-02-18T12:03:00Z” it displays as “18 February 2023 at 23:03” (my timezone is +11). The “Z” means it should be UTC - so is “March 4, 2023 at 12:01” coming from “2023-03-04T10:01:00Z” or “2023-03-04T11:01:00Z”? It should be the first option to end up with “11:01”, whereas the second will give “12:01” local.

Alternatively, could it be that the timezone is set incorrectly for HA? Under “Settings > System > General”. This might cause it to parse the UTC time incorrectly.

Hmm no I checked my timezone, it should be correct. Could it be a summertime issue perhaps?

I tested this and the time looks correct here:

What does this give you:

{{ "2023-02-18T12:03:00Z" | as_datetime }}
{{ "2023-02-18T12:03:00Z" | as_datetime | as_local }}