Energy Dashboard Configuration with Sensors

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)}}”
  • 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?

UPDATE AND SOLUTION

As I said in the initial question to the comunity, I have a Shelly monitoring consumption on my consumer board and one monitoring the generation from my solar array.

I’ve created a template sensor to calculate the total power consumed from the Grid
Grid = Total consumption (Shelly 1) - Solar Power (Shelly 2)

'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)}}”’

I’ve then created a Integration Sensor so that I can use this in the energy Dashboard

‘- platform: integration
source: sensor.grid
name: grid_kwh
unit_prefix: k
round: 2
method: left’

The second Sensor that I was having trouble in creating was the Grid Return Sensor, How much power am I pushing back to the grid from the solar array. Again I created a sensor

‘- sensor:
- name: “Grid Return”
unit_of_measurement: “W”
device_class: power
state_class: measurement
state: >
{% if states(“sensor.grid”) | float >=0 %}
0
{% else %}
{% set returned = states(“sensor.2_solar_channel_1_power”) | float - states(“sensor.2_solar_channel_2_power”) | float %}
{{ returned }}
{% endif %}’

The issue that I was having is that the Energy Dashboard doesn’t recoginse negative values So here I have used the logic

If the Grid Power is greater than Zero, I’m using power from the grid and also whatever solar I am generating, so the return to Grid is Zero

If the Grid Power is Less than Zero, then I am generating more power than I’m using so do the calculation Solar Power-Total Consumption = Grid Returned. This returns a positive value

I can then use this sensor as part of the integration sensor

'- platform: integration
source: sensor.grid_return
name: grid_return_kwh_new
unit_prefix: k
round: 2
method: left ’

Works perfectly

1 Like

Hello, I want to create the same sensor but I don’t know how could you help me.