No long-term statistics despite of correct state_class

Hi, I have a problem with HA and I hope you can help me. A few weeks ago I installed a LORA transmitter in the nearest forest, which sends sensor values home every few minutes. It’s working fine, I can parse the payload with Home Assistant and get the corresponding values displayed. However, after 10 days the temperature display does not simply lose the level of detail as normal, but nothing is saved at all.
At first i thought the state_class was missing, but it’s there. Here is an excerpt from config.yaml for the voltage. Sometimes unrealistic values are sent, but the query for realistic values is included.

- name: "Lora1 Voltage"
          unique_id: "lora_1_voltage"
          state_topic: "tele/Tasmota_Lora/RESULT"
          value_template: >-
            {% set payload = value_json.LoRaReceived %}
            {% if payload.startswith('Lora1:') %}
                {% set v = payload.split(':')[1][1:] | float %}
                {{ v if 2.5 <= v <= 4.5 else none }}
            {% endif %}
          unit_of_measurement: "V"
          device_class: voltage
          state_class: measurement

Do you have any idea what else might block him from showing long-term data? Thank you.

Assuming that is the mqtt integration (you did not say or include the relevant config), you need an else case for your if and you can’t use none in a numeric sensor. Check your logs for errors related to this.

- name: "Lora1 Voltage"
  unique_id: "lora_1_voltage"
  state_topic: "tele/Tasmota_Lora/RESULT"
  value_template: >
    {% set payload = value_json.LoRaReceived %}
    {% if payload.startswith('Lora1:') %}
        {% set v = payload.split(':')[1][1:] | float %}
        {% if 2.5 <= v <= 4.5 %}
          {{ v }}
        {% else %}
          {{ this.state }} {# keeps the last value #}
        {% endif %}
    {% else %}
      {{ this.state }} 
    {% endif %}

Then go to Developer Tools → Statistics and fix any issues listed there before restarting.

It would also help if you shared the payload so we could check your template.

Thanks so far. The none was on purpose so that I can see from the gaps in the graph when there were again some inconsistent values. But I will fix it as you say.
The payload is as follows:

"LoRaReceived":"Lora1:V3.51:T12.25:H60.56:P1000.31","RSSI":-107.0,"SNR":3.7

You can use the availability template option for the gaps.

Template looks ok.

Ok, thanks so far. But these gaps are not responsible for the fact that the long-term storage did not work, are they?

Nope. Sorry, I lost sight of the goal.

Were there any issues to fix in Dev tools stats?

What are the attributes for the entity listed in Developer Tools → States?

No problem. There are no issues reported for these sensors under dev tool stats.
In case of the temperature the attributes are as follows:

state_class: measurement
unit_of_measurement: °C
device_class: temperature
icon: mdi:thermometer
friendly_name: Temperatur Wald

No more ideas anybody?

Go to Developer Tools → Statistics

Is sensor.lora1_voltage in the list?

Is its icon (in the righthand column) a “trending up” symbol? Or is it something else?

Thanks. All entities are in the list with no problem and no symbol.

Given that name, Home Assistant will create a sensor with the following entity_id:

sensor.lora1_voltage

Where is that sensor entity in the list you posted? I see just one voltage-related sensor and it’s not sensor.lora1_voltage.

Ok, now I understand. Well, the entity for the battery voltage is sensor.lora_sensor_voltage (Batterie Wald). Don’t ask me why he didn’t create the entity_id sensor.lora1_voltage. Is this what the problem is all about?

It’s difficult to answer your questions because conflicting evidence has been presented.

  • The MQTT Sensor’s configuration is designed to produce sensor.lora1_voltage.

  • The list of displayed sensors has no sensor.lora1_voltage.

Here’s what I suggest:

  1. Go to Developer Tools - > States
  2. In the first column named “Entity”, enter sensor.lora in the “Filter Entities” field.

This will display all entity_ids beginning with sensor.lora.
Does the list contain sensor.lora1_voltage?

If it does not contain it, then the possibilities are:

  1. Home Assistant never used your MQTT Sensor configuration to create sensor.lora1_voltage.
  2. Home Assistant did use your configuration but later on you renamed it (via the UI) from sensor.lora1_voltage to sensor.lora_voltage.

No, there is no sensor.lora1_voltage. But I’m a little bit confused now. Shouldn’t it be sensor.lora_1_voltage, since this is the unique id? Anyway, both aren’t there. I can’t remember to have renamed it but I have renamed the entity right now to sensor.lora_1_voltage though but would it fix this anyway?

Then it implies Home Assistant has not used this MQTT Sensor configuration:

        - name: "Lora1 Voltage"
          unique_id: "lora_1_voltage"
          ... etc ...

When you supply a value for the name option, Home Assistant will use that to create an entity_id.

Unlikely.

It’s challenging to provides definitive answers because the presented evidence is contradictory. The MQTT Sensor’s name is “Lora1 Voltage” so that should create sensor.lora1_voltage, but this entity_id doesn’t exist in Developer tools → States.

This may sound far-fetched but is it possible that Home Assistant is discovering your LORA sensors and creating sensor entities automatically?

That would be nice, but I only had the entities after I had edited the config.yaml. If I throw one of them out, the corresponding sensor is no longer recognised.
Can I reset this somehow or do I have any other options?

Do this on a PC (so you can see all columns in Settings → Devices & Services → Entities):

  1. Remove the configuration of all MQTT Sensors you defined in configuration.yaml

  2. Restart Home Assistant.

  3. Go to Settings → Devices & Services → Entities

  4. If you see any of the sensors you created still listed here, click the icon displayed in the rightmost column and then delete the sensor.

  5. Do the same in Developer Tools → Statistics

  6. Restart Home Assistant.

  7. There should be no trace any of MQTT Sensors you created.

Re-enter the MQTT Sensor configuration into configuration.yaml. Ensure the name fields contain correct values. Restart Home Assistant.

O boy, I have no clue what happened, but just out of curiosity I have renamed the voltage entity as sensor.lora1_voltage and now from all lora1 entities long-term data is recorded, although I haven’t renamed all of them accordingly.
However, the lora2 entities are all called according to the mqtt sensor config and there is still no long-term data. I’m getting desperate. I will try to delete everything from lora2 as you recommended and see if this will fix it. I really have no clue what is wrong with it.

Well, just in case somebody is running into the same problem. For me it helped to rename any entity of the device to anything else to make long term recording work and then again to the old entity. I guess this is just a bug. Anyway, thanks for your help.

I have several MQTT Sensors, whose history is recorded in long-term statistics, and I have never had to do what you have described (nor have I seen anyone else mention the need for it).

It’s clear from the contradictory evidence you have provided that the problem was caused by something you did in the past that wasn’t explicitly mentioned here. You did something in order to create sensors with entity_ids that do not match the names in the configurations you posted. They don’t just do that on their own.

The only way to confirm it’s an actual bug is to explain how to duplicate it. If you don’t know, or cannot explain, the steps to produce the error, then no one replicate it to test, isolate, and fix the root cause.