Only display negative values of a sensor

Hi Folks

I am quite new to HA since a few months and am slowly adding to it. I started with monitoring my solar inverters and now I added a Shelly Pro 3EM to monitor energy consumption and production of the whole house.

Shelly 3EM displays the phases without any relation to each other, but here in Switzerland, the 3 phases get counted against each other (don’t know the exact term in english, in German it is “saldieren”) so if I use 100W on Phase 1 and produce 100W on Phase 2, my net consumption is 0.

I added a Helper to sum up the 3 phases which works, so i see my current energy consumption but to correctly calculate my energy consumption and production, especially the percentage of solar energy I use myself vs. what I feed into the grid I need an additional utility meter sensor that only counts if I feed back into the grid.

What I want is a helper sensor that displys 0W when I consume from the grid and displays the feedback power when I feed back to the grid.

Grid consumption 200W → Helper 0W
Grid consumption -200W → Helper 200W

I tried to use the template from this topic: Sum Negative Values
Adding the template sensor via the GUI → Helpers → Template Sensor but the value is “unavailable” after saving.

Any help highly appreciated.

Use a template sensor helper with a state template of:

{{ -((0, states('sensor.ENTITY_ID')|float(0))|min) }}

Here’s a breakdown of that template:

Sensor state is a string, convert it to a number and use 0 if anything goes wrong:


If we call that x, next step is to find the smaller of 0 and x to only consider negative values:

(0, x)|min

and finally, multiply by -1:

{{ -( (0, x)|min ) }}

@Troon Thank you for your reply and especially for explaining the code. I am trying to learn but often the solutions just provide a naked code without any explanation.

Your solution works like a charm. Because of bad weather I didn’t have any surplus solar energy to test it but I created a manual test instance with a number slider and it showed the correct values so I guess it also works with the grid power sensor.

1 Like