Templated sensor not accepted for home battery system

Hi,

I’ve just installed a battery back up system with a few solar panels in my home which I’m trying to integrate into home assistant. I’m having trouble getting the energy tab to accept my sensors for energy into and out of the battery.

My set up:

I have a Shelly EM monitoring the mains into my home - working great

I have a Bluetti AC500 hybrid inverter system, and I’m using a 3rd party integration to extract data from the inverter and present it to HA as MQTT topics. I’ve got (all measured as power in watts): Solar power in, AC power in, AC power out.

It is my understanding that the Energy monitor in HA is based around an AC coupled system which is grid attached, so I have tried to isolate just the charge/discharge of the battery its self - basically excluding power passed straight through the AC500, either from solar or grid.

I have created the following templated sensors in Yaml:

template:
  - sensor:
      - name: "AC500 Battery Power In"
        unique_id: ac500_battery_power_in
        unit_of_measurement: "W"
        state: >-
            {% if (states('sensor.ac500_ac_input_power')|float(0)) + (states('sensor.ac500_dc_input_power')|float(0)) > (states('sensor.ac500_ac_output_power')|float(0)) %}
                {{ (states('sensor.ac500_ac_input_power')|float(0)) + (states('sensor.ac500_dc_input_power')|float(0)) - (states('sensor.ac500_ac_output_power')|float(0))  }}
            {% else %}
                {{ '0'|float(0)}}
            {% endif %}

  - sensor:
      - name: "AC500 Battery Power Out"
        unique_id: ac500_battery_power_out
        unit_of_measurement: "W"
        state: >-
            {% if (states('sensor.ac500_ac_input_power')|float(0)) + (states('sensor.ac500_dc_input_power')|float(0)) < (states('sensor.ac500_ac_output_power')|float(0)) %}
                {{ (states('sensor.ac500_ac_output_power')|float(0)) - ((states('sensor.ac500_ac_input_power')|float(0)) + (states('sensor.ac500_dc_input_power')|float(0))) }}
            {% else %}
                {{ '0'|float(0)}}
            {% endif %}

This gives me the power into and out of the battery in watts. I’ve used the Riemann sum integral helper to turn these into cumulative power, and then templated the output again to give it the proper attributes (according to the documentation: https://www.home-assistant.io/docs/energy/faq/#troubleshooting-missing-entities.

More yaml to do that (under the same template block above):

  - sensor:
      - name: "AC500 Energy In (Template)"
        unique_id: ac500_energy_in_template
        unit_of_measurement: kWh
        device_class: energy
        state_class: total_increasing
        state: "{{ states('sensor.ac500_energy_in')|float(0) }}"

  - sensor:
      - name: "AC500 Energy Out (Template)"
        unique_id: ac500_energy_out_template
        unit_of_measurement: kWh
        device_class: energy
        state_class: total_increasing
        state: "{{ states('sensor.ac500_energy_out')|float(0) }}"

This appears to have worked:

The entities appear on my dashboard and give me sensible numbers. As you can see, they have all the attributes which mean they should be accepted by the energy tab. But they do not show up on the list of sensors when I try to add them.

The power sensor and the output from the Riemann sum integral helpers are both available to add as “energy going into the battery” and “energy going out of the battery” in the energy config, but when I do I get a warning:

Unexpected device class

The following entities do not have the expected device class:

* sensor.ac500_energy_in
* sensor.ac500_energy_out

And the values always show up as 0KWh in the graphs and totals.

Additionally, the templated sensors have no history recorded by HA. The output from the integrators (the input to the templates above) are recording histories just fine, but appear to have the wrong device class.

I’m stumped as to why these sensors I’ve templated won’t show up in the list of available sensors. Does anyone know what I’m doing wrong?

Thanks!

Why the templated ones do not ahow up is weird. But you should not need them.

You’d need to show is the output of the original sensors from the developer tools to see what device class they have. Because now you only show the ones you converted through another layer of templates.

I would have expected the Riemann sum integral sensors to have the right device class. It might be because you did not set a device_class on the power sensors you put in.

Worst case scenario you could override it with customize, but that would be a last resort.

So I think I have at least partially solved it.

So I also created an integrator for the solar input in watts, which automatically has the correct device class and was accepted just fine.

For what ever reason, the UI generated integrals for the other two did not get the right device class (I would show a picture, but it will only let me attach 1 per post).

The main issue is that I’m an idiot and originally created my templated sensors with the unit “KWh” not “kWh” - and after noticing and fixing that, did not go back and update the data in the log to change the unit on historic records.

I noticed that under the statistics tab under developer tools, fixed it there and I’ve been able to add them to energy.

They’re still showing 0 kWh in and out though. And when you look at the history, every time I re-load the yaml, a 0 kWh gets recorded in the log:

Could these 0s in the log be what is confusing things? and if so, can I manually delete them?

If you got them set up in the dashboard, it will take up to an hour to see the results there. The 0’s will lead to weird readings in statistics. You can use the developer tools statistics page to edit the weird values out.

Hi,

so its showing values now at least! It’s claiming I have used 127 kWh and supplied 72 kWh to my battery haha.

I think I’ve solved it, and from tomorrow I’m sure the stats will make more sense. I’ve been trying to edit out the 0s using the stats page, but I think I actually caused the above strange readings.

Thanks for your help.

Alex

If you changed them to 0 you are on the right way, once you have them all it will be fine. I’d do that, otherwise the long period views will remain off.

It may seem worse if you have both positive and negative outliers and remove one of them but not the other.