Custom sensor display precision

I’ve defined a sensor in my yaml:

- name: "CT1 Current Sensor 1"
      state_topic: "Mains_Power/Status"
      state_class: measurement
      value_template: >
        {% set x = value.split(',') %}
        {{ ((x[1] | float(0)) -0.05) / 6.296 }}
      unit_of_measurement: "A"
      icon: mdi:current-ac

However, the value is being displayed as 0.00158831003811944 A, which is one or two decimal points more than I need. I want to keep the sensor with precision than I’m showing, as I am using the value for further calculations.

For device sensors, there is the precision drop down. However, for yaml defined sensors, I get:
This entity (‘sensor.ct1_power_1’) does not have a unique ID, therefore its settings cannot be managed from the UI. See the documentation for more detail.

What config do I need to set the display precision? I’d prefer to avoid making a dummy sensor just for display purposes, as it starts to make the yaml unmaintainable.

Thanks

User “round()” to get “.xxx” format:

{{ some_float_value_or_expression | round(3) }}

As of release 2023.3 you can set it in the gui, check the release notes.

This is a template sensor w/o unique id.
And I do not think that splitting settings about one sensor between UI & yaml is a good idea.

1 Like

Add a unique ID, then. I use this site to provide random UUIDs but you can use any random string.

- sensor:
    - name: "CT1 Current Sensor 1"
      unique_id: 7145e5a0-caf3-4fef-b99d-d95d0cfd1b0a
      state_topic: "Mains_Power/Status"
      state_class: measurement
      value_template: "{{ (value.split(',')[1]|float(0) - 0.05) / 6.296 }}"
      unit_of_measurement: "A"
      icon: mdi:current-ac

What is a purpose of this ID - just to set a precision?
OK, this is one of the ways - but then you will get a user-yaml-defined sensor (i.e. not created by some integration added via UI) whose settings are partially defined via UI. If managing setup via UI is absolutely OK for you - then fine).
I myself do not recommend to use UI-controlled precision for entities created/managed by yaml.
I accept a tendency of “moving HA settings from to UI” only if similar yaml-settings are supported.
Currently many integrations are UI-controlled only, I had to accept it - but rest of other parts of HA are kept in yaml. This is MY choice.

The sensor is unaffected and will continue to measure with a precision of 10 attoamperes which the OP wanted to remain — your round() would reduce precision. The unique ID gives the UI a reference to allow it to customise its display in the front end. It’s a separation of data and presentation, but I do take your point.

Agreed, but imagine this special case:
– entities & integrations are set up in yaml (except UI-controlled-only integrations);
– all HA parts are stored as packages (“Tesla”, “garden”, “vacuum” etc);
– lovelace is managed in yaml;
– when a vacuum is moved to a country house - just move the whole package into another HA setup;
– when one more Tesla is added - just create a copy of the corr. package with needed changes;
– also you may just “remove” (temporarily ofc) some package if you do not use it.
All this is possible only when your setup is yaml-controlled. And this is why I keep all my helpers & automations in yaml too.

Thanks all.
The unique ID has enabled me to set the precision.

I need a different storage precision to the display precision, as I am using data entity for further calculations - e.g. sum(current * voltage) * unit rate, and a loss of precision will impact the answer (albeit only a few pence in this case).

I agree its not ideal having some config in the yaml and others in the UI, but setting the precision only in the yaml loses precision rather than simply masking it.

Then using rounding for frontend only is a good idea.
But in similar cases I usually ether change a precision locally (where it is possible - like multiple-entity-row or template-entity-row) or use a specially created template sensor with rounding (excluded from Recorder)…

I have a similar question. Gasoline prices are often shown with 3 decimal places, the last one always being a 9 anyway. I would like to cut off the 9 and do it without rounding. By default, Home Assistant seems to give only the option to use it rounded. How can I implement it differently?

I was also hoping to customize the sensor and then use a value_template, but that doesn’t seem to work.

I tried this:

sensor.tankerkoenig_aral_gas_station_e5:
  entity_picture: /local/custom-images/fuel/aral-fuel-station.png
  value_template:"{{value | round(2, 'floor')}}"