I’ve been trying to create a sensor to use in the Energy management dashboard and can’t seem to be able to create the final piece.
I have a Shelly EM with one coil on my solar production and the other on the input to the consumer unit, which gives me total used.
From these two inputs I’ve calculated the ‘Using from Grid’ using a template and integration sensor
'template:
-
sensor:
- name: “Grid”
unit_of_measurement: “W”
device_class: power
state_class: measurement
state: “{{ (states(‘sensor.2_solar_channel_2_power’) | float) - (states(‘sensor.2_solar_channel_1_power’) | float)}}”
- name: “Grid”
-
platform: integration
source: sensor.grid
name: grid_kwh
unit_prefix: k
round: 2
method: left’
the Integration is what I use in the energy dashboard
Now in order to create a sensor to use for return to grid, I thought that I can take the Solar-Total Consumption=Solar return, the issue being when the Solar is less than the Total Consumption, the value of the sensor is negative. How can I create a template sensor that only considers positive values?
I’ve tried all sorts. | abs just turns the value from a negative to a positive and I can’t seem to get this approach working either
'- sensor:
- name: “Grid return calc”
unit_of_measurement: “W”
device_class: power
state_class: measurement
state: “{{ (states(‘sensor.2_solar_channel_1_power’) | float) - (states(‘sensor.2_solar_channel_2_power’) | float)}}”
- sensor:
- name: “Grid return”
unit_of_measurement: “W”
device_class: power
state_class: measurement
state: >
{% set x = state(‘sensor.grid_return_calc’) | float %}
{% if is_number(x) and x | float > 0 %}
states(‘sensor.grid_return_calc’) | float )
{% endif %}’
Can anyone help please?