I’m quite sure that you can not just add the energy from the 3 phases. You have to divide the result with SQRT(3) to get the actual amount that the electrical company registers (note that this is a good thing, less energy spent!).
Here’s my calculation:
consumed_energy:
friendly_name: "Total Consumption"
device_class: energy
unit_of_measurement: 'kWh'
value_template: "{{ ((states('sensor.em_channel_a_energy')|float + states('sensor.em_channel_b_energy')|float + states('sensor.em_channel_c_energy')|float)/sqrt(3))|round(2) }}"
availability_template: "{{ not ( is_state('sensor.em_channel_a_energy', 'unavailable') or is_state('sensor.em_channel_b_energy', 'unavailable') or is_state('sensor.em_channel_c_energy', 'unavailable') ) }}"
does anyone have this working with the MQTT based setup (mqtt shelly discovery script)? I’m trying to backport your findings to it, but it isn’t exactly straight forward i found
i use this:
- sensor:
- name: "House Energy kWh"
unit_of_measurement: "kWh"
state_class: measurement
device_class: energy
state: >
{{ [ states('sensor.shelly_3em_meter_total_0'),
states('sensor.shelly_3em_meter_total_1'),
states('sensor.shelly_3em_meter_total_2') ]
| map('float') | sum }}
attributes:
last_reset: '1970-01-01T00:00:00+00:00'
availability: >
{{ not 'unavailable' in
[ states('sensor.shelly_3em_meter_total_0'),
states('sensor.shelly_3em_meter_total_1'),
states('sensor.shelly_3em_meter_total_2') ] }}
but i get those zero values after HA is reloaded anyhow
Thanks for the code in this thread! I managed to sum up all my shelly devices to bigger consumption units (i.e. the 3 Phases of the wall connector or all lights). However when I use the energy entity generated with the code from the first post, on day 1 it will show the entire energy of the specific devices since they were installed. On day two it is only the daily consumption. This only happens to the entities that are added up. Is there a way I can tell home assistant to start monitoring energy from a specific day and forget about energy that was monitored before that date?
I use shelly4hass and customized my energy entities to be visible in the energy section of home assistant.
all right… so not only it’s not working as expected, as i still see deep to zero instead of going into unavailable … but we now also get this in the logs
2021-10-07 12:39:40 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'float' got invalid input 'unavailable' when rendering template '{{ [states('sensor.shelly_3em_meter_total_0'),
states('sensor.shelly_3em_meter_total_1'),
states('sensor.shelly_3em_meter_total_2') ]
| map('float') | sum }}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2021.12
Version 2021.10.0 now requires that, for certain functions such as float, the default value is explicitly specified. If it’s missing, it’s reported as a warning message. In the December release, it will become an error message.
Change it from this:
map('float')
to this:
map('float', 0)
If float is unable to convert a value, it will report 0 for that value. If you want it to report something else, just change the 0 to whatever you prefer.
Hi @amitfin . Thanks for your post, I’m new to HA and I’m trying to configure my shelly 3em into HA. I defined the sensor into a sensor.yaml file where I’ll put all my virtual sensors, them I recall this file into configuration.yaml with template: !include sensors.yaml, I check the configuration and it’s valid. Anyway when I go to energy section for adding the consumptions I still find the three sensors for each faces and not the overall energy sensor created. Can please you tell me why? Many thanks for your time and patience
That is likely the case, when I first tested it it looked like the sum needed to be divided by srt3 and on the Shelly forum there was also an explanation to this. However since I now have removed my unit I can not confirm either anymore so stick with just adding if that gives you the same values as your electric company debit you.
@Spitfire, the 3EM device itself reports the total energy consumption. It’s only the home assistant integration that is missing this total sensor.
Here is a side-by-side screenshot of our September numbers from the 3EM itself and the simple sum in home assistant.
As can be seen, the numbers are aligned (and dividing it by SQRT(3) would make them different):
@genga91, I think that the configuration should be placed in template.yaml and not sensor.yaml.
I’m not using “!include” directive in our config, so it’s easy to see the hierarchy (think about “!include” as dumping the content of the other file in the place you want to.)
I moved into the configuration file directly and it’s working well know. Don’t understand why it doesn’t if I make a new file into config folder and then include that file into configuration… Thanks for your help anyway