Template sensor issue

Good evening all,

I’ve been finding an issue with the reporting coming from my battery storage. I have two batteries but only information from one is being reported. I wanted to record the difference between the two and I’ve created a template sensor.

sensor:
  - platform: template
    sensors:
      battery_2_discharging_rate:
        unit_of_measurement: W
        value_template: "-{{ states('sensor.solax_house_load') | float + states('sensor.solax_battery_power_charge') | float }}"
        friendly_name: Battery 2 Discharge Rate
        icon_template: mdi:flash
        
sensor:
  - platform: template
    sensors:
      battery_discharging_rate:
        unit_of_measurement: W
        value_template: "{{ states('sensor.battery_2_discharging_rate') | float + states('sensor.solax_battery_power_charge') | float }}"
        friendly_name: Battery Discharge Rate
        icon_template: mdi:flash

However, the second sensor does not work. It comes back as unavailable. It works when I create it in Developer Tools in the Template section but when I put it into configuration.yaml and restart I get nothing. I’m clearly doing something wrong, but I am out of ideas for what it would be. The second sensor is just adding the total from the first sensor to the amount the other battery is discharging.

It’s late now, and I’m going to have to leave it for the night, but any help would be appreciated.

Thanks,

Tom

sensor:
  - platform: template
    sensors:
      battery_2_discharging_rate:
        unit_of_measurement: W
        value_template: "-{{ states('sensor.solax_house_load') | float(0) + states('sensor.solax_battery_power_charge') | float(0) }}"
        friendly_name: Battery 2 Discharge Rate
        icon_template: mdi:flash
      battery_discharging_rate:
        unit_of_measurement: W
        value_template: "{{ states('sensor.battery_2_discharging_rate') | float(0) + states('sensor.solax_battery_power_charge') | float(0) }}"
        friendly_name: Battery Discharge Rate
        icon_template: mdi:flash

Or:

sensor:
  - platform: template
    sensors:
      battery_2_discharging_rate:
        unit_of_measurement: W
        value_template: "-{{ states('sensor.solax_house_load') | float + states('sensor.solax_battery_power_charge') | float }}"
        friendly_name: Battery 2 Discharge Rate
        icon_template: mdi:flash
        
  - platform: template
    sensors:
      battery_discharging_rate:
        unit_of_measurement: W
        value_template: "{{ states('sensor.battery_2_discharging_rate') | float + states('sensor.solax_battery_power_charge') | float }}"
        friendly_name: Battery Discharge Rate
        icon_template: mdi:flash

Apologies for not replying sooner, it’s been a manic weekend. They work, I then found I was equation was only right for some of the time; so it needs more work. Thanks for the help!

Tom