home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.22k stars 30.58k forks source link

Threshold state/attr updates when underlying entity changes, but threshold unchanged #80912

Closed rct closed 10 months ago

rct commented 2 years ago

The problem

The threshold integration appears to generate a state update and a shared attribute update each time the source entity the threshold is based on changes. This occurs even if the threshold entity itself does not change.

When the source entity changes, it's value is saved as the attribute sensor_value for the threshold entity.

If the threshold entity is based on an entity that changes frequently, like power consumption or temperature, this can generate a lot of rows in the recorder DB.

While this might be useful for debugging the threshold sensor, the data for the source entity is presumably already stored in recorder. So adding a threshold sensor multiplies the number of updates to recorder.

From a UX perspective, in can be surprising that a threshold helper might contribute to DB growth even if the threshold entity rarely changes.

Related community thread and comment from bdraco: https://community.home-assistant.io/t/threshold-helper-update-rate-db-size-state-rows/479192/5?u=rct

Having the sensor_value seems like debug information that probably shouldn’t be in the attributes since it is already available in the state machine, and already recorded in the database.

I think it would be reasonable to remove it.

What version of Home Assistant Core has the issue?

2022.10.4

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Supervised

Integration causing the issue

Threshold

Link to integration documentation on our website

https://www.home-assistant.io/integrations/threshold/

Diagnostics information

No response

Example YAML snippet

The threshold sensor was configured via the UI.  The JSON from `core.config_entries`:

      {
        "entry_id": "ecf137fb9c1c3a7c91859b810a8e2d43",
        "version": 1,
        "domain": "threshold",
        "title": "Ice Maker Low Power Use",
        "data": {},
        "options": {
          "lower": 1.5,
          "upper": null,
          "name": "Ice Maker Low Power Use",
          "entity_id": "sensor.ice_maker_current_consumption",
          "hysteresis": 0.0
        },
        "pref_disable_new_entities": false,
        "pref_disable_polling": false,
        "source": "user",
        "unique_id": null,
        "disabled_by": null
      },

Anything in the logs that might be useful for us?

No response

Additional information

The use case for the threshold sensor is to monitor power usage, via a TPLink HS110 (built in TP Link Kasa Smart integration), and alert/take action if the power usage is below the expected range (eg the device has been powered off or has malfunctioned.)

The source entity for the threshold sensor is sensor.devicename_current_consumption from the TP-Link Kasa Smart integration. That entity is updating every 20 seconds.

The threshold sensor itself has not changed state during the history of recorder DB, other than for Home Assistant restarts.

The states table rows for the threshold sensor:

image

The shared_attr data written to the state_attributes table:

'{"entity_id":"sensor.ice_maker_current_consumption","hysteresis":0.0,"lower":1.5,"position":"above","sensor_value":135.4,"type":"lower","upper":null,"device_class":"problem","icon":"mdi:snowflake-thermometer","friendly_name":"Ice Maker Low Power Use"}'

(Note that sensor_value is the only attribute that is changing if the threshold hasn't been crossed.

home-assistant[bot] commented 2 years ago

Hey there @fabaff, mind taking a look at this issue as it has been labeled with an integration (threshold) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `threshold` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Change the title of the issue. - `@home-assistant unassign threshold` Removes the current integration label and assignees on the issue, add the integration domain after the command.

(message by CodeOwnersMention)


threshold documentation threshold source (message by IssueLinks)

home-assistant[bot] commented 2 years ago

Hey there @rytilahti, @thegardenmonkey, mind taking a look at this issue as it has been labeled with an integration (tplink) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `tplink` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Change the title of the issue. - `@home-assistant unassign tplink` Removes the current integration label and assignees on the issue, add the integration domain after the command.

(message by CodeOwnersMention)


tplink documentation tplink source (message by IssueLinks)

fabaff commented 1 year ago

If the threshold entity is based on an entity that changes frequently, like power consumption or temperature, this can generate a lot of rows in the recorder DB.

It's possible in recorder to exclude certain elements from being logged.

phinor commented 1 year ago

I have a threshold monitor to keep track of mains electricity supply ("Eskom Supply"). If the supply goes, then the pond pump must turn off to conserve battery.

image

The routine is being triggered every couple of seconds:

image

This is in spite of the state of the sensor not changing:

image

This seems to run contrary to monitoring state changes in other sensors which reflect changes only when state changes rather than when state is checked.

rct commented 1 year ago

If the threshold entity is based on an entity that changes frequently, like power consumption or temperature, this can generate a lot of rows in the recorder DB.

It's possible in recorder to exclude certain elements from being logged.

Assuming by elements you mean entities?

I could be wrong, however it seems the threshold sensor itself would have to be excluded from recorder. And the bulk of the space in the recorder DB is from the threshold sensor's attributes which record the state of the checked entity every time it changes.

Excluding threshold sensors from the recorder requires both editing YAML and a Home Assistant restart. So if one is going to wind up having to do all of that, then it would just be better to define your own template binary sensor and avoid the threshold sensor.

phinor commented 1 year ago

One further thing I've noticed - because of the constant triggering of the threshold sensor in my automation, I can no longer turn on the pump manually outside of the scheduled time without first disabling the automation.

Surely the threshold sensor should differentiate when it is "updated" vs when it is "changed"? The automation suggests that it requires the threshold sensor to "change" ("on" to "off", or vice versa). Currently, it appears to trigger whenever the sensor is "updated."

N3rdix commented 1 year ago

I also have an issue with this behavior as it constantly writes entries to the states DB table. I have added the threshold sensor to the recorder on purpose and have thousand of rows in the DB although the state of this sensor never changed.

Wouldn't it be valid to change this behavior to just record changed values? I believe this is the case in other parts, like a template binary sensor which reacts on frequent changes of another entity...

rct commented 1 year ago

@fabaff - in the 5+ months this issue has been opened, you've only made a single comment--to exclude threshold from recorder.

Do you feel the current behavior is appropriate and disagree with the issues people have raised here?

22hn commented 1 year ago

Add me to the list of people having issues with this...

My automation gets triggered every time the entity that the threshold helper is based on changes, even if the helper it self isn't changed. Filling up the logs...

spartan117aut commented 1 year ago

I have the same problem. Threshold based on sun azimuth, used as automation trigger - gets triggered on every azimuth change (which is a lot!) though the threshold state hasn't changed..

So if one is going to wind up having to do all of that, then it would just be better to define your own template binary sensor and avoid the threshold sensor.

This.

issue-triage-workflows[bot] commented 1 year ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

rct commented 1 year ago

I need to test on 2023.9, but I don't think this ever got fixed. I've changed any threshold sensors over to binary template sensors.

issue-triage-workflows[bot] commented 10 months ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.