Template sensor returns correct values but not always

I have 3 energy identities that I want to combine. For this I created a sensor template in the configuration.yaml. This usually works, but sometimes a value (state) is created that is incorrect. You can’t see this in the normal dashboard but the energy dashboard is going crazy.

My configuration.yaml is:

  • sensor:
    • name: “ener_1og”
      unique_id: ener_1og
      state: >-
      {{ (states(‘sensor.shellypmminig3_ecda3bc46ae0_energy’) | float(0) +
      states(‘sensor.shellypmmini_543204a97e18_energy’) | float(0) +
      states(‘sensor.shellypmminig3_ecda3bc46afc_energy’) | float(0)) }}
      unit_of_measurement: kWh
      device_class: energy
      state_class: total_increasing

I entered the (0) in the queries but without success.

I ask for help, thanks very much

My HA Configuration has about 950 Entities and 70 devices, I started at 01/2024 with HA and I am an Engineer

Looks like there is a sensor or more that is unavailable.

The dips are when your sources go unavailable or unknown, which typically happen when HA restarts or if the sensor(s) drop off your network momentarily. You should account for this by making an availability template for your template entity.

        availability: >
          {{ 'sensor.shellypmminig3_ecda3bc46ae0_energy' | has_value and 'sensor.shellypmmini_543204a97e18_energy' | has_value and 'sensor.shellypmminig3_ecda3bc46afc_energy' | has_value }}

No, that will break has_value, has_value requires an entity_id as the argument.

1 Like

Every day is a school day. Apologies.

NP, I made the same mistake too and the doc’s don’t do us any favors for templating at the moment.

Hi Petro, that was the solution. Everything has been peaceful for 2 days now. Thank you

You marked the wrong post as the answer