MQTT Sensor - state only updating when reloading config

Hi,

I’m using a binary MQTT sensor in my configuration.yaml. The sensor is created successfully and the state is updated after a reload of the configuration.
When the state is changing on the external MQTT broker, Home Assistant does not update the value of the sensor. The state of the sensor in Home Assistant will only update when there is a reboot or a reload of the configuration.

Does somebody have an idea what I can do?

I configured the senosr in the following way:

mqtt:
  - binary_sensor:
      name: "EVCC smartCostActive"
      state_topic: "haustechnik/evcc/site/smartCostActive"
      payload_on: "true"
      payload_off: "false"

Thanks in advance!

1 Like

You need to add value_template. It should be sufficient to add it like this:

mqtt:
  - binary_sensor:
      name: "EVCC smartCostActive"
      state_topic: "haustechnik/evcc/site/smartCostActive"
      value_template: '{{ value }}'
      payload_on: "true"
      payload_off: "false"

This means the state is the value that the state_topic returns. If its true/false like in your case or on/off then this should work.

Thanks for your reply. Unfortunately, this is not working. The sensor state is just not updating. Only when reloading the configuration, the sensor gets an update.
In MQTT Explorer I can confirm that the topic on the broker is up2date.

Please provide details about your HA MQTT configuration. Do you have MQTT related log entries in HA? In HA you can also enable debug logs for MQTT to get more detailed infos. Do you have other MQTT sensors configured in HA or is this the only one? Also share details from the related messages in MQTT Explorer.

Thanks for your reply. This is the only MQTT sensor I have cofngiured. After your hint, I activaated the MQTT debug log. The log shows, that HA can see the topic state change in the broker. But it does not set the state of the sensor when the topic value changes. The sensor still changes only on configuration reload.
The debug log is like

2023-11-20 22:45:58.456 DEBUG (MainThread) [homeassistant.components.mqtt.client] Subscribing to haustechnik/evcc/site/smartCostActive, mid: 28, qos: 0
2023-11-20 22:45:58.661 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received message on haustechnik/evcc/site/smartCostActive (qos=0): b'false'
2023-11-20 22:45:58.661 DEBUG (MainThread) [homeassistant.components.mqtt.models] Rendering incoming payload 'false' with variables {'entity_id': 'binary_sensor.evcc_smartcostactive', 'name': 'EVCC smartCostActive', 'this': <template TemplateStateFromEntityId(binary_sensor.evcc_smartcostactive)>} and Template<template=({{ value }}) renders=0>
2023-11-20 22:45:59.249 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received retained message on haustechnik/evcc/site/smartCostActive (qos=0): b'false'
2023-11-20 22:45:59.249 DEBUG (MainThread) [homeassistant.components.mqtt.models] Rendering incoming payload 'false' with variables {'entity_id': 'binary_sensor.evcc_smartcostactive', 'name': 'EVCC smartCostActive', 'this': <template TemplateStateFromEntityId(binary_sensor.evcc_smartcostactive)>} and Template<template=({{ value }}) renders=1>
2023-11-20 22:46:09.254 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received retained message on haustechnik/evcc/site/smartCostActive (qos=0): b'false'
2023-11-20 22:46:19.256 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received retained message on haustechnik/evcc/site/smartCostActive (qos=0): b'false'
2023-11-20 22:46:29.255 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received retained message on haustechnik/evcc/site/smartCostActive (qos=0): b'false'
2023-11-20 22:46:31.767 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received retained message on haustechnik/evcc/site/smartCostActive (qos=0): b'false'
2023-11-20 22:46:39.253 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received retained message on haustechnik/evcc/site/smartCostActive (qos=0): b'false'
2023-11-20 22:46:49.259 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received retained message on haustechnik/evcc/site/smartCostActive (qos=0): b'true'
2023-11-20 22:46:59.254 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received retained message on haustechnik/evcc/site/smartCostActive (qos=0): b'true'
2023-11-20 22:47:09.259 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received retained message on haustechnik/evcc/site/smartCostActive (qos=0): b'true'

The “b” seems strange. How does the MQTT message look like in MQTT Explorer? The JSON structure is key here.

EDIT:
I just realized that you updated the debug log. So as you said the sensor is updated after you rebooted HA.

Which MQTT broker are you using and is it running in the same network as HA?

Sorry, yes…I updated the debug log when I saw the initial state change in the log.

I’m using MQTT broker instance on ioBroker. It’s located in the same network.

This is a screenshot showing the topic in MQTT Explorer:

This is strange. It could be an incompatibility issue with iobroker MQTT.
In fact I just found that someone had the same problem but it seems it did not get fixed:
https://community.home-assistant.io/t/mqtt-sensor-stopped-working-after-ha-update/629882

This just came to my mind: can you send MQTT from iobroker to another MQTT broker? If that is possible you could use the Mosquitto broker add-on for HA. Then send the data from iobroker to it and have HA to use Mosquitto instead of iobroker.

I will first try the HA Mosquitto solo with one topic. So maybe we will see, if the problem still exists this way.

Meanwhile I watched my MQTT debug log again and found some other entries. They are my NUKI Lock, which is integrated as device in HA. Seems like it is also based on the MQTT integration. The states are up2date, even the MQTT debug log shows, that all MQTT messages are received with the “b” prefix. So, the interpretation of the “b” messages works in HA in some way…

2023-11-21 09:05:39.253 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received retained message on haustechnik/evcc/site/smartCostActive (qos=0): b'true'

2023-11-21 09:05:40.157 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received retained message on nuki/battery/voltage (qos=0): b'5.64'

2023-11-21 09:05:40.463 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received retained message on nuki/configuration/buttonEnabled (qos=0): b'1'

2023-11-21 09:05:40.464 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received retained message on nuki/configuration/ledEnabled (qos=0): b'1'

2023-11-21 09:05:40.465 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received retained message on nuki/configuration/ledBrightness (qos=0): b'3'

2023-11-21 09:05:40.467 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received retained message on nuki/info/firmwareVersion (qos=0): b'3.6.9'

2023-11-21 09:05:40.468 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received retained message on nuki/info/hardwareVersion (qos=0): b'4.1'

2023-11-21 09:05:49.250 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received retained message on haustechnik/evcc/site/smartCostActive (qos=0): b'true'

2023-11-21 09:05:59.251 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received retained message on haustechnik/evcc/site/smartCostActive (qos=0): b'true'

2023-11-21 09:06:09.250 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received retained message on haustechnik/evcc/site/smartCostActive (qos=0): b'true'

EDIT: The NUKI sensors are not manually set up in the configuration.yaml.

Seems it has to do with the broker. When using the Mosquitto Addon in HA, the sensor state gets updated without any problems - even the MQTT message is showing the “b” prefix in debug log.

2023-11-21 13:14:09.713 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received retained message on haustechnik/evcc/site/smartCostActive (qos=0): b'false'

2023-11-21 13:14:09.713 DEBUG (MainThread) [homeassistant.components.mqtt.models] Rendering incoming payload 'false' with variables {'entity_id': 'binary_sensor.evcc_smartcostactive', 'name': 'EVCC smartCostActive', 'this': <template TemplateStateFromEntityId(binary_sensor.evcc_smartcostactive)>} and Template<template=({{ value }}) renders=0>

2023-11-21 13:14:11.713 DEBUG (MainThread) [homeassistant.components.mqtt.client] Transmitting message on homeassistant/status: 'online', mid: 3, qos: 0

2023-11-21 13:14:26.937 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received message on haustechnik/evcc/site/smartCostActive (qos=0): b'false'

2023-11-21 13:14:26.937 DEBUG (MainThread) [homeassistant.components.mqtt.models] Rendering incoming payload 'false' with variables {'entity_id': 'binary_sensor.evcc_smartcostactive', 'name': 'EVCC smartCostActive', 'this': <template TemplateStateFromEntityId(binary_sensor.evcc_smartcostactive)>} and Template<template=({{ value }}) renders=1>

2023-11-21 13:14:57.527 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received message on haustechnik/evcc/site/smartCostActive (qos=0): b'false'

2023-11-21 13:14:57.527 DEBUG (MainThread) [homeassistant.components.mqtt.models] Rendering incoming payload 'false' with variables {'entity_id': 'binary_sensor.evcc_smartcostactive', 'name': 'EVCC smartCostActive', 'this': <template TemplateStateFromEntityId(binary_sensor.evcc_smartcostactive)>} and Template<template=({{ value }}) renders=2>

2023-11-21 13:15:28.198 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received message on haustechnik/evcc/site/smartCostActive (qos=0): b'true'

2023-11-21 13:15:28.198 DEBUG (MainThread) [homeassistant.components.mqtt.models] Rendering incoming payload 'true' with variables {'entity_id': 'binary_sensor.evcc_smartcostactive', 'name': 'EVCC smartCostActive', 'this': <template TemplateStateFromEntityId(binary_sensor.evcc_smartcostactive)>} and Template<template=({{ value }}) renders=3>

2023-11-21 13:15:58.838 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received message on haustechnik/evcc/site/smartCostActive (qos=0): b'false'

2023-11-21 13:15:58.838 DEBUG (MainThread) [homeassistant.components.mqtt.models] Rendering incoming payload 'false' with variables {'entity_id': 'binary_sensor.evcc_smartcostactive', 'name': 'EVCC smartCostActive', 'this': <template TemplateStateFromEntityId(binary_sensor.evcc_smartcostactive)>} and Template<template=({{ value }}) renders=4>

Got it now working with the ioBroker MQTT instance. In the setting of the instace, there is an option “send messages without retain-Flag”. This option has to be ACTIVATED. Otherwise HA will not switch the sensor state. This seems to be an incompatibility between ioBroker and HA.

Thanks for your help. You guided me the right way :wink: Glad it’s working now.

2 Likes

Got the same problem :frowning:
Unfortunately setting the “send messages without retain-Flag” didn’t work for me. According to MQTT Explorer there still is a “retained” flag on that topic.

In addition to that, every time I restart the MQTT broker (Iobroker instance), an unlatch action is triggered and my door swings open…

Any ideas?