So, I have a Shelly EM measuring the total consumption of the house on one channel and the solar production on another.
In order to determine what I’m pulling from the grid, I need to subtract what the house is consuming from what the panels are producing… BUT I essentially need it to report 0 if the value is positive and, if negative, report the negative value as positive.
Example 1: I’m producing 2kWh and the house is using 2.5kWh. I’m therefore using 2kWh-2.5kWh = -0.5kWh (I need this to report as positive 0.5kWh).
Example 2: I’m producing 2kWh and the house is using 0.5kWh. I’m therefore using 2kWh-0.5kWh = 1.5kWh (I need this to report as 0).
The code I have at this time is:
template:
- sensor:
- name: Energia Rede
state: "{{ (states('sensor.shellyem_a4e57cbaafa6_channel_2_energy')|float + states('sensor.shellyem_a4e57cbaafa6_channel_1_energy_returned')|float - states('ssensor.shellyem_a4e57cbaafa6_channel_1_energy')|float)}}"
unit_of_measurement: kWh
device_class: energy
state_class: measurement
Can someone help me tweak it to achieve what I need?
Can you clarify what the three sensors are and how they relate to the calculation? Given the screenshot I provided above, you should be able to work out what you need to do with them to make it work.
calculate (solar panels + returned to grid - house usage), let’s call it x
if x is positive, report 0
if x is negative, report -x
Yes? If so, please paste this into Developer Tools / Templates and post a screenshot:
{% set solar = states('sensor.shellyem_a4e57cbaafa6_channel_2_energy')|float(0) %}
{% set returned = states('sensor.shellyem_a4e57cbaafa6_channel_1_energy_returned')|float(0) %}
{% set house = states('sensor.shellyem_a4e57cbaafa6_channel_1_energy')|float(0) %}
{% set x = solar + returned - house %}
x is {{ solar }} + {{ returned }} - {{ house }} = {{ x }}
result is {{ -([x,0]|min) }}