Using the date as an entity has a different result than using it as a state

Hey there it is really simple but I don’t know how to get over it. It cannot be possible to have several lines of code every time you want to show a date in the shape you want.

You see I’m using the sensor.uptime multiple times. The chips card is nice but I guess I will forget what all these values mean if it only says last month. That is why I wanted to create my own template and add some text. The problem here is that I have to use it as a state of the entity and as soon as I do this I get the timestamp returned and not the default localized date value as when I use the entity as such.

Why is it happening? How can I fix it? I know it would be possible to get the same value there with several lines of code but is it really necessary?

type: custom:mushroom-chips-card
chips:
  - type: entity
    entity: sensor.current_version
    icon_color: blue-grey
    use_entity_picture: false
  - type: entity
    entity: sensor.last_boot
    icon_color: blue-grey
  - type: entity
    entity: sensor.uptime
    icon_color: blue-grey
  - type: template
    content: Uptime {{ states.sensor.uptime.state }}
    icon: mdi:clock
    icon_color: blue-grey
    picture: ''

The UI does not show the actual state of entities in entity cards or the more info pop-up. It uses the type information of the entity and locale of the user to make the value more presentable. For example it will capitalize letters and localize text values, localize numbers, format and localize dates, etc.

The only place in the UI you can see the actual state of an entity is in developer tools → states. And in templates, the actual state is what is used in templates. You are now seeing the actual state instead of how the UI choose to present it

Hmm okay. Is it possible in a template to use the localized value that is shown to the user in the frontend?

There’s no easy option for that but you can definitely recreate it with stuff from here. Easiest option is probably this:

Uptime {{ states('sensor.uptime') | as_datetime | relative_time }}

That will do an age string though. If you want the full date value like the UI has it you probably need to use timestamp_custom or strftime and specify the format.

1 Like

Thank you very much. It is not what I hoped to find but it is still a valid solution.

1 Like