Template variable appears as a non-numeric state on dashboard

I have a template variable created in configuration.yaml as follows:

  • sensor:
    • name: ‘Bathroom Humidity Excess’
      device_class: ‘humidity’
      state: >
      {% set bathroom = states(‘sensor.bathroom_humidity’) | float %}
      {% set landing = states(‘sensor.landing_humidity’) | float %}
      {{ (bathroom - landing) | round(1, default=0) }}

This works fine, in the sense that I have automation conditions that use this value.
But when I create a dashboard history card from this entity, it shows as a series of little coloured rectangles - i.e., it doesn’t appear to realize it is a number, rather than text.

How do I change this?

Adrian Stephens, Cambridge UK

In the future, please take the time to format your yaml properly.

Assuming your yaml is correct, you need to add a unit_of_measurement.

Hello Petro,
Apologies for the lack of formatting. Here it is:

  - sensor:
      - name: 'Bathroom Humidity Excess'
        device_class: 'humidity'
        state: >
          {% set bathroom = states('sensor.bathroom_humidity') | float %}
          {% set landing = states('sensor.landing_humidity') | float %}
          {{ (bathroom - landing) | round(1, default=0) }}        

As I understood it from reading the documentation, the device class also implies a unit of measurement. Are you saying adding an explicit unit of measurement is different?



it should be implied, but if the entity itself in HA does not have a unit of measurement, it will treat the state as non-numeric. So check the entity in the system. If it has a unit of measurement, then you’re good.

If you created the sensor at a point in time without a unit of measurement, those old data points will still be treated as a string and you’ll have to wait until they are purged from your database before it can be viewed as a numerical values. Making changes now does not affect what happened in the past.

Thank you, Petro. That did the trick. Defining unit of measurement to be ‘%’ also made the presentation numeric. The documentation does say that, but I didn’t understand that bit until you made it obvious.