Home Energy Management in 2021.8

Hi all,
I stumbled upon this topic as I am trying to integrate the battery template sensor (charge/discharge) into my energy tab, but these two sensors are simply not showing up

template:
#Split power in and out of battery
  - sensor:
      - name: "Battery input power"
        unit_of_measurement: "W"
        state: >
          {% if states('sensor.battery_power')|float >= 0 %}
            {{ states('sensor.battery_power') }}
          {% else %}
            0
          {% endif %}
      - name: "Battery output power"
        unit_of_measurement: "W"
        state: >
          {% if states('sensor.battery_power')|float < 0 %}
            {{ -1 * states('sensor.battery_power')|float }}
          {% else %}
            0
          {% endif %}

In Dev Tools, I get all the results, but no sensors are appearing

Any ideas?
I’m running the latest HA

Already answered to this question in this thread, but I’ll repeat it once again for stumblers that don’t want to really read it since the beginning. :slight_smile:

In order for the sensors to appear in the energy integration they must have the following attributes:

  • device_class set to energy
  • state_class set to either: measurement or total or total_increasing

You can find details here: Sensor Entity | Home Assistant Developer Docs

Thank you Alex, really appreciated!
On to the reading I go :slight_smile:

My pleasure Bobby.

Ok, now I’m a bit confused

template:
#Split power in and out of battery
  - sensor:
      - name: "Battery input power"
        unit_of_measurement: "W"
        device_class: energy
        state_class: total_increasing
        state: >
          {% if states('sensor.battery_power_victron')|float >= 0 %}
            {{ states('sensor.battery_power_victron') }}
          {% else %}
            0
          {% endif %}
      - name: "Battery output power"
        unit_of_measurement: "W"
        device_class: energy
        state_class: total_increasing
        state: >
          {% if states('sensor.battery_power_victron')|float < 0 %}
            {{ -1 * states('sensor.battery_power_victron')|float }}
          {% else %}
            0
          {% endif %}

Its still now showing up :frowning:
What am I doing wrong?

First of all, I think also for batteries the integration needs ENERGY not POWER sensors. I don’t have a battery so I never tested this. So the device_class you used is wrong.

Second: those sensors are not total_increasing like you configured them, they are a measurement.

I guess you read too quickly…:wink:

:stuck_out_tongue:
Ok, I think I get it

My battery sensor gives +x W when charging and -x W when discharging…
So first I need to create a template sensor to convert from W to Wh

Only after can I use it in the above mentioned sensor…

I woke up at 3am here where I am and an going deeper down the rabbit hole (HA hole) :smiley:
Thanks one more :slight_smile:

1 Like

It’s 4.56am here. I live in Italy. Don’t do this stuff if you’re not totally awake. :slight_smile:

Energy Integration needs energy sensors. If you have power sensors, you need to calculate the energy. You can use the Riemann sum integral integration to do that.

If you need an example, here’s the configuration of the sensors for a Tesla PowerWall system (link) made by @briancmoses

Brian made a nice configuration to adapt/rename the existing power sensors using template sensors and also defined the corresponding energy sensors using the integral platform. You can learn a lot from his configuration.

sensor:
  - platform: template
    sensors:
      grid_return_kw:
        friendly_name: "Grid Return (kW)"
        device_class: power
        unit_of_measurement: kW
        value_template: "{{ states('sensor.powerwall_site_now') | float | min(0) | abs | round(4) }}"
          
      grid_consumption_kw:
        friendly_name: "Grid Consumption (kW)"
        device_class: power
        unit_of_measurement: kW
        value_template: "{{ states('sensor.powerwall_site_now') | float | max(0) | round(4) }}"
    
      powerwall_charging_kw:    
        friendly_name: "Powerwall Charging (kW)"
        device_class: power
        unit_of_measurement: kW
        value_template: "{{ states('sensor.powerwall_battery_now') | float | min(0) | abs | round(4) }}"
        
      powerwall_discharging_kw:    
        friendly_name: "Powerwall Discharging (kW)"
        device_class: power
        unit_of_measurement: kW
        value_template: "{{ states('sensor.powerwall_battery_now') | float | max(0) | round(4) }}"

  - platform: integration
    source: sensor.powerwall_solar_now
    name: energy_solar

  - platform: integration
    name: energy_totalConsumption
    source: sensor.powerwall_load_now    

  - platform: integration
    name: energy_grid_consumption
    source: sensor.grid_consumption_kw
    
  - platform: integration
    name: energy_grid_return
    source: sensor.grid_return_kw
    
  - platform: integration
    source: sensor.powerwall_charging_kw
    name: energy_powerwall_charging

  - platform: integration
    source: sensor.powerwall_discharging_kw
    name: energy_powerwall_discharging

Grazie :slight_smile:
I will dig into it during today

1 Like

Thank you Alex
I got a hang of it and figured it out…you were right - I was reading way to fast :man_facepalming: :grinning:

You have to be like a sniper: “slow is smooth…smooth is fast.” :wink:

Hi, work well this your method? The date is simolar Shelly App?

Hi, yes it works well now since I installed it. Statistics are accurate

I did not understand how to create the sensors for the storage battery

Fist you need to have a sensor that will measure the battery (in my case that is “sensor.battery_power_victron”)

Assuming you have that, then you need to create 4 new sensors.

  1. battery_input_power.yaml
- platform: template
  sensors:
    battery_input_power:
       friendly_name: "Battery Input Power"
       device_class: power
       unit_of_measurement: "W"
       value_template: "{{ max(0, states('sensor.battery_power_victron') | float) }}"
  1. battery_output_power.yaml
- platform: template
  sensors:
    battery_output_power:
       friendly_name: "Battery Output Power"
       device_class: power
       unit_of_measurement: "W"
       value_template: "{{ max(0, 0 - states('sensor.battery_power_victron') | float) }}"

The above two (1 and 2) will measure amount of power coming IN and going OUT of the battery.
Then you need to setup the Battery kWh meter IN and OUT of the battery. Those are the two below sensors (3 and 4).

  1. battery_charge.yaml
  - platform: integration
    source: sensor.battery_input_power
    name: Battery Charge
    unit_prefix: k
    method: left
    #or right or trapezoidal for methods
    round: 3
  1. battery_discharge.yaml
  - platform: integration
    source: sensor.battery_output_power
    name: Battery Discharge
    unit_prefix: k
    method: left
    #or right or trapezoidal for methods
    round: 3

Then you can integrate this into the Energy tab.


Final result:

Hi thank you. What do you measure the battery with?

It it a Pylontech US3000 (lithium iron phosphate) battery and it is connected to the Victron system with BMS coms cable. That is how Victron system sees and knows if the battery is charging or discharging.
Victron system is integrated into Home Assistant either via Modbus or MQTT (I use mod bus)

Thank you…

I could not find anything about entity requirements in the Home Assistant documentation up to now. Wouldn’t it make sense to document the entity requirement on the Home Assistant documentation?

I just figured out the I explicitly had to define these attributes for my MQTT sensor:

  • state_class: measurement
  • device_class: energy

I also noticed that it worked without defining last_reset value.

Cheers, red

At the bottom of that page, and every page in the docs, are links to provide feedback, or submit edits.