core: MQTT: Updates to templates do not take effect before restart

The problem

When pushing MQTT discovery documents, using value templates, the new templates do not take effect until HASS is restarted.

Environment

  • Home Assistant Core release with the issue: 0.109.6
  • Last working Home Assistant Core release (if known): N/A
  • Operating environment (Home Assistant/Supervised/Docker/venv): Docker
  • Integration causing this issue: MQTT
  • Link to integration documentation on our website: Link

Problem-relevant configuration.yaml

# Configure a default setup of Home Assistant (frontend, api, etc)
mqtt:
  broker: queue.home
  username: home-assistant
  password: home-assistant
  discovery: true
  discovery_prefix: homeassistant
  birth_message:
    topic: 'hass/status'
    payload: 'online'
  will_message:
    topic: 'hass/status'
    payload: 'offline'

Traceback/Error logs

In my specific case, I was missing a template to extract the value from my sensor. So I received errors like this:

2020-05-15 21:32:01 ERROR (MainThread) [homeassistant.util.logging] Exception in brightness_received when handling msg on 'zwave2mqtt/powerplug_3/38/1/0': '{"time":1589571121863,"value":0}'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/mqtt/debug_info.py", line 35, in wrapper
    msg_callback(msg)
  File "/usr/src/homeassistant/homeassistant/components/mqtt/light/schema_basic.py", line 328, in brightness_received
    device_value = float(payload)
ValueError: could not convert string to float: '{"time":1589571121863,"value":0}'

I pushed a new discovery config document with a "brightness_value_template": "{{ value_json.value }}" (for my light), but the issue persisted. Only after restarting HASS, did the new template take effect.

I have the same experience with other template values in the MQTT integration.

Additional information

It should be entirely possible to reproduce this, by setting a value template that produces an error. If the error does not occur, the template hasn’t taken effect.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (12 by maintainers)

Most upvoted comments

I just realized that I linked to the wrong PR, the correction for the handling of MQTT light’s value templates is in #39325. The problem is that the template is picked from a local variable visible to the message callback closure, maybe that’s what you mean by this link isn't updated unless one of the three values there are change?

As you correctly point out this problem is not limited to MQTT.light, several other MQTT integrations have exactly the same problem, I’ll try get them all sorted.

The problem mentioned in the title (templates for lights not updating) should be solved by #39325, I didn’t notice that you see the same problem also with sensors, sorry about that.

Edit: Fix link to wrong PR