Allow negative power & energy in energy dashboard

Some CT-type power monitors like the Aeotec ZW095 output a negative value for current flowing in the direction opposite the markings on the CT. This is good to handle bidirectional power flow in systems that both produce and consume energy. My electrician strictly followed the instructions in installing both of my monitors so that the one on my breaker panel measuring total consumption reads positive while the one on my solar inverter reads negative power and energy, as in consumption reduction. This is no problem for InfluxDB and Grafana but, while the new energy dashboard will display my consumption, it says that I have no solar generation data, probably because it consists of negative values. I suppose that I could flip around my CTs but there probably are, or will be, other monitors that output signed data so that it would be prudent for the dashboard to handle signed power and energy data.

You could create a template sensor to switch the value from -ve to +ve ?

So a template sensor is where I can do the math. That moved me forward but not all the way because the template sensor I created with the postive value (sensor.generated_energy) computed from the measured negative value (sensor.solar_energy) is not accepted (does not appear in dropdown list) by the energy dashboard. This was my first attempt to create a template sensor so what I finally got to display a positive value in the UI was as follows. If there is a simpler way, let me know.

template:
  - sensor:
    - name: "Generated Energy"
      state: "{{ states('sensor.solar_energy')|float*(-1) }}"
    - name: "Generated Power"
      state: "{{ states('sensor.solar_power')|float*(-1) }}"

Hi Everyone,

I just wanted to share my setup which is working well with the new energy meter to convert power to Energy both from the grid and to the grid. This is with an integration using an eGauge as well as the SolarEdge integration.

(Sensor, Platfom template, sensors:)

      grid_export_power_energy:
        unique_id: grid_export_power_energy
        value_template: >-
          {% set power = states('sensor.total_export') | float %}
          {% if power > 0.0 %} {{ states('sensor.total_export')|float }}
          {% else %} 0.0 {% endif %}
        device_class: Power
        unit_of_measurement: 'W' 
      grid_consumption_power_energy:
        unique_id: grid_consumption_power_energy
        value_template: >-
          {% set power = states('sensor.total_export') | float %}
          {% if power < 0.0 %} {{ (states('sensor.total_export')|float * -1) }}
          {% else %} 0.0 {% endif %}
        device_class: Power
        unit_of_measurement: 'W' 

  - platform: integration
    source: sensor.total_solar_2
    unit_prefix: k
    name: Solar_Production

  - platform: integration
    source: sensor.grid_consumption_power_energy
    unit_prefix: k
    name: Energy_Consumption

  - platform: integration
    source: sensor.grid_export_power_energy
    unit_prefix: k
    name: Energy_Exported

5 Likes

So it looks like maybe the device_class and unit_of_measure need to be specified for the energy add-on to recognize an entitiy as pertaining to it, but doing so in my template sensor declarations in my configuration file doesn’t work. In fact, adding unit_of_measure throws an error.
In what file did you add this code? Is it a script? I am not familiar with the syntax of the first line. Is it part of the code or merely a comment or note?

After still not finding a way to handle negative power in HA 2021.9.2, I found that my Aeotec ZW095 Home Energy Monitor has a parameter that can be set to have the HEM report the absolute value of power and energy. Setting that on the monitor on the output of my old solar inverter now allows me to show solar power in the energy dashboard but that uncovers another limitation: there is no place in the energy dashboard to configure my total load, which is what my other ZW095 on my breaker panel reads. Instead, the only place that load can be specified is under “Electricity grid”, which only allows me to specify the portion of energy drawn from grid along with the energy returned to the grid such as smart meters report.

In the ideal situation, I would have a bidirectional smart meter (that reports both directions of power flow as positive numbers) and a PV inverter both of which I could read into HA, but I have older equipment so I added monitors that simply measure total consumption and total production separately. With all of the energy monitors, smart meters, smart meter interfaces, and battery systems out there, I think there are many home energy systems that don’t fit the present model of the HA Energy Dashboard. I know it will complicate things for the developers but I request that the dashboard be modified to provide a total, in contrast to net, load meter and to handle signed power and energy readings everywhere.

If I add my total load monitor as “Grid consumption” and my solar production monitor as “Return to grid”, the dashboard computes everything assuming the total consumption is the sum of these two, which is wrong because a portion of my total load was met by my solar production.

1 Like

There is a change in 2021.9.3

Hey have you had anymore luck? I’ve recently went on a journey as my situation is somewhat similar to yours. My CT Clamps read positive when energy is flowing from the grid, and they go negative when feeding the grid. So I did this:

- platform: template
  sensors:
    # Negative values only - if not negative return 0, otherwise provide "Return to Grid" Value
    house_energy_negative:
      unique_id: house_energy_negative
      unit_of_measurement: 'W'
      value_template: >
        {% if states('sensor.energy_monitor_total_house_watts') | int > 0 %}
          0
        {% else -%}
          {{ (states('sensor.energy_monitor_total_house_watts') | float) | abs }}  
        {% endif %}      

    # Positve values only - if negative, return 0, otherwise provide "Power from Grid" Value
    house_energy_positive:
      unique_id: house_energy_positive
      unit_of_measurement: 'W'
      value_template: >
        {% if states('sensor.energy_monitor_total_house_watts') | int > 0 %}
          {{ states('sensor.energy_monitor_total_house_watts') }}
        {% else -%}
          0
        {% endif %}
    # Add all the watts used by house (which goes negative) with total solar generated to get total house used metric
    house_energy_used_total:
      unique_id: house_energy_total
      unit_of_measurement: 'W'
      value_template: "{{ states('sensor.energy_monitor_total_house_watts') | float + states('sensor.energy_monitor_total_solar_watts') | float }}"


Then I used this: Integration - Riemann sum integral - Home Assistant

to get kWh to feed the Energy dashboard basically, positive values, return to grid values.

- platform: integration
  source: sensor.house_energy_negative
  name: energy_spent_energy_backtogrid
  unit_prefix: k
  round: 2

- platform: integration
  source: sensor.house_energy_positive
  name: energy_spent_energy_positive
  unit_prefix: k
  round: 2

i think this should work to be accurate. I still feed the solar data in the normal way without doing anything special, just the kwh generated by my solar enphase device.

3 Likes

I think the most ideal situation would be to simply have the option to select, if a value goes negative, apply that value as ‘return to grid’

5 Likes

I got negative number when pulling energy from grid, it used to work OK till latest updates, but now I get error in Energy configuration page saying that figure is negative. Strange
Also some MODBUS figures gone unavailable, when it was OK before…
BUGS BUGS (((

This worked perfectly for me. I just had to use lowercase “power” for device_class.

Thank you so much!

I like the workarounds, but I agree with the OP. The energy dashboard should not have been designed such that it specifically disallowed and discarded negative energy values. I’d understand a warning that maybe I didn’t have my sensor correctly configured, but I should be allowed to use a negative value if i want to. I am net-metered, and excess energy produced by my solar system flows “backwards” through my mains back to the grid. I don’t want this backwards flow to be counted as consumption, when in reality, it is NEGATIVE consumption. I’m curious as to how things are wired in other countries. Here in the US, and at least in AZ, i’d venture to bet that most configurations are similar to mine, where the solar system ties into a breaker on the “house” side of my meter. kWh counts up at night (positive W values) when my solar system isn’t making any power, and counts down (negative W values) during the day when solar production exceeds house draw.

2 Likes

I have the same situation. Everything works great until the sensor reports a negative value. It would be nice if this would just be allowed.
Flow when sensor value is always positive: image
And when the sensor value goes negative because I eventually produced more than I used I see: image

Also trying to get the energy dashboard working. I use an IoTaWatt, and have my utility CT separate values for Import and Export; Export is negative per electrical conventions; likewise solar is negative since it is a source.

Is this not supported directly-- do I need to create a separate sensor that tracks the absolute value of the sensors?

It hurts my brain to see it that way, but if that is the only solution I will consider it.

Hi, Im trying to do something very similar, and have become stuck. Ive tied to sue the information above to put into my configuration.yaml, and some parts work, but others dont.

Here is the extract from that file. the net_elec_consumption and energy_spent components work great. These calculate the net energy Im using at a point in time and cumulatively. However the house_energy_export and energy_exported dont work at all, and I cant figure out why. The first is meant to just show the energy exported (the -ve components of net_elec_consumption), and the latter the cumulative amount of energy exported. The house_energy_export doesnt appear as an entity, and energy_exported is undefined (presumably because the sensor it is using to integrate hasnt been created).

I am trying to learn the syntax of yaml, and what goes into the different sensor and platform sections of configuration.yaml, but find it unclear, and I still havent found anywhere that explains these things clearly (if anyone has suggestions that would be great).

Does anyone have any suggestions on what I should do differently to get the below code to work?

Thanks for your help

Jon

sensor:
  - platform: integration
    source: sensor.net_elec_consumption
    name: energy_spent
    unit_prefix: k
    round: 2
    
  - platform: integration
    source: sensor.house_energy_export
    name: energy_exported
    unit_prefix: k
    round: 2
    
template:
  - sensor:
      - name: "net elec consumption"
        unit_of_measurement: "w"
        state: >
          {% set consumption = states('sensor.envoy_xxx_current_power_consumption') | float %}    
          {% set production = states('sensor.envoy_xxx_current_power_production') | float %}

          {{ (consumption - production) | round(1, default=0) }}
          

    # Negative values only - if not negative return 0, otherwise provide "Return to Grid" Value
  - sensor:
      - name: "house energy export"
        unit_of_measurement: "w"
        state: >
          {% if states('sensor.net_elec_consumption') | int > 0 %}
            0
          {% else -%}
            {{ (states('sensor.net_elec_consumption') | float) | abs }}  
          {% endif %}      

Just wondering if anyone ever ended up having any luck with fixing this issue? HA 2023.1.4 now throwing lots of warning for using negative energy values. TIA

There are different kind of setups how this can be done - and where your measurement could be done.

Here in Germany, we usually have a two direction meter if you run a Solar Installation.
This meter is counting your Grid import and your Grid export - which is required, since we get payed for each exported kWh.

Anyway - I cannot (yet) use this meter in HomeAssistant (on an easy way) - so I am using the meters from my Solar Inverter itself.
Usually, the Inverter (SolarEdge) does only report the production values - but it does not know how much Energy is comming from the Grid - or going to the grid, neither - how much energy you consume from your production.

Therefore, most Vendors do have power-Meters that can be included into the system - and should provide the relevant information.
But there are also other ways to measure these …

If you already Receive NEGATIVE values (but can’t add them into HA) - then I would just create a templates where you apply rules like this (simplyfied)

IMPORT SENSOR:
if Meter_value > 0
value = Meter_value
else unavailable

EXPORT SENSOR:
if Meter_value < 0
value = Meter_value * -1
else unavailable

I haven’t yet tested if such a configuration is possible - and unfortunately, I can’t realy do such a test because I don’t have such sensors … but maybe I can do some tests on one of my test setups within the next days…

No progress here.
Having the same issue with negative value.

Entity has a negative state.
The following entities have a negative state
while a positive state is expected:
sensor.home_energy_meter_gen5_electric_consumption_kwh

Solar production is higher than consumed energy.

Create a helper of a template sensor, with a ‘state:’ that is the sensor mulitplied by -1. This will give convert your negative sensor into a positive value sensor (i.e. -100 x -1 = 100)