[Long-Term Statistics] How to add a custom entity text state to LTS?

Hi2UAll,

I have a question about the Long-Term Statistics (LTS) used by recorder, stored in the Home Assistant (HA) default Database (DB).

As an example I use a Virtual Entity (VE) configured by MQTT using Node-RED (NR).

Reason:
I have an old config (>5 years) which uses security states like who is at home, burgler/fire/flood states, Presence Detection and so on.

The VE MQTT sensors I use presents the state in a text string. For example the home state of my house is presented as Thuis, Bewaakt, Weg, Slaap, Vakantie (Home/Waiting to arrive/Away/Sleeping/Holliday).

In Node-Red I configured a sensor by using a Function Node configured to add it to MQTT, installing the sensor automatically in HA.

Part of my payload:

var _payload = {
    "name": "Bewonersstatus",
    "icon": "hassio:home-circle-outline",
    "unique_id": "mqtt_bewonerstatus",
    "platform": "mqtt",
    "state_topic": _topic + "/state",
    "device": {
        "name": "Mqtt Security",
        "manufacturer": "MQTT Hass.io",
        "model": "Custom",
        "identifiers": ["Mqtt Security"],
        "sw_version": "20230803",
        "suggested_area": "Algemeen"
        }
    };

The question:
How can I configure my VE to store a text-value to the LTS?
Do I have just to add "state_class": "measurement", or do I have to add also "device_class": "WHATIS_NEEDED_HERE" to get it working?

Please help, I would highly appreciate it.

1 Like

Afaik LTS is numerical floating point only. it stores aggegated min/max/average per hour, which you can’t for text. Even if you’d use indices per state you would end up with fractions. (Vacation + Home + Vacation) / 3 = Sleep?

1 Like

Thanks Edwin, I was afraid of that, thanks for confirming.

So, in short, I could use a numeric value to the specific state, and use that as state in my VE, with expansion of the VE with the `“state_class”: “measurement” and use of “device_class”?
Which device class should/could I pick?

Yes you could, and data would be useless. If you have both state 1 and 3 in the same hour, your data would show 2, which never happened. For real numeric LTS, the device class does not matter. It just needs a unit_of_measurement to show it is numeric and a proper state_class to determine the type of LTS storage needed.

Oeps, you’r absolutly right, never thought about that! :roll_eyes:

Clear!

So in basic, it’s not doable. Thanks for your explanations, it will save me a lot of trying/debugging/time! :sweat_smile: