Some energy sensor help please

By copying other similar examples I’ve found in my searching here (I’m a complete novice with yaml) I created some power and energy sensors in my configuration.yaml file, one of which performs some simple maths to create a new sensor output.

They work and I can display the results OK.

However the output of this new sensor provides a history which seems as if it is outputting text data rather than numerical data (but I’m not entirely sure about this).

This is what this new sensor’s output history looks like:

with all those different colours representing each change in the numerical value of the sensor, when I would expect it to look a bit more like one of these types of history charts:

This is the code for the sensor:

  - platform: template
    sensors:
      hot_water_energy_diverted:
        value_template: >-
          {% set white = states('sensor.export_energy_phase_2_daily') | float %}
          {% set red = states('sensor.export_energy_phase_1_daily') | float %}
          {{ (white - red) }}

The idea is this new sensor is calculating the difference between the other two sensors.

It’s doing that OK, and I can display the value from the sensor in a dashboard entity card and it is the correct value but why is it not chartable like the other two sensors from which it is derived?

Is there something in the yaml code I am missing?

Thanks for any guidance.

I think last formula also needs | float.

You may also have to set up the sensor type to be numeric, I can’t recall this well.

1 Like

Thanks, like this?

     {{ (white - red) | float }}

I did that, checked configuration was good and restarted Home Assistant but it still shows the same type of history card output.

As you say perhaps it’s some definition of sensor type I (clearly) don’t know about yet.

1 Like

I am not sure what the issue is. :frowning:

Your suggestion helped me find the answer - I needed to add units of measurement. It seems this is what makes the history card show as a chart.

  - platform: template
    sensors:
      hot_water_energy_diverted:
        unit_of_measurement: "kWh"
        value_template: >-
          {% set white = states('sensor.export_energy_phase_2_daily') | float %}
          {% set red = states('sensor.export_energy_phase_1_daily') | float %}
          {{ (white - red) | float }}

And I now see this:

From here:

Ended up with this (an estimate of how much energy has been diverted to my hot water storage tank today so far):