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.67k stars 30.8k forks source link

MQTT auto configure message cause sensor last updated to change #26725

Closed aherbjornsen closed 4 years ago

aherbjornsen commented 5 years ago

Home Assistant release with the issue: 0.99

Last working Home Assistant release (if known):

Operating environment (Hass.io/Docker/Windows/etc.): Hass.io

Component/platform: MQTT

https://www.home-assistant.io/components/mqtt/

Description of problem:

Auto discovery messages (/homeassistant/..../config)) will cause the corresponding sensor to update the "last_updated" time.

Problem-relevant configuration.yaml entries and (fill out even if it seems unimportant):

mqtt:
  discovery: true
  broker: 10.0.0.182
  port: 1883
  client_id: slemme-ha
  keepalive: 60
  username: !secret mqtt_username
  password: !secret mqtt_password
  protocol: 3.1

Traceback (if applicable):

Additional information: I monitor a lot of MiFlora units using plantgw (https://github.com/ChristianKuehnel/plantgateway/tree/master/plantgw). It will send MQTT auto configure messages for all units/sensors, even if they are stale at the moment. Upon receipt of the auto configure MQTT message Hass will change the last updated time of the corresponding sensor. This makes it impossible to track stale sensors.

probot-home-assistant[bot] commented 5 years ago

Hey there @home-assistant/core, mind taking a look at this issue as its been labeled with a integration (mqtt) you are listed as a codeowner for? Thanks!

emontnemery commented 5 years ago

@aherbjornsen I can't reproduce the issue; last_updated is not changed when MQTT discovery message is received unless something has been changed. Can you provide an example of an MQTT disovery message which triggers this issue?

aherbjornsen commented 5 years ago

These are messages from plantgw (https://github.com/ChristianKuehnel/plantgateway/tree/master/plantgw), which relays data from my MiFlora sensors.

They typically look like this:

homeassistant/sensor/plant_C47C8D6B0E54_moisture/config {"name": "potte5_moisture", "unit_of_measurement": "%", "force_update": true, "value_template": "{{value_json.moisture}}", "state_topic": "slemme/hjemme/my/plants/potte5/"}

Whenever this message is received. HASS will set the last_updated time of "sensor.potte5_moisture".

balloob commented 5 years ago

This happens because you have force_update set to true.

aherbjornsen commented 5 years ago

@balloob Then I do not fully grasp how the force_update parameter is supposed to work.

What I need is for the sensor "last_updated" to change whenever a new value is received, even if that value is the same as the previous one.

I was hoping that adding "force_update" to the "config" message would make that sensor behave like a manually defined mqtt sensor with "force_update" set to true (which does what I describe above).

I see no reason to update the sensor "last_updated" just because a "config" message is received.

I've tried adding "force_update: true" to the MQTT message with new sensor values. That does not cause "last_updated" to change, if the new value is the same as the old.

JonasR-M commented 5 years ago

@balloob I am facing the same issue, but with my homebrew Z-Wave MQTT integration. In particular Scene controllers which often will have same state, but needs to trigger each time button is clicked - hence force_update must be set.

@emontnemery Looking at the MQTT/discovery.py code (ln 178), it seems no distinction is made if something actually changed in the config push. Looking at eg. MQTT/sensor.py (ln 142) a call is made to self.async_write_ha_state() indiscriminately every time a discovery_update is called.

I don't see why state should update at all - and I think it's dangerous practice to retrigger with last known state, this could set off alarm systems, unlock doors etc. just because an MQTT client reconnects and pushes its devices again.

In addition same thing can actually be seen, just by using Developer Tools ->MQTT and subscribe to #.

This issue should affect any MQTT integration that uses discovery, and in my case results in some 50+ events to trigger and scenes to activate erroneously.

stale[bot] commented 4 years 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 now has been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.