ble_monitor icon indicating copy to clipboard operation
ble_monitor copied to clipboard

Is it possible to hide sensors attributes?

Open chertvl opened this issue 2 years ago • 6 comments

What happened?

Hello, Ernst. A little question about basic functionality. Is it possible to disable providing all attributes for some device sensors?

For example, I have several LYWSD03MMC thermometers, each of them creates main: Temperature, Humidity and additional: Battery / Voltage / RSSI / Device tracker entities. Each of those sensor have all possible attributes, for example RSSI. It cause to state_changed event every time when one of attribute changes, for example, RSSI. So we have 6 entities with 7-10 attributes which updates every minute or less for each TH device. Or MiFlora which sending something every 15 sec. This causes huge database size growth.

I had to exclude additional sensors from recorder (Battery, RSSI, Voltage, Device tracker), but now I can't look into battery sensor history and long-term statistics.

It would be great if I will have a way to disable providing all of these attributes for additional entities. If battery sensor will have only state (without quickly changing attributes), it will use database more rationally, writing in database less information (only state) and only when battery changes (not RSSI attribute etc).

Sensor type

No response

Relevant log output

No response

chertvl avatar Apr 19 '22 12:04 chertvl

As far as I know, this is not possible. However, a temperature sensor should be such, that it only updates once per period (default 60 seconds). If it is updating more frequent, than there is something wrong/ a bug. Are you sure the MiFlora is updating every 15 seconds? What value did you configure for the period option?

Ernst79 avatar Apr 29 '22 11:04 Ernst79

Are you sure the MiFlora is updating every 15 seconds? What value did you configure for the period option?

I did not use period option. My bad, all miflora entities updates every 1 min too. But I dont know what for I need _battery, _voltage and device_tracker entities updates every 1 minute without state changes (changing only RSSI attrubute in all of these entities, thats makes my database huge growth). I want to use long-therm statistics for batteries, for example, but I have 1440 records in db-table per day for each: battery, voltage, device_tracker. I have only 5 ble devices (1x miflora, 3x lywsd03mmc, 1x T500 toothbrush), thats causes 22000+ records in database per day for this entities with measurement state-class.

In normal use my db size is ~50mb per 1 day, I had to exclude_glob this 3 entities (voltage, battery, device_tracker) from recorder, because without excluding i have +30mb per day (almost twice as much), useless records for battery entities.

chertvl avatar Apr 29 '22 17:04 chertvl

What value did you configure for the period option?

Now i tried deleting a couple of lines in the file sensor.py: For 'class MeasuringSensor(BaseSensor)' removed:


            state_median = round(sts.median(measurements), rdecimals)
            state_mean = round(sts.mean(measurements), rdecimals)
            if self._use_median:
                textattr = "last_median_of"
                self._state = state_median
            else:
                textattr = "last_mean_of"
                self._state = state_mean
            self._extra_state_attributes[textattr] = len(measurements)
            self._extra_state_attributes["median"] = state_median
            self._extra_state_attributes["mean"] = state_mean
            if self.entity_description.key != "rssi":
                self._extra_state_attributes["rssi"] = round(sts.mean(self.rssi_values))
            if self._period_cnt >= 1:
                self._measurements.clear()
                self.rssi_values.clear()

for temperature, battery and humidity sensors removed: self._extra_state_attributes["last_packet_id"] = data["packet"]

And if I'm not mistaken, for now with this modification HA creates record in DB file only when sensor state is changed (because only unchanged attributes remains for this sensors class, like type or mac). I'll write my observations in a couple of days, but I expect that the battery, temperature and humidity sensor will create much fewer entries in the database, because 90% of the time they do not change (attributes such as rssi, last pack id provoked extra entries and growth Database).

chertvl avatar Jun 30 '22 15:06 chertvl

Nothing succeeded, HA still creates new DB entries for every sensor update, I dont know why, because only context.id attribute changes now.


{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.bedroom_humidity",
        "old_state": {
            "entity_id": "sensor.bedroom_humidity",
            "state": "41",
            "attributes": {
                "state_class": "measurement",
                "sensor_type": "LYWSD03MMC",
                "mac_address": "A4:C1:38:31:27:E5",
                "median": 41,
                "mean": 41,
                "last_mean_of": 20,
                "rssi": -64,
                "firmware": "Xiaomi (MiBeacon V3)",
                "last_packet_id": 233,
                "battery_level": 57,
                "uuid": "A4:C1:38:31:27:E5",
                "unit_of_measurement": "%",
                "device_class": "humidity",
                "friendly_name": "ble humidity Спальня TH",
                "entity_picture": "/local/myicons/humi.png"
            },
            "last_changed": "2022-06-30T15:45:19.268859+00:00",
            "last_updated": "2022-06-30T15:45:19.268859+00:00",
            "context": {
                "id": "01G6TMKGX45BJ72PW276NT5H1Y",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.bedroom_humidity",
            "state": "41",
            "attributes": {
                "state_class": "measurement",
                "sensor_type": "LYWSD03MMC",
                "mac_address": "A4:C1:38:31:27:E5",
                "median": 41,
                "mean": 41,
                "last_mean_of": 20,
                "rssi": -64,
                "firmware": "Xiaomi (MiBeacon V3)",
                "last_packet_id": 233,
                "battery_level": 57,
                "uuid": "A4:C1:38:31:27:E5",
                "unit_of_measurement": "%",
                "device_class": "humidity",
                "friendly_name": "ble humidity Спальня TH",
                "entity_picture": "/local/myicons/humi.png"
            },
            "last_changed": "2022-06-30T15:46:19.761554+00:00",
            "last_updated": "2022-06-30T15:46:19.761554+00:00",
            "context": {
                "id": "01G6TMNBZHJVNAE5PJCKXB637S",
                "parent_id": null,
                "user_id": null
            }
        }
    },

chertvl avatar Jun 30 '22 15:06 chertvl

The "problem" is that other sensors, like the LYWSD03MMC with ATC firmware, are sending data every second, with 2 decimals precision. This change will probably work for sensors with less accuracy, like battery sensors, but not for these high accuracy readings that change a lot. Perhaps we can make it an option, to

a) only update on changes of the main reading (e.g. on temperature change, but not update on RSSI and packet ID change) b) update every period (like is implemented now c) ....

Only difficulty I see is that most sensors have multiple useful sensors, so we have to define what is a "main reading"

Ernst79 avatar Jun 30 '22 15:06 Ernst79

a) only update on changes of the main reading (e.g. on temperature change, but not update on RSSI and packet ID change)

This is exactly the option I would like to achieve. I dont have many various devices (only 5x lywsd03mcc, T500 toothbrush, miflora and miscale), and all "main reading" have sensor entities, so for me "main readings" == sensors states. Maybe it's possible to make an additional custom boolean in the config? "true" - update entities when any reading change, "false" - update only when sensor main reading (state) changes. (defaut: true, for non-breaking-change)

chertvl avatar Jun 30 '22 15:06 chertvl