I faced an issue with the utility_meter when the shelly em is powered down for a while. In that moment, the energy read by hass is 0 and then when the device is power up again recover the latest value:
template:
- sensor:
- name: "Exported energy"
unit_of_measurement: 'kWh'
device_class: energy
state_class: total_increasing
availability: >-
{{ states('sensor.iammeter1_exportgrid') not in ['unavailable', 'unknown', 'none', 0] }}
state: >-
{{ states('sensor.iammeter1_exportgrid')|float }}
…and an example of my calculated ones:
- sensor:
- name: "Self used energy (daily)"
unit_of_measurement: 'kWh'
device_class: energy
state_class: total_increasing
availability: >-
{{
states('sensor.yield_energy_daily') not in ['unavailable', 'unknown', 'none']
and
states('sensor.exported_energy_daily') not in ['unavailable', 'unknown', 'none']
}}
state: >-
{{
0 if states('sensor.yield_energy_daily')|float(0) == 0
else
states('sensor.yield_energy_daily')|float -
states('sensor.exported_energy_daily')|float
}}