Skip to content

Using Templated Sensors to Calculate Power Flow and Energy

jds11111 edited this page Jul 26, 2022 · 7 revisions

In an ideal world, the power flows and energy distribution would be calculated automatically by the integration, but that gets very complex very fast due to the variations in system configuation:

  • Presence / absence of meters
  • Presence / absence of batteries
  • Configuration (location) of meters i.e. grid connection / load connection point

All of these change the maths that has to be performed to come up with the right values. The following are examples of templated sensors to calculate power and energy in various configurations.

Templated sensors should be entered in your configuration.yaml file, and home assistant reloaded.

Note that in all of the following templates, the integration prefix has been changed from the default "solaredge" to "solaredge_modbus". When adding / configuring the integration, you should either change the prefix to "solaredge_modbus", or modify the template sensors to just be "solaredge" instead of "solaredge_modbus".

Single inverter, single meter at grid import / export point

The following sensors allow derivation of the right values for the Tesla Style Power Card

sensor:                                                                         
  - platform: template                                                          
    sensors:
      power_grid_import:                                                    
        friendly_name: "Power - Grid Import"                                    
        unit_of_measurement: "W"    
        # Grid import to loads is negative values of import / export meter
        value_template: "{{ min([states('sensor.solaredge_modbus_m1_ac_power') | float(0), 0]) | abs() }}"
      power_grid_export:                                                     
        friendly_name: "Power - Grid Export"                                    
        unit_of_measurement: "W"   
        # Grid export is positive values of import / export meter
        value_template: "{{ max([states('sensor.solaredge_modbus_m1_ac_power') | float(0), 0]) }}"                                                                                                                                                   
      power_consumption:                                                  
        friendly_name: "Power - Consumption"                                    
        unit_of_measurement: "W"       
        # House consumption is inverter output minus import / export meter flow
        value_template: "{{ (states('sensor.solaredge_modbus_ac_power') | float(0)) - (states('sensor.solaredge_modbus_m1_ac_power') | float(0)) }}"
      power_self_consumption:                                            
        friendly_name: "Power - Self Consumption"                               
        unit_of_measurement: "W"           
        # Self consumption of solar generation is inverter output, minus what is being exported to grid                                 
        value_template: "{{ (states('sensor.solaredge_modbus_ac_power') | float(0)) - states('sensor.power_grid_export') | float(0) }}" 

The Tesla Style Power Card is configured like this:

type: custom:tesla-style-solar-power-card
house_entity: sensor.power_consumption
grid_entity: sensor.solaredge_modbus_m1_ac_power
generation_entity: sensor.solaredge_modbus_ac_power
grid_to_house_entity: sensor.power_grid_import
generation_to_grid_entity: sensor.power_grid_export
generation_to_house_entity: sensor.power_self_consumption

Single inverter, single battery, single meter at grid import / export point

The following sensors allow derivation of the right values for both the Tesla Style Power Card and the Home Assistant Energy Dashboard

sensor:                                                                         
  - platform: template                                                          
    sensors:  
      power_battery_charging:                                                   
        friendly_name: "Power - Battery Charging"                               
        unit_of_measurement: "W"                                                
        value_template: "{{ max([states('sensor.solaredge_modbus_battery1_power') | float(0), 0]) | abs() }}"
      power_battery_discharging:                                                
        friendly_name: "Power - Battery Discharging"                            
        unit_of_measurement: "W"                                                
        value_template: "{{ min([states('sensor.solaredge_modbus_battery1_power') | float(0), 0]) | abs() }}"
      power_battery_charging_ac:                                                
        friendly_name: "Power - Battery Charging from AC"                       
        unit_of_measurement: "W"                                                
        value_template: "{{ min([states('sensor.solaredge_modbus_ac_power') | float(0), 0]) | abs() }}"
      power_battery_charging_solar:                                             
        friendly_name: "Power - Battery Charging from Solar"                    
        unit_of_measurement: "W"                                                
        value_template: "{{ (states('sensor.power_battery_charging') | float(0)) - (states('sensor.power_battery_charging_ac') | float(0)) }}"
                                                                                
      power_grid_import:                                                        
        friendly_name: "Power - Grid Import"                                    
        unit_of_measurement: "W"                                                
        value_template: "{{ (min([states('sensor.solaredge_modbus_m1_ac_power') | float(0), 0]) | abs()) - (states('sensor.power_battery_charging_ac') | float(0)) }}"
      power_grid_export:                                                        
        friendly_name: "Power - Grid Export"                                    
        unit_of_measurement: "W"                                                
        value_template: "{{ max([states('sensor.solaredge_modbus_m1_ac_power') | float(0), 0]) | abs() }}"
                                                                                
      power_solar_generation:                                                   
        friendly_name: "Power - Solar Generation"                               
        unit_of_measurement: "W"                                                
        value_template: "{{ max([states('sensor.solaredge_modbus_ac_power') | float(0), 0]) + (states('sensor.power_battery_charging_solar') | float(0)) - (states('sensor.power_battery_discharging') | float(0)) }}"
                                                                                
      power_consumption:                                                        
        friendly_name: "Power - Consumption"                                    
        unit_of_measurement: "W"                                                
        value_template: "{{ max([states('sensor.solaredge_modbus_ac_power') | float(0), 0]) - states('sensor.solaredge_modbus_m1_ac_power') | float(0) }}"
      power_self_consumption:                                                   
        friendly_name: "Power - Self Consumption"                               
        unit_of_measurement: "W"                                                
        value_template: "{{ max([states('sensor.solaredge_modbus_ac_power') | float(0), 0]) - (states('sensor.power_battery_discharging') | float(0)) - states('sensor.power_grid_export') | float(0) }}"
                                                                                
      energy_solar_generation:                                                  
        friendly_name: "Energy - Solar Generation"                              
        unit_of_measurement: "kWh"                                              
        value_template: "{{ (states('sensor.solaredge_modbus_ac_energy_kwh') | float(0)) - (states('sensor.energy_battery_discharged') | float(0)) + (states('sensor.energy_battery_charged_solar') | float(0)) }}"
        availability_template: "{{ states('sensor.solaredge_modbus_ac_energy_kwh') not in ['unknown', 'unavailable'] }}"
                                    
  - platform: integration
    # Integrate the battery charging power flow to get energy                                                       
    name: energy_battery_charged                                                
    source: sensor.power_battery_charging                                       
    method: left                                                                
    unit_prefix: k                                                              
  - platform: integration    
    # Integrate the battery charging from solar power flow to get energy                                                     
    name: energy_battery_charged_solar                                          
    source: sensor.power_battery_charging_solar                                 
    method: left                                                                
    unit_prefix: k                                                              
  - platform: integration  
    # Integrate the battery discharging power flow to get energy                                                       
    name:  energy_battery_discharged                                            
    source: sensor.power_battery_discharging                                    
    method: left                                                                
    unit_prefix: k    

My battery firmware seems to have a bug where the reported energy charged / discharged periodically return to 0 rather than being lifetime values. To work around that, I use the integration platform to integrate the power flows into energy values.

Some additional hacks are necessary to change the state / device classes of the above sensors so that they are usable from the energy dashboard. These are:

homeassistant:                                                                                                       
  customize_glob:                                                               
    sensor.energy_solar_generation:                                             
      device_class: energy                                                      
      state_class: total_increasing                                             
    sensor.energy_battery_charged:                                              
      device_class: energy                                                      
    sensor.energy_battery_discharged:                                           
      device_class: energy

The Tesla Style Power Card is configured like this:

type: custom:tesla-style-solar-power-card
name: Power Flow
energy_flow_diagramm: 1
change_house_bubble_color_with_flow: 1
house_entity: sensor.power_consumption
grid_entity: sensor.solaredge_modbus_m1_ac_power
generation_entity: sensor.power_solar_generation
battery_entity: sensor.solaredge_modbus_battery1_power
battery_extra_entity: sensor.solaredge_modbus_battery1_state_of_charge
grid_to_house_entity: sensor.power_grid_import
grid_to_battery_entity: sensor.power_battery_charging_ac
generation_to_grid_entity: sensor.power_grid_export
generation_to_house_entity: sensor.power_self_consumption
generation_to_battery_entity: sensor.power_battery_charging_solar
battery_to_house_entity: sensor.power_battery_discharging

And the energy dashboard like: