update: mostly fixed the problem with
“if is_number(‘sensor.entity’) else none.” No more corrupt db at restarts, only when restoring HA full backups.
Hello. Got myself this device, Shelly EM to read my solar inverter pv energy and power. Channel 1 reads solar inverter grid input power and energy, channel 2 reads its 230v output, meaning solar+the grid input red by ch1.
Integrating it to HA was a breeze. Getting ch2-ch1 as solar production energy meter and playing nice with ha energy feature was anything but. At power loss, at ha server restart, and when generally messing with Home Assistant upgrades and stuff like that, all the kwh values will sum on a single day, while the past data is gone. Managed to get it back by losing a day or two but now i’m digging thru 1 week snapshots.
This is my adapted code from some example on the internet.
template:
- sensor:
- name: "solar_power"
unit_of_measurement: "W"
icon: hass:nature
device_class: power
state: >
{% set iesire_w = states('sensor.shellyem_98cdac1f3e9e_channel_2_power') | float %}
{% set intrare_w = states('sensor.shellyem_98cdac1f3e9e_channel_1_power') | float %}
{{ ((iesire_w - intrare_w) - 20) | round(1) }}
- sensor:
name: "solar_energy"
unit_of_measurement: "kWh"
device_class: energy
icon: hass:leaf
state: >
{% set iesire_kwh = states('sensor.shellyem_98cdac1f3e9e_channel_2_energy') | float %}
{% set intrare_kwh = states('sensor.shellyem_98cdac1f3e9e_channel_1_energy') | float %}
{{ ((iesire_kwh - intrare_kwh)) | round(1) }}
utility_meter:
energy:
source: sensor.solar_energy
name: "productie_solara"
cycle: yearly