To make the new energy display easier to understand I’ve added template sensors that add up the energy from several devices to produce one aggregate device.
You can use this by simply putting any device that measures energy in the list. You will then get a new sensor ‘sensor.outside_lights_energy_kwh’ that reports the total energy of the group, which you can add to the energy dashboard.
A few things to note:
You need the unit_of_measurement, device_class, state_class, and last_reset for it to work with the energy dashboard.
I have template: !include_dir_merge_list templates/ in my configuration.yaml and I put the file light_energy.yaml in the templates directory to load it.
To debug this you go to Configuration->Server control.
Click “Check Configuration” to find syntax errors.
Then reload “Template Entities”.
You then go to “Logs” and check for any other errors in the top entry.
(It should also be possible to add these to a group and expand the group in the sensor, but I didn’t think of that before I did this.)
I don’t think that this will work in the long run. The last_reset of each of the underlying sensors are not respected this way.
Say your allrum_balcony_switch_energy_kwh “kwh count” gets reset for some reason, then the sum of all kwh will be lower than before but the energy monitor will not see a changed last_reset of the “outer” template sensor and thus very likely be very confused and also probably show incorrect values.
This works in that I get the right value, but it also causes an error in the logs:
TemplateError(‘UndefinedError: ‘mappingproxy object’ has no attribute ‘last_reset’’) while processing template 'Template
I don’t know how to fix that. When I run the code in the developer Template tool I get a valid last_reset for each one of those, and the template does give the right value.
The second issue is that it’s ugly: there are now two lists that have to be kept the same. I’d like to define a variable for the list, but I’m not sure how to do that. If I define it in the “state” template then it is not in scope for the “attributes” one. The “variable” section doesn’t work for templates the way it does for automations/scripts. I tried using an input_text, but it’s limited to 100 characters.
If you have any thoughts on
how to get rid of the Template error,
2 how to make the template more elegant, or,
how important last_reset really is (I’m guessing the Energy display is wrong for one day at worst)
I’d really appreciate it!
I think I figured out how to get rid of the warning. I need to filter the list for items that have the 'last_reset" attribute. This effectively filters out items from the list that are Undefined. This seems to work:
(Filtering the list by selectattr(‘last_reset’) removes all the Undefined entries from the list.)
Nope. I was wrong. That doesn’t cause errors in the Developer template tool, but I still get this error in the HA log:
Template variable warning: ‘mappingproxy object’ has no attribute ‘last_reset’ when rendering
I tried it again, and now I’m getting a different error. It looks like it doesn’t find the attribute when running in the template, because it is now complaining the list is empty when I filter it:
TemplateError(‘ValueError: max() arg is an empty sequence’)
Your earlier posts were for energy (kWh) and noticed your last post was for power (kW). Have you found something that works better for grouping energy sensors or are you still using this?
Power appears to work well as a group summed but using a group or template for energy doesn’t work well as each sensor is reset at different times. This causes the energy total at the end of the day to be wildly inaccurate, especially when the energy group contains ESPHome devices that reset the energy total daily sometimes just seconds after midnight.
Trying the integral integration on the power group. So far it seems accurate.
I don’t have any energy sensors that reset daily so I haven’t run into problems summing either. The only issue I’ve had is that devices with their own internal power integration (energy reports) seem to be better than manually integrating their energy reports because sometimes they don’t report low power usages. (Of course I’m not certain they are integrating them in that case either…)