Can I reformat teh way time is displayed in an entity

I have a number of Tasmota plugs which have energy monitoring. The HA Entities include Total energy use and an entity to show the date/time the energy monitoring started. The default layout for this last entity is 2021-09-24T08:40:52.

I would like to reformat the display in Lovelace to only show the date.

Can anyone help?

1 Like

Create a Template Sensor that reformats the Total energy sensor’s time, then display the Template Sensor’s value in the Lovelace UI.

The Template Sensor’s template is simply this:

{{ states('sensor.your_sensor_energy_totalstarttime')[:10] }}

Other than that, the standard set of Lovelace cards don’t provide a way to reformat a sensor’s value. That’s why you need to create a new entity, such as a Template Sensor, to serve as an intermediary.

The only standard Lovelace card that supports templating is the Markdown card. You could use the template I posted above directly within the Markdown card without the need of the Template Sensor.

Thanks Taras, that’s great.

I’d like to know more about formatting options. What is the [:10] in your solution and where might I and out further info?

HaHa I just figured what you did - First 10 characters of output! Is there a way to actually reformat the way the date displays?

You need to convert it to a Unix timestamp and that feed it into timestamp_custom along with the pattern you want.

For example, this:

{{ (states('sensor.your_sensor_energy_totalstarttime') | as_datetime).timestamp() | timestamp_custom('%d/%m/%Y') }}

produces a date format like this: 25/09/2021

You can use this reference to compose the pattern you want.

Perfect - thank you. I also found the Template Tool under Developer Tool - really useful.

Thank you for your help this afternoon.

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.

und wo genau gebe ich diesen Code ein?

It’s explained in the second post of this topic: