Looking for help on power calculation with negative values

Hi,

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
    sensors:
    power_from_grid:
    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:

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

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

2 Likes

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