Display precision for sensor attributes

Hi everyone. Considering myself relatively experienced HA user, but can’t work out the following: I have defined a template sensor in YAML with several attributes. When displaying this sensor in a standard entity card, it get’s rounded down to 1 or 2 decimals (depending on the value).

Since it represents the electricity price, I want it to display always 3 decimals. Whatever I did, I couldn’t get it to work. However, when I defined the same calculation as a sensor (instead of attribute of a sensor), it does work?!

Here is how the sensor and the sensor+attribute show up in the developer tools:

But this is how they show in the entity cards:

Of course, I can easily define 5 separate sensors instead of attributes, but I just want to understand if I’m doing something wrong? Thanks.

YAML:

      - name: "Utilities Price Electricity Current Full"
        unique_id: utilities_price_electricity_current_full
        # state_class: measurement
        device_class: monetary
        unit_of_measurement: "€/kWh"
        state: >
          {% if is_state('sensor.utilities_db_prices_current_compare_electricity', 'Dynamic') %}
          ...more script...
          {{ (market_price + purchase_fee + energy_tax) * (1 + vat / 100) | float }}


      - name: "Utilities Price Full Electricity"
        unique_id: utilities_price_full_electricity
        # state_class: measurement
        device_class: monetary
        unit_of_measurement: "€/kWh"
        state: "{{ states('sensor.current_electricity_market_price') }}"
        attributes:
          price_full_current: >
            {% if is_state('sensor.utilities_db_prices_current_compare_electricity', 'Dynamic') %}
            ...more script...
            {{ (market_price + purchase_fee + energy_tax) * (1 + vat / 100) | float }}

I don’t think you can control the precision of attribute values in an entity card, but an option might be to use a markdown card:

type: markdown
content: "{{ ( state_attr( 'sensor.utilities_price_full_electricity', 'price_full_current' ) ) | round(3) }} €/kWh"

You need a fixed precision for attributes in a template sensor - define it in a template sensor.

Thanks, yes that’s the way I’m going. Just kinda weird that as a default the entity_card displays states and attributes differently (wrt. precision).

HA doesn’t modify the number of sig figs for attributes because attributes are meant to be raw data. HA in general is moving away from them in favor of entities so that users can control things like sig fig.

The card shows values as is. The difference is that for a state you can customize a precision in UI - if this entity is “editable” in UI. Note that’s for template sensors you can define a precision for a state and attributes inside a template definition. So, that possibility to customize a precision in UI is imho needed only for not-template sensors.