Conversion timestamp with template card

Does anyone know how the entity type does the conversion from a timestamp?

For me, the entity type in the UI says something like “In 1h”, but if I read the entity myself I only have the complete timestamp. I would like to have exactly the same output in my template card, but I don’t find the magic logic of how this happens:

This is how it is in the entity card:

The sensor looks like this:

And this is what it looks like as a template card:

Change the device class…‘timestamp’ has this effect in some (!) cards, e.g. in entities but not entity

Ok this comes from a integration.
What is the best way to change it? Own sensor or global customize?

little info provided so non specific response…
you can customize it Customizing entities - Home Assistant (home-assistant.io)
or create a template sensor based on this one Template - Home Assistant (home-assistant.io)
or use a template within a card to convert it.
…it boils down to the use you hae in mind, I an not guess this with above

EDIT: my preference would be in the card itself, adding sensors just for this is imo inefficient

1 Like

IMHO changing a device_class is a bad idea.
Better to use a custom card which may present the entity in a user-defined way - like template-entity-row.

code
type: entities
entities:
  - entity: sensor.test_timestamp
  
  - type: custom:template-entity-row
    entity: sensor.test_timestamp
    
  - type: custom:template-entity-row
    entity: sensor.test_timestamp
    state: >-
      {{ as_datetime(states(config.entity)).date() }}

Care to add why? I did this twice as I similarly got annoyed about cards showing ‘in x minutes’

IMHO changing a device_class is smth like “changing a nature of data on a low level”; if some issue may be easily solved by a different presentation - then it should be done on a Frontend side.

Agree, my preference too (see above) but in the past when I just started…I did this twice and never had any issue

What information do you need exactly?

The sensor comes from the Home Connect integration and I’m currently only trying to convert the timestamp in the frontend.

Haven’t you been given possible solutions already?

No. Iam looking for a solution for “custom:mushroom-template-card”. And in your example I see not the same result, what I am looking for. Currently I play with “timestamp_custom” around

Not sure what you want to achieve but template stuff… alike this?

{{ as_datetime('2023-06-21T09:50:18+00:00').strftime("%A %d %H:%M") }}
will give you: Wednesday 21 09:50

or in your case

{{ as_datetime(states('sensor.waschmaschine_remaining_program_time')).strftime("%A %d %H:%M") }}

@vingerha

Thats looks better

{{ relative_time(now()-timedelta(seconds = 
as_timestamp(states('sensor.waschmaschine_remaining_program_time'))
- as_timestamp(now())))}}

why are you doing all those calculations?

{{ relative_time(states('sensor.waschmaschine_remaining_program_time') | as_datetime) }}

Example:
{{ relative_time(states('sensor.waschmaschine_remaining_program_time') | as_datetime) }}
out put 2023-06-26 13:31:49+00:00

My output is.
57 minutes
or
1 hour etc.

And this iam showing in the mushroom card template something like:
“The washing machine needs 57 minutes”

Ah, that’s a future time. That’s why.

Exact timestamp when the program ended (in future).

I still find it interesting that the entity card with device class timestamp displays it like this and not just the date and time or something like that

That’s what device_class does, it lets the frontend (and other aspects of HA) know what to do with the entities state. The filter/function relative_time does not work with future datetimes. Probably something that should be updated.

Yes off course but timestamp looks in every card a little different, correct?

Nope. They will always display relative time.