Getting a template sensor calculating gas kWh to show up on the Energy Dashboard

Salve Cognoscenti,
I am struggling to get a calculated template sensor value to show up in the Energy Dashboard.

The only way I can calculate my gas usage is by having the gas consumption rate of the two gas boilers in my house as constants, measuring the duration each boiler is on each day, and then calculating the kWh with a bit of maths.
The template sensor I have set up works this out and will display the result on the front end:

- platform: template
  sensors:
    gas_kwh_today:
      friendly_name: "Gas kWh today"
      unit_of_measurement: "kWh"
      value_template: >
        {% set pool_gas_dur = states('sensor.duration_pool_heater_on_today') %}
        {% set pool_gas_dur =  pool_gas_dur|float if is_number(pool_gas_dur) else 0 %}
        {% set house_gas_dur = states('sensor.duration_gas_heater_on_today') %}
        {% set house_gas_dur =  house_gas_dur|float if is_number(house_gas_dur) else 0 %}
        {% set gas_kwh = 11.1354133333333 * ((pool_gas_dur * states('input_number.pool_gas_m3_per_hr')|float) + (house_gas_dur * states('input_number.house_gas_m3_per_hr')|float)) %}
        {{ gas_kwh|round(2) }}

But of course this won’t show up in the Energy Dashboard because it does not have a device_class or state_class defined. And this cannot be added to this now deprecated integration.
So my problem is I have not been able to work out how to convert my template sensors to the new format.
I have tried adding the following to the configuration.yaml file:


template:
  - sensor:
      - name:"Gas kWh today_1"
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total
        state: >
          {% set pool_gas_dur = states('sensor.duration_pool_heater_on_today') %}
          {% set pool_gas_dur =  pool_gas_dur|float if is_number(pool_gas_dur) else 0 %}
          {% set house_gas_dur = states('sensor.duration_gas_heater_on_today') %}
          {% set house_gas_dur =  house_gas_dur|float if is_number(house_gas_dur) else 0 %}
          {% set gas_kwh = 11.1354133333333 * ((pool_gas_dur * states('input_number.pool_gas_m3_per_hr')|float) + (house_gas_dur * states('input_number.house_gas_m3_per_hr')|float)) %}
          {{ gas_kwh|round(2) }}

But when I check the configuration I get the following error message:

Error loading /config/configuration.yaml: mapping values are not allowed here
  in "/config/configuration.yaml", line 90, column 28

Line 90 is: unit_of_measurement: “kWh”

Any suggestions would be very welcome, thank you.

The problem is caused by a small mistake on line 89. It’s missing a space between the option and its value.

Change this:

      - name:"Gas kWh today_1"

To this:

      - name: "Gas kWh today_1"

@123 do you never sleep??
Thanks so much for getting back to me so quickly. I feel more of a twit than usual missing that error.

Incidentally, I panicked when I checked the Energy Dashboard - I wiped all my electricity, solar etc measurements.
Then I realised it was 6 minutes past midnight.
Must get a life.
Cheers!

1 Like