Looking for help on power calculation with negative values


I have 3 sensors that i have setup that will do calculations to work out my power to the grid, power from the grid and power from solar (generated).

At present the power from the grid will be positive till the solar is producing the same or more than required. I have a sensor that does the following:

  • platform: template
    value_template: “{{ states(‘sensor.power_consumed’) | float | round(0) - states(‘sensor.growatt_total_output_power’) | float | regex_match(’[^0-9]+’) }}”
    friendly_name: “Power From Grid”
    unit_of_measurement: “W”

The goal is to take my consumption value and subtract the solar generated value. Issues is when the solar is generating more then consumed this sensor value becomes a negative value, what i would like is for this sensor to have a postitive value when it is positive of course BUT when the value goes negative i want it to report 0.

Anyway i can do this at all?

Here’s what i use:

        unit_of_measurement: 'W'
        value_template: >
          {% if states('sensor.e3dc_net_power') | int > 0 %}
            {{ states('sensor.e3dc_net_power') }}
          {% else -%}
          {% endif %}

And please, if you post code, format it properly (point 11)


Excellent, that worked, sorry about the format i pasted straight from the template tester in development tools.