Hi All

Im a trying to combine a few sensors using a value template but stuck. Simple + and - I can do but I have an issue with a sensor that changes between + and - figures

Basically this is to calculate the total energy usage of my house based on some sensors as follows:

• sensor.solax_inverter_power
Is always positive as its the power being supplied by the inverter and equal (ish) to the amount the house is using unless it cannot supply the required amount which then the house draws from the grid

• sensor.solax_measured_power
It fluctuates between positive & negative (CT clamp on the incoming supply) but I only want to know when it is negative as this means I am drawing from the grid

So, I have setup a few templates but with no sucess and cant get the equasion right due to the measured power sensor being either positive and negative

How to I create the value template that if sensor.solax_measured_power is positive it ignores it and if it is negative it adds it to the positive?

Am I going to have to do it in a few steps/sums like below

Lets say inverter_power = 1000 and measured_power > -500
the sum would be 1000 - -500 = 1500w

but lets say measured power is +500
the sum would be 1000 - +500 = 500 which is wrong as it should be 1000 as the 500 is being pushed to the grid as it +

(edited, typed wrong figures not 1000 + 500 = 1500 as per original post - Thanks Petro)

My current value_template (below) is correct when measured_power is negative but wrong when positive:

``````- platform: template
sensors:
electricity_usage_tock:
friendly_name: "Total Electricity Usage"
unit_of_measurement: 'W'
value_template: "{{ (states('sensor.solax_inverter_power') | int) - (states('sensor.solax_measured_power') | int) }}"
``````

The question is, How do I ignore > 0 figure on the measued_power but include < 0 figure in the value_template

I am sure its the the â€śintâ€ť but need some advise

I donâ€™t follow your logic at all. The way you describe it, a minus sign should work for everything.

you have a minus sign. The resulting equation is 1000 - -500 = 1500

Here you flip the sign to positive on the 500, but instead you add the result. If you keep the minus sign constant. The actual equation is 1000 - +500 = 500. So this whole part is confusion because youâ€™re all over the place.

Are you just trying to throw out the measured power if itâ€™s greater than zero?

If thatâ€™s the case just do an if statement.

``````- platform: template
sensors:
electricity_usage_tock:
friendly_name: "Total Electricity Usage"
unit_of_measurement: 'W'
value_template: >
{% set measured_power = states('sensor.solax_measured_power') | int %}
{% if measured_power > 0 %}
{{ states('sensor.solax_inverter_power') }}
{% else %}
{{ states('sensor.solax_inverter_power') | int - measured_power }}
{% endif %}
``````

@petro

Sorry for the confusion, I see I had my sums wrong in the post. In my head and on paper it was right but I typed it wrong. You are correct 1000 - +500 = 500 not as I had

Simple answer, Yes I am trying to ignore/trow out any + figure from the measured power as this is to the grid and is excess

Let me test and will come back

@petro

That was spot on, Many Thanks

``````- platform: template
sensors:
electricity_usage_tock:
friendly_name: "Total Electricity Usage"
unit_of_measurement: 'W'
value_template: >
{% set measured_power = states('sensor.solax_measured_power') | int %}
{% if measured_power > 0 %}
{{ states('sensor.solax_inverter_power') }}
{% else %}
{{ states('sensor.solax_inverter_power') | int - measured_power }}
{% endif %}
``````
1 Like