Skip to main content
Smarthome 3 mins

Home Assistant Quickie: Displaying values as percentages

A percentage display is often much more intuitive than a raw measurement value. I’ll show you how to output such values.

Home Assistant Quickie: Displaying values as percentages

With my cistern, the fill level in centimeters and liters is calculated from the voltage of the water level sensor . Of course I know that at 100.5 cm fill level the cistern is full to overflowing and therefore contains 5130 liters of water according to the manufacturer’s specifications. But how do you output the fill level as a percentage? This is also interesting for battery displays, because 70% battery level is more intuitive to understand than 2.7 volts.

With a sensor template, the conversion is very simple, because you can practically build a “virtual” sensor that outputs a calculation from other sources.

⚠ [affiliate] Keine Produkte mit Cache für home-assistant. Bitte affiliate-sync --lang en ausführen.

We need a sensor with the current value, e.g. the current water level in liters. For me, this is the entity sensor.zisterne_liter. Also the maximum value to be achieved, the base value. For my cistern, that would be 5130 (liters).

Now create a new sensor. Either under sensors: in the configuration.yaml or, as I do, in an external sensors.yaml that only contains my sensors - this creates more clarity.

yaml
1
2
3
4
5
6
7
sensors:
  - platform: template
    sensors:
      zisterne_prozent:
        unit_of_measurement: '%'
        value_template: >-
          {{ (((states('sensor.zisterne_liter'))|float / 5130) *100)|round(1) }}

In value_template we divide the measured value of the sensor by the base value, i.e. 5130, and multiply the result by 100. round(1) indicates that it should be rounded to one decimal place. If you don’t want decimal places, specify round(0).

After reloading, you have a new entity sensor.zisterne_prozent that you can then integrate into the dashboard.

For a battery, you first need to know at what point a battery can be considered empty. For button cells, as found in many devices like the Aqara ZigBee sensors, 3 volt lithium button cells are used. Most manufacturers state that the cell is empty at a voltage of 2 volts. Therefore, you must not assume 3 volts as the base value, but only the difference between a new battery at 3 volts and an empty battery at 2 volts, i.e. 1 volt. For 1.5 volt batteries, 0.9 volts is assumed for an empty battery, so you would have to assume 0.6 volts as the base value (1.5 volts - 0.9 volts).

yaml
1
2
3
4
5
6
7
sensors:
  - platform: template
    sensors:
      batterie_prozent:
        unit_of_measurement: '%'
        value_template: >-
          {{ (((states('sensor.batteriespannung'))|float / 1) *100)|round(0) }}

Here you take the sensor that measures the current battery voltage of a device and, for example, set 1 as the base value for a 3 volt battery, and you already have the battery status in percent.


Hi! I'm Markus – the Fricklr. ⚡ Electrical engineer, 🎵 musician (bass, guitar, keys) and professionally 'something with the internet' 🌐 for over 30 years – nowadays with AI too 🤖 – a 55-year-old generalist from Bavaria, Germany.

Your feedback helps me write even more interesting posts. After "Yes" there's also a small option to support my work.

Explore related topics

Content Map →
Show related content as a list
💡 Drag, hover & click to explore
Bigger dots are more relevant

What do you think? Leave a comment!

Share your thoughts, questions or experiences with the community.

Links marked with this symbol are affiliate links. As an Amazon Associate I earn from qualifying purchases. There are no additional costs for you.