MQTT Sensors failing

when looking to the debug logs of HA 40.0 I see:

INFO:homeassistant.components.mqtt:Successfully reconnected to the MQTT server
INFO:homeassistant.components.mqtt:Successfully reconnected to the MQTT server
INFO:homeassistant.components.mqtt:Successfully reconnected to the MQTT server

This does not happen in 41.x

So does your broker log show HA connected or disconnected?

point being that in 40.x mqtt seems to reconnect every now and again so that might be the reason that this issues does not show in 40.x.

I also read something else:

Update on the MQTT Out of Memory errors in 0.39

MQTT started causing “Out of Memory” errors for some people on a Raspberry Pi. We have been able to track this down to Raspberry Pis that are using an older firmware. If you are experiencing this issue, please upgrade your firmware using rpi-update.

(https://github.com/Hexxeh/rpi-update#installing)

Ignore this about mqtt and reconnecting, there were some dependency issues. It now does not reconnect every now and again :slight_smile:

Be careful when updating firmware as the latest build (4.9+) causes pilight to fail, this due to a dependency on wiringX which is not currently compatible, there is a work around but its a pain, I just downgraded to 4.4. If not using pilight then its probably ok :slight_smile:

It seems that when the MQTT connection disconnects and reconnects HA does not see (all off) the subscribed topics anymore. this seems to happen exactly at the time of a syslog rotate which causes mosquitto to reload it’s config. So all in all it seems HA drops MQTT monitoring (of at least some topics) after a hickup in mqtt.

That’s interesting.

You can get mosquitto to reload its config by sending a sighup

$ sudo kill -HUP <pid of mosquitto process>

and you can subscribe to the topic $SYS/broker/subscriptions/count to get the number of current subscriptions. If you monitor this you can see if HA stops subscribing, or if it just stops noticing the data.

Thanks, that seems to confirm it:

$SYS/broker/subscriptions/count 17
kill -HUP $PID
$SYS/broker/subscriptions/count 5

There is something special about your subscriptions, because mine all stay connected. What do you have defined?

mqtt:
discovery: true
broker: 192.168.1.12
client_id: hassbian
keepalive: 15

sensor:

  • platform: mqtt
    state_topic: “home/badkamer/temp”
    name: ‘Badkamer Temperatuur’
    force_update: true
  • platform: mqtt
    state_topic: “home/badkamer/humidity”
    name: ‘Badkamer Vochtigheid’
    force_update: true
  • platform: mqtt
    state_topic: “home/badkamer/dht11/battery”
    name: ‘Badkamer sensor battery level’
    force_update: true
  • platform: mqtt
    state_topic: “hue/woonkamer_dimmer/buttonevent”
    name: ‘Woonkamer Dimmer’
    force_update: true
  • platform: mqtt
    state_topic: “hue/keuken/buttonevent”
    name: ‘Keuken Dimmer’
    force_update: true
  • platform: mqtt
    state_topic: “hue/slaapkamer/buttonevent”
    name: ‘Slaapkamer Dimmer’
    force_update: true
  • platform: mqtt
    state_topic: “hue/hue_ambient_light_sensor_2/lightlevel”
    unit_of_measurement: Lux
    name: ‘Keuken Licht’
    force_update: true
  • platform: mqtt
    state_topic: “hue/hue_ambient_light_sensor_3/lightlevel”
    unit_of_measurement: Lux
    name: ‘Woonkamer Licht’
    force_update: true
  • platform: mqtt
    state_topic: “hue/hue_ambient_light_sensor_1/lightlevel”
    unit_of_measurement: Lux
    name: ‘Slaapkamer Licht’
    force_update: true

binary_sensor:

  • platform: mqtt
    state_topic: “hue/woonkamer_motion_detector/presence”
    name: Beweging Woonkamer
    payload_on: ‘true’
    payload_off: ‘false’
    device_class: moving
    force_update: true
  • platform: mqtt
    state_topic: “hue/slaapkamer_motion_detector/presence”
    name: Beweging Slaapkamer
    payload_on: ‘true’
    payload_off: ‘false’
    device_class: moving
    force_update: true
  • platform: mqtt
    state_topic: “hue/keuken_motion_detector/presence”
    name: Beweging Keuken
    payload_on: ‘true’
    payload_off: ‘false’
    device_class: moving
    force_update: true

And that is it.
The force update ad recently added to check if it would help, it doesnt :slight_smile:

There is nothing there I can see. The only difference is that I don’t have discovery set and I allow the client id to be set by HA, rather than defining it.

The other thing is the version of mosquitto you are running. The only way I know to find that out is

$ dpkg --status mosquitto

if you have installed via apt-get

I have, on raspbian:

Package: mosquitto
Status: install ok installed
Priority: optional
Section: net
Installed-Size: 186
Maintainer: Roger A. Light [email protected]
Architecture: armhf
Version: 1.3.4-2
Depends: libc6 (>= 2.4), libssl1.0.0 (>= 1.0.1), libwrap0 (>= 7.6-4~), adduser (>= 3.10), lsb-base (>= 4.1+Debian3), libuuid1
Conffiles:
/etc/mosquitto/mosquitto.conf 379a6d6e30d19bfdedf8099a9f3f8770
/etc/mosquitto/conf.d/README b4ac621550824082a735732bfb42b51d
/etc/mosquitto/ca_certificates/README c1c6ae67f2def06c6a483be09b49d4de
/etc/mosquitto/certs/README 4d8a70d4cefab07d4dabc5be1f786c1f
/etc/init/mosquitto.conf 97d9bc16841e4ba66a68ef1ee280fd9f
/etc/init.d/mosquitto 7c1c057c3c625deaac042ba97032ec69
/etc/logrotate.d/mosquitto 7f203558c910819d152ee3f033063cff
Description: MQTT version 3.1/3.1.1 compatible message broker
This is a message broker that supports version 3.1 and 3.1.1 of the MQTT
protocol.
.
MQTT provides a method of carrying out messaging using a publish/subscribe
model. It is lightweight, both in terms of bandwidth usage and ease of
implementation. This makes it particularly useful at the edge of the network
where a sensor or other simple device may be implemented using an arduino for
example.
Homepage: http://mosquitto.org/

root@hassbian:/home/homeassistant/.homeassistant# grep topic configuration.yaml |wc -l
12

it fits with the amount falling away. Also using owntrack though, which also does mqtt.

My mosquitto is up to version 1.4.11, so I think it is worth trying to update that.

Have you done and sudo apt-get update then sudo apt-get upgrade recently. If you have, and still get the same version, there are instructions for using the latest version of mosquitto from their repository at https://mosquitto.org/2013/01/mosquitto-debian-repository/

Looking at my files, I haven’t had to do that on this box, so I think an ordinary update on Jessie would do it. Assuming you are using Jessie, not Wheezy?

Just updated to 1.4.11, but no difference i’m afraid. dropping from 16 to 4. Are you running mosquitto on the same host as HA , or another?

Hmmz, I had a bunch of extra logging config options in mosquitto.conf. Removing those seemed to fix it.

That being said, restarting mosquitto still drops most of the mqtt subscriptions from HA, without HA recovering those subscriptions.

Yes, I always thought someone would fix that but no sign of it so far. But I haven’t been all that bothered as mosquitto has never stopped on my system. The only problem was getting mosquitto to start before HA on boot.

Also running 0.41.0 as a development version as I only set it up very recently.

I have HA configured to do an auto restart every night at 3:00 am. If I click on the temp sensor the next day I always seem to get one reading corresponding to the restart time but no state history after that. I am assuming that mosquitto is running continuously and hence is started before HA starts.