Create virtual sensor for grid usage

Hi,

I have a clamp sensor on my power usage, and a readout of generation from my sma inverter.

But currently it adds them to gether for the home usage, where as i need to create a virtual sensor that takes the clamp reading and minuses the solar output to get what is being taken from or fed back to the grid…

How can i do that??? (PS, complete noob new to Home Assistant)

I have the same setup and the same problem!

hahaha… i seen a post and was like YESSSS… but nooo… it would seem to be easy… new virtual sensor that = a - b

ive managed to create the net sensor and give it an entity id and a device class… but it still cannot be selected in the energy dashboard…

I just need help to make it get into the dashboard. It shows as a power sensor in my normal sensors area…

template:
  - sensor:
      - name: "Net Power Usage"
        unit_of_measurement: "kWh"
        unique_id: sensor.calculated_netpowerusage
        device_class: energy
        state: >
          {% set usage = states('sensor.power_meter_electric_consumption_kwh') | float %}
          {% set generation = states('sensor.sb6_0_1av_41_598_pv_gen_meter') | float %}

          {{ (usage - generation + 37820) }}

Ok, ive gotten closer… see code below. My issue now is that it expects a positive for grid consumption…

so on a day that i have exported more then i have used, the virtual sensor is negative… i need to work out the maths etc to only count up not down. and then create another one that does the opposite for sold back to the grid…

ORRR, if its negative to only report zero as a rule???

template:
  - sensor:
      - name: "Net Power"
        unit_of_measurement: "kWh"
        unique_id: sensor.netpower
        device_class: energy
        state_class: total_increasing
        state: >
          {% set usage = states('sensor.power_meter_electric_consumption_kwh') | float %}
          {% set generation = states('sensor.sb6_0_1av_41_598_pv_gen_meter') | float %}

          {{ (usage - generation + 37820) | round(4) }}

OK, worked out a solution. The devices also create sensors for Watts (power).

So by doing the same maths equations i can work out the instantaeous power being consumed from the grid or sent back to the grid. I then used the Integration - Riemann sum integralto create a energy sensor for both and then used that in the energy tab for solar to grid and power used from grid…

Took some googling and sooo many reloads, but i got it and so far happy, not sure of the accuracy, but a good overlook…

Code in config yaml…

template:
  - sensor:
      - name: "Used Watts"
        unit_of_measurement: "W"
        unique_id: sensor.actualwattsused
        device_class: power
        state_class: measurement
        state: >
          {% set usage = states('sensor.power_meter_electric_consumption_w') | float %}
          {% set generation = states('sensor.sb6_0_1av_41_598_grid_power') | float %}
          {% if (usage - generation) < 0 %}
             0
          {% else %}
             {{ (usage - generation) | round(2) }}
          {% endif %}

      - name: "Exported Solar"
        unit_of_measurement: "W"
        unique_id: sensor.actualwattssold
        device_class: power
        state_class: measurement
        state: >
          {% set usage = states('sensor.power_meter_electric_consumption_w') | float %}
          {% set generation = states('sensor.sb6_0_1av_41_598_grid_power') | float %}
          {% if (generation - usage) < 0 %}
             0
          {% else %}
             {{ (generation - usage) | round(2) }}
          {% endif %}

1 Like