Energy miss calculation problem

Hello, having a problem with a template sensor.

So, i have a SDM120 that gives me the whole house energy measurement, and have a guest house connected to the same grid, the SDM120 gives me the energy of the house and guest house, so i did this template sensor to calculate the energy that the house is using:

template:
  - sensor:
      - name: casa_energy_total_2
        unit_of_measurement: "kWh"
        state: >
          {{ ( states('sensor.powcasath_energy_total') | float | round(1)) - ( states('sensor.powapart_energy_total_2') | float | round(1))}}
        device_class: energy
        state_class: total_increasing
        unique_id: 24467489bb7d4777949907a6072967b5
        attributes:         
          last_reset: '2021-08-30T18:07:24+00:00'

For most of the part it works well, but from time to time it will miss calculate and i dont know why and give me wrong numbers

4

The SDM120 looks like this:
5

And the guest house like this:
6

The sensors are working well, so probably its something i made wrong with the template sensor.

And on the energy tab everything gets screwed up because of this miss calculation

3

If either of the sensors in your template are unavailable the |float filter will convert the string ‘unavailable’ to 0.

You need to add an availability option.

Also if you are on version 2021.9 the last_reset option has been depreciated and should be removed.

template:
  - sensor:
      - name: casa_energy_total_2
        unit_of_measurement: "kWh"
        state: >
          {{ ( states('sensor.powcasath_energy_total') | float | round(1)) - ( states('sensor.powapart_energy_total_2') | float | round(1))}}
        device_class: energy
        state_class: total_increasing
        unique_id: 24467489bb7d4777949907a6072967b5
        availability: >
          {{ states('sensor.powcasath_energy_total') != 'unavailable' and states('sensor.powapart_energy_total_2') != 'unavailable' }}
1 Like

Thanks for the reply, will try it later :slight_smile:

It worked for the most part, unfortunately sometimes when I update HA, or restart it, error on the results. Very rare, but happens.

Can anyone help me with a calculation.
I use the below template to calculate what my house is consuming, since the apartment uses power from the house, (i could setup another sensor just for the house, but i dont have anymore space on the electric board), i have HA calculate what the house is using by subtracting the apartment power from the house sensor and i get what power im using, works great most of the time, but i have solar panels, and when the house sensor is giving negative values, it gives me wrong values.

Is there a way to make it that when the template sensor result is negative value, the result would be a zero?

template:
  - sensor:
      - name: casa_power_2
        state: >
          {{ ( states('sensor.house_energy_power') | float | round(0)) - ( states('sensor.apartment_energy_power_2') | float | round(0))}}
        unit_of_measurement: 'W'
        device_class: power
        unique_id: 24467489bb7d4777949907a6072967z6
        availability: >
          {{ states('sensor.house_energy_power') != 'unavailable' and states('sensor.apartment_energy_power_2') != 'unavailable' }}