Net Power Calculation - Template Sensor

I am using the Enphase sensor to monitor my solar production and consumption.
I need a ‘power produced’ - ‘power consumed’ sensor in order to control high power devices based on grid export/import.

I have attempted to rejig the config in Newbie question - subtract one sensor value from another for my purposes and failed miserably.

if anyone fancies a giggle checking out my comedy yaml i’d appreciate a little help!

With thanks.

I have added this to my configuration.yaml

  - platform: template
    name: NetPower
    sensors:
      netpowercalc:
      value_template: '{{(states.sensor.envoy_current_energy_production.state | float ) - (states.sensor.envoy_current_energy_consumption.state | float)}}'
      friendly_name: “Net Power”

and i’m getting the following on check config.

Invalid config for [sensor.template]: expected a dictionary for dictionary value @ data[‘sensors’][‘friendly_name’]. Got ‘“Net Power”’
expected a dictionary for dictionary value @ data[‘sensors’][‘netpowercalc’]. Got None
expected a dictionary for dictionary value @ data[‘sensors’][‘value_template’]. Got ‘{{(states.sensor.envoy_current_energy_production.state | float ) - (states.sensor.envoy_current_energy_consumption.state | float)}}’. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/

:grimacing:

  - platform: template
    sensors:
      netpower:
        value_template: '{{(states.sensor.envoy_current_energy_production.state | float ) - (states.sensor.envoy_current_energy_consumption.state | float)}}'
        unit_of_measurement: 'W'
        friendly_name: Net Power

in answer to my own question!
one for the noobs, i got there in the end.
perseverance!

2 Likes

Thanks heaps. Exactly what i needed.

1 Like

I have now updated the code as the previous method is no longer recomended according to this page

Legacy Sensor configuration format

I followed the latest advice at
CONFIGURATION VARIABLES

Here is the updated code which also removes the decimal point

template:
  - sensor:
      - name: Net Power
        state_class: measurement
        unit_of_measurement: W
        device_class: power
        icon: mdi:transmission-tower
        state: >
          {% set production = states.sensor.envoy_SERIALNUMBER_current_energy_production.state | int %}
          {% set consumption = states.sensor.envoy_SERIALNUMBER_current_energy_consumption.state | int %}
          {{ (production - consumption) }}

See the warning box here:

It is better practice to use e.g.

states('sensor.envoy_SERIALNUMBER_current_energy_production')
1 Like

Excellent, thank you for pointing that out.

Here is the further revised code

template:
  - sensor:
      - name: Net Power
        state_class: measurement
        unit_of_measurement: W
        device_class: power
        icon: mdi:transmission-tower
        state: >
          {% set production = states('sensor.envoy_SERIALNUMBER_current_power_production') | int %}
          {% set consumption = states('sensor.envoy_SERIALNUMBER_current_power_consumption') | int %}
          {{ (production - consumption) }}

Note: as of late July 2021, these sensor names used to contain the word energy and have now been renamed to power.
I have now updated the code again to reflect the new name.
I noticed they updated this file in late July 2021 which is when the names changed
https://github.com/home-assistant/core/pull/53653/commits/2e7602a66eb3929ebc44aad157899ffc0d3be4b4

2 Likes

This is slightly off topic but I added a notification to notify me when net power goes above or below 0 and then show what the current value of the sensor is.

alias: Notifty when Positive
description: ''
trigger:
  - platform: numeric_state
    entity_id: sensor.net_power
    above: '0'
condition: []
action:
  - service: notify.notify
    data_template:
      message: Positive Net Power {{ states('sensor.net_power') }} Watts
mode: single

For any newbies that want their Enphase Envoy to work with the new Energy feature in Home Assistant 2021.8 please see Enhpase Envoy on 2021.8 with new Energy feature

1 Like

Im building a slightly different template sensor to calculate the sim of individual phases

template:
  - sensor:
    - name: Net Power
      unique_id: "net_power"
      state_class: measurement
      unit_of_measurement: W
      device_class: power
      icon: mdi:transmission-tower
      state: >
        {% set L1 = states('sensor.all_l1_power_2')|float %}
        {% set L2 = states('sensor.all_l2_power_2')|float %}
        {% set L3 = states('sensor.all_l3_power_2')|float %}
        {{ (L1 + L2 + L3) | round(2) }}

The template works and calculates fine, but the entity newer apears in home assistant