Hi @uksa007,
I just would like to thank you very much for your contribution / code.
I came across this post by chance yesterday and can only say: Awesome! It works wonderful!
This is the first time the energy panel works like it should
My devices:
HA: Home Assistant 2022.11.2
Shelly 3EM FW: 20221027-110030/v1.12.1-ga9117d3
Shelly Plug S FW: 20221027-101131/v1.12.1-ga9117d3
My setup:
Feeding a mini solar system via L2 (named “phase 2” in the further course). The measured power of the solar system is measured by the Shelly Plug S.
The code looks like this (configuration.yaml):
- platform: template
sensors:
# Template sensor for values of power import (active_power > 0)
power_import:
friendly_name: "Power Import"
unit_of_measurement: 'W'
device_class: power
value_template: >-
{% if (states('sensor.phase_1_power')|float + states('sensor.phase_2_power')|float + states('sensor.phase_3_power')|float) > 0 %}
{{ states('sensor.phase_1_power')|float + states('sensor.phase_2_power')|float + states('sensor.phase_3_power')|float }}
{% else %}
{{ 0 }}
{% endif %}
availability_template: "{{
[ states('sensor.phase_1_power'),
states('sensor.phase_2_power'),
states('sensor.phase_3_power')
] | map('is_number') | min
}}"
# Template sensor for values of power export (active_power < 0)
power_export:
friendly_name: "Power Export"
unit_of_measurement: 'W'
device_class: power
value_template: >-
{% if (states('sensor.phase_1_power')|float + states('sensor.phase_2_power')|float + states('sensor.phase_3_power')|float) < 0 %}
{{ (states('sensor.phase_1_power')|float + states('sensor.phase_2_power')|float + states('sensor.phase_3_power')|float) * -1 }}
{% else %}
{{ 0 }}
{% endif %}
availability_template: "{{
[ states('sensor.phase_1_power'),
states('sensor.phase_2_power'),
states('sensor.phase_3_power')
] | map('is_number') | min
}}"
# Template sensor for values of power consumption
power_consumption:
friendly_name: "Power Consumption"
unit_of_measurement: 'W'
device_class: power
value_template: >-
{% if (states('sensor.power_export')|float(0)) > 0 and (states('sensor.shellyplug_s_b4c105_power')|float(0) - states('sensor.power_export')|float(0)) < 0 %}
{% elif (states('sensor.power_export')|float(0)) > 0 and (states('sensor.shellyplug_s_b4c105_power')|float(0) - states('sensor.power_export')|float(0)) > 0 %}
{{ (states('sensor.shellyplug_s_b4c105_power')|float(0)) - states('sensor.power_export')|float(0) }}
{% else %}
{{ states('sensor.power_import')|float(0) + states('sensor.shellyplug_s_b4c105_power')|float(0) }}
{% endif %}
Previously I had my configuration (unsatisfactory) as follows:
- name: "Energy Total"
unique_id: energy_total
state: >-
{{ states('sensor.phase_1_energy')|float(0) +
states('sensor.phase_2_energy')|float(0) +
states('sensor.phase_3_energy')|float(0) }}
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
attributes:
last_reset: "1970-01-01T00:00:00+00:00"
availability: >
{{ not 'unavailable' in
[ states('sensor.phase_1_energy'),
states('sensor.phase_2_energy'),
states('sensor.phase_3_energy') ] }}
- name: "Power Total"
unique_id: power_total
state: >-
{{ (states('sensor.phase_1_power')|float(0) +
states('sensor.phase_2_power')|float(0) +
states('sensor.phase_3_power')|float(0) |round(1)) }}
unit_of_measurement: W
device_class: power
state_class: measurement
availability: >
{{ not 'unavailable' in
[ states('sensor.phase_1_power'),
states('sensor.phase_2_power'),
states('sensor.phase_3_power') ] }}
The only question I have on this and maybe you can help me on this.
My Wi-Fi has a night shut-off. That means it is switched off (on weekdays) from 11pm to 5am.
With the previous code, the consumption from the Shelly 3EM was still fed into HA when the WLAN was activated. I then had a total consumption of the whole night in the morning. This is also completely correct.
With your code, the consumption is only displayed from the period in which the WLAN is active again. I think this is because the saved data is not taken over by the Shelly 3EM.
Do you have an idea for this?