I’m stuck on trying to understand how to create a sensor that performs a calculation.
I have Enphase Solar system. The native Enphase Envoy integration is active and creates two sensors:
What I would like to achieve is to have a NET energy sensor. So that I can identify if I am sending back to grid (production is higher than consumption) or I am consuming from the grid (consumption is higher than production).
I presume this would not be a binary_sensor as it would need to report more than on/off.
Has anyone accomplished this and could help me understand how to achieve.
You need to create template sensors. I have the below created, and I use them for the ‘Tesla style power wheel card’ among other things. The ones you are looking for are the second and third one.
- platform: template
sensors:
envoy_house_consumption:
friendly_name: "Current House Consumption"
value_template: "{{ [0, (states('sensor.envoy_current_energy_production') | int - states('sensor.envoy_current_exporting') | int)] | max }}"
unit_of_measurement: 'W'
icon_template: 'mdi:flash'
envoy_current_exporting:
friendly_name: "Current Energy Exporting"
value_template: "{{ [0, (states('sensor.envoy_current_energy_production') | int - states('sensor.envoy_current_energy_consumption') | int)] | max }}"
unit_of_measurement: 'W'
icon_template: 'mdi:flash'
envoy_current_importing:
friendly_name: "Current Energy Importing"
value_template: "{{ [0, (states('sensor.envoy_current_energy_consumption') | int - states('sensor.envoy_current_energy_production') | int)] | max }}"
unit_of_measurement: 'W'
icon_template: 'mdi:flash'
envoy_today_s_energy_exporting:
friendly_name: "Today Energy Exporting"
value_template: "{{ [0, (states('sensor.envoy_today_s_energy_production') | int - states('sensor.envoy_today_s_energy_consumption') | int)] | max }}"
unit_of_measurement: 'Wh'
icon_template: 'mdi:flash'
envoy_today_s_energy_importing:
friendly_name: "Today Energy Importing"
value_template: "{{ [0, (states('sensor.envoy_today_s_energy_consumption') | int - states('sensor.envoy_today_s_energy_production') | int)] | max }}"
unit_of_measurement: 'Wh'
icon_template: 'mdi:flash'
envoy_today_s_consumption_negative:
value_template: '{{ ((states.sensor.envoy_today_s_energy_consumption.state | float * -1)) | round(1) }}'
friendly_name: 'Today Energy Consumption'
unit_of_measurement: 'Wh'
power_kw:
friendly_name: "Power Available"
unit_of_measurement: 'kW'
value_template: >
{% if states('sensor.envoy_current_exporting')|float > states('sensor.envoy_current_importing')|float %}
{{ (states('sensor.envoy_current_exporting') | int / 1000) | round(2)}}
{% else %}
{{ (states('sensor.envoy_current_importing') | int / -1000) | round(2)}}
{% endif %}
If you want a basic Net Power sensor, this is what I did in configuration.yaml
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) }}