Home battery on energy dashboard

Hello all!
I have tried to put my battery in the energy dashboard. I have a sensor that sends data from in and out from the battery. In is + and out is a - value in KW. The energy dashboard will not take this. is there anything I can do here?

Yes you can create template sensors:

template:
  - sensor:
      - name: "Battery Energy In"
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total_increasing
        state: >
          {% if states('sensor.your_battery_energy_sensor_here')|float(0) >= 0 %}
            {{ states('sensor.your_battery_energy_sensor_here') }}
          {% else %}
            {{ this.state }}
          {% endif }}
        availability: "{{ states('sensor.your_battery_energy_sensor_here')|is_number }}"

      - name: "Battery Energy Out"
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total_increasing
        state: >
          {% if states('sensor.your_battery_energy_sensor_here')|float(0) < 0 %}
            {{ states('sensor.your_battery_energy_sensor_here')|float(0)|abs }}
          {% else %}
            {{ this.state }}
          {% endif }}
        availability: "{{ states('sensor.your_battery_energy_sensor_here')|is_number }}"

Thanks, but I must have done something wrong. I get tis error.

Invalid config for [template]: invalid template (TemplateSyntaxError: unexpected ‘}’) for dictionary value @ data[‘sensor’][0][‘state’]. Got “{% if states(‘sensor.batery_kwh’) | float(0) >= 0 %}\n {{ states(‘sensor.batery_kwh’) }}\n{% else %}\n {{ this.state }}\n{% endif }}\n”. (See /config/configuration.yaml, line 1022).

          {% endif }}

Should be:

          {% endif %}

EDIT: Actually, I’ve just realised this is a power sensor not an energy sensor. So you can’t use it (directly) with the energy dashboard.

Why is your sensor called sensor.batery_kwh?

kWh is a measure of energy not power. And you said:

Here is the sensor I made for battery in and out, so yes I think you are correct this is power kw not kwh I have mislabeled it. so next question how do I get the energy sensor that i need. Do I use the Riemann sum?

  • platform: template

    sensors:

    batery_kwh:
    
      unit_of_measurement: "kWh"
    
      friendly_name: "battery kWh"
    
      value_template: >
    
        {% set ahtofrom = states('sensor.total_battery_amps' ) %}
    
        {% set voltsnow = states('sensor.pv_fndc_batt_v') %}
    
        {{ (float(ahtofrom) | float(2) * float(voltsnow) | float(2) / 1000 | float(2)) | round(2) }}

Yep that is a power sensor. Volts * Amps = Power not energy. Change the unit of that to “kW”.

Or replace it altogether with this:

template:
  - sensor:
      - name: "Battery Power In"
        unit_of_measurement: "kW"
        device_class: power
        state_class: measurement
        state: >
          {% set power = states('sensor.total_battery_amps' )|float(0) * states('sensor.pv_fndc_batt_v')|float(0) /1000 %}
          {% if power >= 0 %}
            {{ power }}
          {% else %}
            0
          {% endif %}
        availability: "{{ states('sensor.total_battery_amps' )|is_number and states('sensor.pv_fndc_batt_v')|is_number }}"

      - name: "Battery Power Out"
        unit_of_measurement: "kW"
        device_class: power
        state_class: measurement
        state: >
          {% set power = states('sensor.total_battery_amps' )|float(0) * states('sensor.pv_fndc_batt_v')|float(0) /1000 %}
          {% if power < 0 %}
            {{ power|abs }}
          {% else %}
            0
          {% endif %}
        availability: "{{ states('sensor.total_battery_amps' )|is_number and states('sensor.pv_fndc_batt_v')|is_number }}"

Then feed these two power sensors to the Riemann sum integration to create energy sensors:

sensor:
  - platform: integration
    name: Battery Energy In
    source: sensor.battery_power_in
	method: left
  - platform: integration
    name: Battery Energy Out
    source: sensor.battery_power_out
	method: left

You can then use the two resultant energy sensors in the energy dashboard.

2 Likes

I think I got it correct now. (Well in truth you got it) Thank you for all the help.

1 Like

Hello tom_l, I found this very useful for my JBD BMS, but I’m getting an error on Energy dashboard configuration:

statistics_not_defined
sensor.battery_energy_in

Maybe I have to create new template with device_class: energy ? Thanks for the suggestions.

Anyone work out how to show the battery percentage on this page?
Its a Power rather than an Energy tag so assuming we cant???

1 Like