Skip to content

Conversation

@TCWORLD
Copy link
Contributor

@TCWORLD TCWORLD commented Dec 6, 2025

In #27373 the option to track power as well as energy was added. In the case of the Grid and Battery power sources, these are added as a single bi-directional sensor which requires a specific polarity to be meaningful. However not all integrations or hardware use the expected polarity meaning additional template sensors would need to be created to negate these values before they could be used with the energy dashboard.

Proposed change

In this PR, and the associated core PR, we add the option to invert the polarity of a power sensor used for grid/battery/solar power data. This is saved in the preferences as stat_rate_negate alongside with the stat_rate entity id.

In the dialog entries, a check-box is added to select whether to invert the polarity.

Battery power with checked invert option Solar power with unchecked invert option Grid power with unchecked invert option

The scope was limited to these two sources as they are likely to be bidirectional. Solar and individual loads are unlikely to need inverting, though that could be added at a later date.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (thank you!)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

There was a similar proposal to add a direction toggle in the comments of the original PR: home-assistant/core#153809 (comment)

Checklist

  • The code change is tested and works locally.
  • There is no commented out code in this PR.
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

This is a boolean flag which will be used to indicate whether a bi-directional power sensor should be negated if a negative value indicates the wrong direction to expected.
When there is a negate preference present and it is set to true, the value for power retrieved from the entity will be negated (value = 0 - value).
The new form field configures whether a power meter has inverted polarity.
@TCWORLD TCWORLD marked this pull request as ready for review December 7, 2025 09:28
@pergolafabio
Copy link

Can you also do this for Solar? My kWh meter also gives negative values

@karwosts
Copy link
Member

karwosts commented Dec 7, 2025

Can you also do this for Solar? My kWh meter also gives negative values

Suggest to wait for approval of the current before adding anything more. Can always go back and add more when/if the concept is generally approved.

@TheFes
Copy link

TheFes commented Dec 7, 2025

Maybe add the checkbox for both battery power sensors. There are integrations which reverse the logic HA uses, so they are negative when HA expects positive, and the other way around.

Anyway, very nice that you're adding this. Thanks!

Edit: based on the comment above, maybe wait until this one is merged and add that as a follow up :)

@TCWORLD
Copy link
Contributor Author

TCWORLD commented Dec 7, 2025

@pergolafabio This PR is only dealing with the power sensors which use a specific function to access the data. Energy sensors would be possibly more complex, and so ought to be a different FR.

@TheFes There's only one power sensor for the battery as far as I can tell. The other two are energy sensors.

@pergolafabio
Copy link

@pergolafabio This PR is only dealing with the power sensors which use a specific function to access the data. Energy sensors would be possibly more complex, and so ought to be a different FR.

@TheFes There's only one power sensor for the battery as far as I can tell. The other two are energy sensors.

No. Im talking about power sensors, I use a KWh meter for my solar, the kwh meter reports also negative... I had to create a template entity to add solar as power source to make it positive

@TheFes
Copy link

TheFes commented Dec 7, 2025

@pergolafabio kWh is energy, power sensors will be using (k)W.
As to keep referencing kWh, you are causing confusion :)

@pergolafabio
Copy link

Sorry, I'm talking about Watt ...

@TCWORLD
Copy link
Contributor Author

TCWORLD commented Dec 7, 2025

@pergolafabio Ah ok, I was confused as kWh is energy unit.

It should be easy enough to add the checkbox for the solar power sensor as well.

@pergolafabio
Copy link

I use this integration, that power meter reports negative values...
Screenshot_20251207-204430

@pergolafabio
Copy link

@pergolafabio Ah ok, I was confused as kWh is energy unit.

It should be easy enough to add the checkbox for the solar power sensor as well.

Would be great, thnx

To make it clearer what entity it is related to.
Sensor doesn't have to be bi-directional to invert it.
The negate preference had not being used when getting the battery data.
@WillCodeForCats
Copy link

Thank you for this. SolarEdge inverters with an Export+Import meter provide a negative power value when importing from grid and a positive power value when exporting to grid (positive means excess solar generation).

I'd already created a new inverted meter power sensor entity in solaredge-modbus-multi to provide the polarity HA wanted from the integration so that users aren't forced to create template sensors, but I'll hold off on that for a bit.

Some exotic setups may have a negative power value for solar. Given grid and battery have negate options it doesn't seem out of place for solar to have that also.
Copy link
Member

@MindFreeze MindFreeze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this would be very useful but I don't think this is the way to do it. It adds a lot of complexity and fragility to the data handling. I already see that you don't properly handle the polarity of statistics data in the power sources graph, just the latest value, and these kinds of bugs will just keep multiplying every time we add something new about power data.
A more robust approach would be to create new sensors in the energy integration in core. That basically act as the template sensor you would normally create yourself but handled internally.

@MindFreeze
Copy link
Member

The same approach could be used for energy sensors btw. If the user only has a power sensor, the integration can auto create an energy sensor out of it.

@TheFes
Copy link

TheFes commented Dec 8, 2025

@MindFreeze the disadvantage of that, is that you won't have LTS for the new sensor. For my power consumption sensor (from grid) I have years of history. For the newly created template sensor I now use for power production (to grid) I only have history since this month.

@MindFreeze
Copy link
Member

True, but we mostly care about the current value and most recent history. Showing the full power history in the summary is not intended. It just happened because of last minute changes. Power history has performance issues and little utility for users

@compmakers
Copy link

compmakers commented Dec 8, 2025

@pergolafabio This PR is only dealing with the power sensors which use a specific function to access the data. Energy sensors would be possibly more complex, and so ought to be a different FR.
@TheFes There's only one power sensor for the battery as far as I can tell. The other two are energy sensors.

No. Im talking about power sensors, I use a KWh meter for my solar, the kwh meter reports also negative... I had to create a template entity to add solar as power source to make it positive

Hi pergolafabio would it be possible to share your template ? I use the same kWh meter rom Homewizard as well see topic
#28409
It would be very appreciated. (Mijn dank zou zéér groot zijn ;) )

👍😉

@pergolafabio
Copy link

pergolafabio commented Dec 8, 2025

@pergolafabio This PR is only dealing with the power sensors which use a specific function to access the data. Energy sensors would be possibly more complex, and so ought to be a different FR.
@TheFes There's only one power sensor for the battery as far as I can tell. The other two are energy sensors.

No. Im talking about power sensors, I use a KWh meter for my solar, the kwh meter reports also negative... I had to create a template entity to add solar as power source to make it positive

Hi pergolafabio would it be possible to share your template ? I use the same kWh meter rom Homewizard as well see topic https://github.com/home-assistant/frontend/issues/28409 It would be very appreciated. (Mijn dank zou zéér groot zijn ;) )

👍😉

Like this:

  - name: kwh_meter_3_phase_active_power_inverted
    unique_id: kwh_meter_3_phase_active_power_inverted
    state_class: measurement
    unit_of_measurement: 'W'
    state: >
      {{ (states('sensor.kwh_meter_3_phase_active_power') | int) * -1  }}
    availability: >
      {{ states("sensor.kwh_meter_3_phase_active_power") | is_number  }}

@compmakers
Copy link

@pergolafabio This PR is only dealing with the power sensors which use a specific function to access the data. Energy sensors would be possibly more complex, and so ought to be a different FR.
@TheFes There's only one power sensor for the battery as far as I can tell. The other two are energy sensors.

No. Im talking about power sensors, I use a KWh meter for my solar, the kwh meter reports also negative... I had to create a template entity to add solar as power source to make it positive

Hi pergolafabio would it be possible to share your template ? I use the same kWh meter rom Homewizard as well see topic https://github.com/home-assistant/frontend/issues/28409 It would be very appreciated. (Mijn dank zou zéér groot zijn ;) )
👍😉

Like this:

  - name: kwh_meter_3_phase_active_power_inverted
    unique_id: kwh_meter_3_phase_active_power_inverted
    state_class: measurement
    unit_of_measurement: 'W'
    state: >
      {{ (states('sensor.kwh_meter_3_phase_active_power') | int) * -1  }}
    availability: >
      {{ states("sensor.kwh_meter_3_phase_active_power") | is_number  }}

Nice 👍 Thank you and thank you for your very quick reply.

I will start this right now,

Again huge appreciated 😉

@TheFes
Copy link

TheFes commented Dec 8, 2025

You can also create it in the GUI as template helper using this template

{% set source = 'sensor.electricity_meter_energieproductie' %}
{{ states(source) | float * -1 if source | has_value else none }}

The template sensor will be unknown if the source sensor is not available.

@compmakers
Copy link

You can also create it in the GUI as template helper using this template

{% set source = 'sensor.electricity_meter_energieproductie' %}
{{ states(source) | float * -1 if source | has_value else none }}

The template sensor will be unknown if the source sensor is not available.

Thank you TheFes, that worked also great much appreciated 👍😉

@TCWORLD
Copy link
Contributor Author

TCWORLD commented Dec 8, 2025

@MindFreeze I appreciate you checking this. I've added a fix for the statistics issue, you're right I missed that.

While its possible to add extra template sensors to negate the values, be that manually or internally, IMHO this kind of defeats the point. Firstly, I agree with @TheFes that this loses historical information which is somewhat irritating, but it also doubles the amount of statistics data stored because you have the original sensor and the new negated sensor.

I guess the issue really stems from their being no hard and fast rule for how direction is encoded in power data from the myriad of different bits of hardware. IMHO This should realistically be handled internally one way or another, but I'm sure others would disagree.

@MindFreeze
Copy link
Member

I will discuss this with the team but my opinion is that this should be handled with a new sensor. Either core creates and manages it, or we provide an easy UI for the user to create and manage it.
Look at how many cards we have for energy. If/when we have that many for power, they will all have to calculate polarity. This is not only a complexity concern but also affects performance.
Keep in mind that this is not the only case we have to handle. I already mentioned deriving energy when only a power sensor is available, but there is also the common case where input and output power are completely separate sensors. There is no sane way to handle this in frontend. It just isn't sustainable.

@TCWORLD
Copy link
Contributor Author

TCWORLD commented Dec 8, 2025

I totally get where your coming from.

Having said that to a certain extent negating if necessary is realistically for all intents and purposes the same operation as scaling of units (kW -> W) which is already directly handled at the data import stage here.


I wonder if the approach that is needed for adding a new attribute or state information for power sensors that specifies direction. That way the actual sensors themselves are tagged to say what they represent and the negation if needed can be handled more consistently.

That would relieve the need for extra sensors and additional preferences, while also giving a potentially more manageable way to handle these sensors going forward (including for the potential use in integrating power to energy).

Although how you define that attribute is possibly a whole other can of worms. e. g. is it something like relative to the home - true or false. But then there's still no consistent default as we've seen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants