Hello,
I have a Home Assistant Green, and an energy meter connected to the P1 port of my main electricity meter. Because I have a dual tariff meter, the energy meter shows the electricity usage for tariff 1 during the day, and tariff 2 during the night. The unit of measurement is kWh.
So, I thought, I want to see the total usage, so I created a helper using the Home Assistant web interface. I chose type “Combine the state of several sensors”. Then I chose the two entities ‘energy consumption tariff 1’ and ‘energy consumption tariff 2’. Then I set it to sum the two.
For both entities these are the yaml settings which I can see in the ‘state’ under developer tools:
state_class: total_increasing
unit_of_measurement: kWh
device_class: energy
friendly_name: Energy Consumed Tariff 1
Now the Helper that I created, has this yaml code:
state_class: measurement
unit_of_measurement: kWh
icon: mdi:calculator
friendly_name: Energy Cons Total
So somehow it decided that even though the state class of the two entities is ‘total_increasing’, it uses state_class ‘measurement’ for the helper object and has no device_class set.
This creates two errors:
Unexpected device class
The following entities do not have the expected device class:
sensor.energy_cons_total
Last reset missing
The following entities have state class ‘measurement’ but ‘last_reset’ is missing:
sensor.energy_cons_total
If I go to the States section in the developer tools, and change the state and device class to ‘total_increasing’ and ‘energy’ respectively, it works and the entity shows the total consumption. Also the errors go away. Unfortunately, this is only temporary.
Everywhere I look, instructions are to edit the configuration.yaml. However, this yaml only has 10 lines of code, so I can’t change the yaml code for the new helper.
Does anyone know how I can setup this helper to use the correct state and device class? I have the file editor installed, but I can’t find which file the settings are stored in.
Thanks in advance!
Pier
Hi. There are two ways to solve this. One is not to use the helper but to use a template sensor instead that you create in yaml. The other is to override the state class using customize. Also using yaml.
To customize, add this to your configuration.yaml (or merge with lines already there):
homeassistant:
customize: !include customize.yaml
Create a file custimize.yaml in your config folder, with the things you want to change:
sensor.energy_cons_total:
state_class: total_increasing
device_class: energy
Use the developer tools to check the config, and if there are no erors, restart home assistant.
ps. You could also put the contensts of customize.yaml straigt below customize, but this way you can keep your config clean and separate out various things. In the long run you’ll be happier this way.
I don’t think the helper can iron out all problems when combining multiple sensors. In your case, the helper could have done a bit better, but how to add different state classes? Helpers are meant to keep things simple, and measurements are by far the most common.
I use the helper just to sum the kWh of two sensors which are identical in their device and state class. It just decided on its own to not use a device class and change the state class to something else, which seems a bit odd. On the other hand, I’m completely new to this so please forgive my ignorance and feel free to correct my assumptions
You are right, in your case the right state class was obvious. But is is a new sensor. It didn’t “change” anything, it just assumed measurement without looking at the entities it was combining. Had you chosen another way to combine the sensors, would the state class then still be total_increasing? Or maybe total, or measurement? So you might consider it a bug, or just a limitation. If you create your own sensor in yaml, you are in full control. If you use gui helpers, you are often more limited in what is posible.
Be aware, if one sensor resets and the other does not at the same time, the sum is technically speaking not total_increasing. It might behave badly when that happens. All the more reason not to use the sum in the energy dashboard, but instead add both peak and offpeak sensors individually.
Thanks @Edwin_D I got it working. I went with the template helper. It was tricky to find out what to put in the ‘state template’ part of the template helper.
I ended up with:
I’d also put in an availability template, which is pretty important for state_class total or total_increasing to get right (something else gui helpers cannot do):
availability: |
{{ has_value('sensor.energy_produced_tariff_1') and
has_value('sensor.energy_produced_tariff_2') }}