Custom Sensor for Solar Consumption

I’m trying to create a sensor in Watts that will measure my solar consumption.
I have my SolarEdge current power sensor, and my house total consumption (includes solar and grid if I’m using any).

I keep running into errors when trying to implement an if statement so that it only reports the amount of solar I’m using, not any extra that is being sent back to the utility company.

Im sure theres either a formatting or syntax that I’m missing, or perhaps you cannot use an if statement in this kind of sensor (which im really hoping isn’t the case).

template:
  - sensor:
      - name: solar consumption test
        unique_id: Solar_Consumption_Test
        state_class: measurement
        unit_of_measurement: W
        device_class: power
        icon: mdi:transmission-tower
        state: >
          {% if states('sensor.solaredge_current_power') | float = 0 %}
              {{ "0" }}
          {% elif states('sensor.solaredge_current_power') | float < states('sensor.home_energy_meter_gen5_electric_consumption_w') | float %}
              {{ states('sensor.solaredge_current_power') | float }}
          {% elif states('sensor.solaredge_current_power') | float => states('sensor.home_energy_meter_gen5_electric_consumption_w') | float %}
              {{ states('sensor.home_energy_meter_gen5_electric_consumption_w') | float }}
          {% else %}
              {{ "0" }}
          {% endif %}
        attributes:         
          last_reset: '1970-01-01T00:00:00+00:00'

Any assistance, or examples from someone who has got this working in the past would be greatly appreciated.

Hard to help without the exact details.

A few things though:

  • your one condition’s => should be >=
  • provide a default value for float in case the sensor is unavailable
  • you don’t strictly need the quotes around the zeroes

Your right, i meant to post this at the bottom and totally forgot.

This is the error im receiving when i restart the system.

Logger: homeassistant.config
Source: config.py:929
First occurred: September 3, 2022 at 9:43:09 PM (1 occurrences)
Last logged: September 3, 2022 at 9:43:09 PM

Invalid config for [template]: invalid template (TemplateSyntaxError: expected token ‘end of statement block’, got ‘=’) for dictionary value @ data[‘sensor’][0][‘state’]. Got "{% if states(‘sensor.solaredge_current_power’) | float = 0 %}\n 0 \n{% elif states(‘sensor.solaredge_current_power’) | float < states(‘sensor.home_energy_meter_gen5_electric_consumption_w’) | float %}\n states(‘sensor.solaredge_current_power’) | float \n{% elif states(‘sensor.solaredge_current_power’) | float => states(‘sensor.home_energy_meter_gen5_electric_consumption_w’) | float %}\n states(‘sensor.home_energy_meter_gen5_electric_consumption_w’) | float \n{% else %\n 0\n{% en… (See /config/configuration.yaml, line 78).

In the first if you’re using an assignment operator and not a comparator. Change = to == (and implement the other changes suggested). Also, where the output is a literal (zero), you don’t need the template blocks. Just put 0 (no quotes needed either).

Thanks for the tips, with those i was able to get it to work with the below.

  - sensor:
      - name: solar consumption test
        unique_id: Solar_Consumption_Test
        state_class: measurement
        unit_of_measurement: W
        device_class: power
        icon: mdi:transmission-tower
        state: >
          {% if states('sensor.solaredge_current_power') | int < states('sensor.home_energy_meter_gen5_electric_consumption_w') | int %}
              {{ states('sensor.solaredge_current_power') | int }}
          {% elif states('sensor.solaredge_current_power') | int >= states('sensor.home_energy_meter_gen5_electric_consumption_w') | int %}
              {{ states('sensor.home_energy_meter_gen5_electric_consumption_w') | int }}
          {% else %}
               0
          {% endif %}
        attributes:         
          last_reset: '1970-01-01T00:00:00+00:00'