Guard Sensors against spike values and reachability issues by default

When you have sensors for digital meters like electricity, gas or water, you can easily get ghost values added when the sensor is not reachable.
Having the total value of your consumption added as a single value every now and again screws up your statistics alot.
So i would a) ignore outliers that are a multiple of normal readouts b) do not add the total value if the tasmota reader for example is not reachable or gives 0 as an answer

This should not happen. If it does it is an issue with the integration you are using and should be reported as such on github.

If you are using an integration that has an availability option, like the template integration, make sure you use it correctly.

It is an esp based ir reader, running tasmota. Values get sent over WiFi to an mqtt broker.
That in turn feeds a virtual energy sensor to be displayed on the ha default energy dashboard.
So if you say it needs to be reported as it is rather a bug than a missing guard feature, who is to blame?

What is a “virtual energy sensor”?

If you mean a template sensor, please show its configuration.

The original from a YT video:

template:
  - sensor:
    # Stromzähler Keller Verbrauch
      - name: "Stromzähler 1. & 2. OG Verbrauch"
        unique_id: "Stromz1u2OGVerbrauch"
        unit_of_measurement: 'kWh'
        device_class: "energy"
        state_class: "total_increasing"
        state: >-
            {{ float(states('sensor.tasmota_ace3000_total_inz1')) | round(3) }}
  - sensor:
    # Stromzähler Keller Erzeugung
      - name: "Stromzähler 1. & 2. OG Erzeugung"
        unique_id: "Stromz1u2OGErzeugung"
        unit_of_measurement: 'kWh'
        device_class: "energy"
        state_class: "total_increasing"
        state: >-
            {{ float(states('sensor.tasmota_ace3000_total_exz1')) | round(3) }}

This didn’t work so i attempted to fix it with

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

template:
template:
  - sensor:
    # Stromzähler Verbrauch
    - name: "Stromzähler Verbrauch"
      unique_id: "StromVerbrauch"
      unit_of_measurement: 'Wh'
      device_class: "energy"
      state_class: "total_increasing"
      state: >-
        {% if states('sensor.tasmota_sm_1_8_0') == 'unavailable' or states('sensor.tasmota_sm_1_8_0') == 0 %}
          {{ states('sensor.stromverbrauch') }}
        {% else %}
          {{ states('sensor.tasmota_sm_1_8_0') }}
        {% endif %}
  - sensor:
    # Stromzähler Erzeugung
    - name: "Stromzähler Erzeugung"
      unique_id: "StromErzeugung"
      unit_of_measurement: 'Wh'
      device_class: "energy"
      state_class: "total_increasing"
      state: >-
        {% if states('sensor.tasmota_sm_2_8_0') == 'unavailable' or states('sensor.tasmota_sm_2_8_0') == 0 %}
          {{ states('sensor.stromerzeugung') }}
        {% else %}
          {{ states('sensor.tasmota_sm_2_8_0') }}
        {% endif %}

  - sensor:
    # Stromzähler  Momentanverbrauch GesamtAllePhasen"
      - name: "Stromzähler Momentanverbrauch_GesamtAllePhasen"
        unique_id: "StromzMomVerbrauchGesamt"
        unit_of_measurement: 'W'
        device_class: "energy"
        state_class: "measurement"
        state: >-
            {{ states('sensor.tasmota_sm_16_7_0') }}

There are a number of issues with that config.

  1. you have repeated template: twice. It should only appear once.

  2. you have no defaults for your float filters. This can cause your templates to fail to load if the state is something that can’t be converted to a number.

  3. you need availability templates to prevent the “spikes” in your data.

  4. you are just copying some states, which is pointless.

And that is exactly why i say we need to make it easier out of the box.
Even people that seem to know what they do, miss these points in their guides.

There is only one guide you should be using, the official documentation. Everything else goes out of date very quickly.