Allow sensor values to be format in UI without a new template sensor

I have a number of sensors which output values as ints or datetimes. When I add these to a dashboard, lovelace will just display the value and unit which can be difficult to read (OctoPrint time elapsed and remaining, I tried to add a date example but am restricted to 1 image per post).

image

I know that I can get these to display in a better format using a template sensor to re-format the value but this seems a lot of effort and quite technical to achieve this. I think it would be good if lovelace could make this easier to do, and came up with a few ideas.

Idea 1: Dates could be format based on some localization setting on the user account. This could also have a “default localization”.

Idea 2: Add a new property to the lovelace cards when specifying an entity. This could be a Jinja template which takes the state and reformats it for display in the UI (removing the unit of measure as it may no longer apply). This has the advantage of being able to show the same sensor in different formats.

Idea 3: Add an optional attribute to sensors for ui_format or something similar. This would contain the state value but in an easier to read format. Lovelace would then use this by default if it is present. If users do not like this format, it could be disabled via a customization or as a property on the card.

It is worth noting that none of these ideas are exclusive and all could be implemented.

:+1:
Would be a great added value for the application. But it will be a big challenge, because not only HA GUI would have to do this, but all GUI modules.

I think this approach is good if it would use not only the date but also numbers, texts (translations) for the set locale (show localizable values):

    1. use localizable strings
    1. use localizable numbers
    1. use localizable date
    1. use localizable time

The localization can be set via the user account (profile):

profile_setlocale

Idea 1: Dates could be format based on some localization setting on the user account. This could also have a “default localization”.

1 Like

I actually found that using an Int or Float makes a difference in the UI.

My DSMR energy meters show a value with 3 decimals in the UI. I’d like to show only the portion before the decimal separator. So I tried to truncate the value to an integer:

sensor:
  - platform: template
    sensors:
      truncated_gas_consumption:
        friendly_name: Trucated Gas Consumption
        unique_id: truncated_gas_consumption
        value_template: "{{ states('sensor.gas_consumption') | int }}"
        unit_of_measurement: m³

The new templated sensor wil show as a plain number. When the original sensor is 1234.567, then the truncated sensor shows 1234.

When I (also) make the int a float, the output is different.

sensor:
  - platform: template
    sensors:
      truncated_gas_consumption:
        friendly_name: Trucated Gas Consumption
        unique_id: truncated_gas_consumption
        value_template: "{{ states('sensor.gas_consumption') | int | float }}"
        unit_of_measurement: m³

This template sensor will show 1.234,0 in the frontend. So it (nicely) shows the thousands separator, but (regretfully) also adds a decimal (of zero).

What I had hoped is a more intelligent UI, that doesn’t show any decimals if the value is a round number.

1 Like

Sounds to be good but that is just only half of the truth at least here.

It all depends on which kind of card you use for display and if that is supporting local settings or not.

Your thousand separator “.” only appears on built in cards, but usually not with custom cards so this is not the game changer that many might expect from your description as I had hoped too.

It simply does not work with bar card and a lot of others, but with built in cards that show entities or others it will work.

This is a double edged sword cause it will hit and hurt once you will have understood that it has close to no impact on advanced stuff.

Unfortunatelly my journey continues cause i do not have a solution to force such cards to use my local formats or preferences like “#.##0,00” which is that simple.