Solar Battery sensor

Hi all,

I have a set up with solar panels and a battery.
The integration of the info in HA works perfectly, except for the data on the battery.

I only have a sensor for the current capacity of the battery (in Wh), but I don’t have a separate sensor for amount charged and discharged.
Doing a search brought me to using template sensors, but I have no idea where to start.

I’ve looked at the topic : Track sensor delta on change
But I can’t get that to work.

Anyone who can help?


I too have a solar pv and battery system, and I want to know how much use the battery is getting.

I get my data directly from the inverter into Node-RED using Modbus and I already have inverter provided figures for total energy to / from battery. However some of my inverter values are inaccurate by around 10% so I continue to experiment with different approaches to getting and processing data.

I have recently added an entity that is the calculated remaining useable kWh capacity of my battery. This comes from the battery SOC value and is the fraction of the stated capacity between my minimum charge and full. Since this entity is similar to your current capacity in Wh, I thought I would have a go at trying to create a sensor from this for the amount charged or discharged.

First, I assume you want the total energy put into the battery from when you start measuring. Daily or monthly energy can then be easily obtained from this using a utility meter sensor.

Assuming measuring from 0, energy discharged = energy charged - current capacity remaining, so we only need to work out one of these and we can calculate the other.

I have managed to do this and quite easily too. Here is what I did:

Starting from
My sensor ‘battery energy store’ which is the remaining battery energy capacity in kWh.

I created an input number using a helper. This is much easier than editing config.yml, and it does not require a restart either.
Minimum number 0
Maximum number 100000 (something large enough to hold a big value)
Step size 0.01 (my capacity sensor is in kWh and I am measuring this to two decimal places)
Unit of measurement kWh

For your example, you would probably have step size ‘1’ to measure in increments of Wh. HA did not like the step size of 0.01 in the UI, but it still accepted it when saved.

I created an automation also using a helper
For the trigger, I used type ‘state’ and for entity, the sensor for my battery capacity
For the condition, I used ‘template’ and a value template of

{{trigger.to_state.state |float > trigger.from_state.state |float}}

This means that the automation will execute when the state (value) of my battery capacity sensor changes, but only when the new state is greater than the old state (ie the battery is charging). I found that I needed the ‘|float’ to turn the state value as a string into a number otherwise the test fails for negative numbers.

Then I just need an action, which is to calculate the difference between the new value and the old value, and add this difference to the existing value in the input number.

This was a little more challenging as the helper UI cannot manage it all, but I first used the UI to set the action to ‘call service’ and the service to ‘input number set (value)’, and a value of ‘0’. Then I added the input number I had already created as the target.
The UI will only allow numbers in the ‘value’ field, but I switched from the UI editor to YAML and entering some template code, ended up with the following

service: input_number.set_value
  value: >-
    {{trigger.to_state.state |float - trigger.from_state.state |float +
    states('input_number.test_up') |float}}
  entity_id: input_number.test_up

This is an action that calls a service to set the input number sensor to a value. The value is obtained from the template, which gets the existing value of the input number, and adds the difference between the current and previous value of the battery capacity (the trigger sensor).
ie it increases the input number by the change (increase) in the trigger sensor.

It was again necessary to use |float to turn the string state values into a float. In your case, if your step size is 1, you should probably use |int to turn the string into an integer.

Note that sensors that are input numbers retain their setting values over HA restarts, which means this input number entity can hold the total energy figures. To avoid accidentally changing it, to display the value of the sensor I should use another template sensor that just copies the state value, and not display the input number sensor on the dashboard.

I have set up a version of this automation to measure the cycles of battery used. My batteries come with a ‘cycle’ counter, which increments by 1 for every 100% of a complete charge/discharge cycle. To be able to replicate this, I can use the automation approach based on the battery SOC % entity.
Since my SOC sensor in theory changes by either +1% or -1% I could use the input-number-increment (+1 or +0.01 step) on each change, but to cope with any monitoring failures, I will use input-number-set-value so that an unexpected sensor change from 25% to 37% for example would correctly add +12 to the input number.

I hope this works for you.