Energy Dashboard and template sensors

Hello,

I am living in a block house and therefore i do not have the possibility to change meters to smart meters. I would although like to keep an eye of the Electricity, Gas, Water consumption and my only way is to do it manually.

I have created a few input number helpers as such:

  • input_number.gas_meter
  • input_number.total_electiricity
  • input_number.cold_water_meter
  • input_number.hot_water_meter

As a next step i need to add these to the energy dashboard but since i cannot do it directly, i needed to created template sensors in the configuration file. Here is my code:

# Gas meter - In order for this to work create a number helper with name: Gas Meter
template:
  - sensor:
      - name: "gas_consumption"
        unit_of_measurement: "mÂł"
        state_class: 'total_increasing'
        device_class: 'gas'
        state: "{{ states('input_number.gas_meter') }}"
        
# Total Electricity Manual Meter - In order for this to work create a number helper with name: Total Electricity

      - name: "electricity_total"
        unit_of_measurment: "kWh"
        state_class: 'total_increasing'
        device_class: 'power'
        state: "{{ states('input_number.total_electiricity') }}"
       
# Water Meters - In order for this to work create 2 number helpers with names: Cold Water Meter and Hot Water Meter
      - name: "cold_water"
        unit_of_measurment: "mÂł"
        state_class: 'total_increasing'
        device_class: 'water'
        state: "{{ states('input_number.cold_water_meter') }}"

      - name: "hot_water"
        unit_of_measurment: "mÂł"
        state_class: 'total_increasing'
        device_class: 'water'
        state: "{{ states('input_number.hot_water_meter') }}"

After restarting HA, i get a notification saying that the unit of measurement is not accepted for template. However, if i paste only the gas meter (the first one), it works and i am able to see it in the dashboard. There must be a syntax error and i have gone through the documentation but unfortunately i am not able to find an example of multiple template sensors in the configuration file.

Also, if someone is more experienced than me, are the device_class correct on every sensor above?

Thank you very much
M

1 Like

I read somewhere that the error is expected as it was not fully implemented…but it should work despite the error, should be fixed this month

Thanks for your reply,

With the above code, home assistant passes the validation and restarts, however nor the electricity or water is appearing in the dashboard. I will try again…

It takes time for the statistics to build…up to two hours

I think i wasn’t clear… The sensors are not visible to be selected in the dashboard… You know, when you go to add a sensor to the grid, and water sections…

For your electricity the device class should be energy not power.

These are mine and working fine gas/water

 - sensor:
      - name: "Gas Usage"
        unique_id: Gas_Main
        unit_of_measurement: "mÂł"
        device_class: gas
        state_class: total_increasing
        state: "{{ states('input_number.gasmeter') |float(0) }}"
        
      - name: "Water Usage"
        unique_id: Water_Main
        unit_of_measurement: "mÂł"
        device_class: water
        state_class: total_increasing
        state: "{{ states('input_number.watermeter') |float(0) }}"

// hehe i did reply to the wrong person…

@michaelkrtikos

This is how it works for me, u can try this if it still doesn’t work for you.

Change this:

  - sensor:
      - name: "gas_consumption"
        unit_of_measurement: "mÂł"
        state_class: 'total_increasing'
        device_class: 'gas'
        state: "{{ states('input_number.gas_meter') }}"

to this:

  - sensor:
      - name: "gas_consumption"
        unit_of_measurement: mÂł
        state_class: total_increasing
        device_class: gas
        state: "{{ states('input_number.gas_meter')|float()) }}"

that’s how i have it working right now.

Thanks Hector,

I will give it a try… With the above configuration (before i try your suggestion) i have the following error:

By any chance running an old HA version?

for me that error happens also

if i use

unit_of_measurement: “m³”

instead of

unit_of_measurement: mÂł

But the question is, do you have this code in the configuration.yaml directly or do you use an other yaml called templates.yaml ?

@ vingerha - I use the latest version of HA.
@ Hector - i directly try this in the configuration.yaml.

I have tried both “m³” and m³ and i get the same error…

In order to avoid confusion as each one of you suggests changes to the code faster than i can reply, i will be providing actual code and error (if any). So far i have changed power to energy and i removed the quotes from the unit of measurement and the code is as follows:

# Gas meter - In order for this to work create a number helper with name: Gas Meter
template:
  - sensor:
      - name: "gas_consumption"
        unit_of_measurement: mÂł
        state_class: 'total_increasing'
        device_class: 'gas'
        state: "{{ (states('input_number.gas_meter')|float()) }}"
        
# Total Electricity Manual Meter - In order for this to work create a number helper with name: Total Electricity

      - name: "electricity_total"
        unit_of_measurment: kWh
        state_class: 'total_increasing'
        device_class: 'energy'
        state: "{{ (states('input_number.total_electiricity')|float()) }}"
       
# Water Meters - In order for this to work create 2 number helpers with names: Cold Water Meter and Hot Water Meter
      - name: "cold_water"
        unit_of_measurment: mÂł
        state_class: 'total_increasing'
        device_class: 'water'
        state: "{{ (states('input_number.cold_water_meter')|float()) }}"

      - name: "hot_water"
        unit_of_measurment: mÂł
        state_class: 'total_increasing'
        device_class: 'water'
        state: "{{ (states('input_number.hot_water_meter')|float()) }}"

Error with this code is:

Guys, i am so ashamed…

There was a typo in the word measurement… instead of

unit_of_measurement

i was typing:

unit_of_measurment

Thank you all for your efforts, now i have it working!

haha no worries,
this can happen to everybody.

glad u got it working.

cheers