MQTT Fan turns every time HASS restarts

Background
I have setup a 433mhz fan as a MQTT fan and using a script to broadcast the signals via pilight

Problem
Every time I restart home assistant the fan comes on :frowning:

Findings

  1. Its definitely the turn_fan_on script that is triggering it, if I remove it my problem goes away
    https://github.com/ettiennegous/home-assistant-configs/blob/master/automations/fans/turn_fan_on.yaml
  2. I have found examples of others with the same issue, their solution was to set the retain to false, I tried it but it doesnt work for me
    https://github.com/ettiennegous/home-assistant-configs/blob/master/configuration.yaml#L84
  3. I have seen suggestions where you disable the automation for x seconds after hass starts and then enable it,
    Although i am sure it works its a massive hack to work around something that I must be misunderstanding so want to avoid this “solution” if possible.

Any help will be greatly appreciated.

Not at all.
I had an issue where my garage door would open/close when I restarted HA.
I set the auto that were causing problems to

initial_state:false

I then created another automation to turn these back on 10 minutes after a restart.

Done.

I feel like it’s a bandaid fix if you are doing something to work around a problem you don’t understand. (Maybe it’s just me)
Also from what I understand you are saying you can’t operate your garage door (automation) for 10 minutes after a hass restart?
I mean it works but i have high hopes to achieve better.

It could be that the retain flag has been set for the topic at some point and never been cleared. In that case, the broker would send a message whenever HA subscribed (such as when HA restarts).

You can check this by subscribing to the topic with a command line tool and seeing if a message is immediately printed. For instance

mosquitto_sub  -v -t 'your topic'
1 Like

I will try it tomorrow, this sounds very plausible and likely!
I know little no nothing about the MQTT protocol.
I did attempt installing a GUI client for it so I could browse its data and logs but didn’t get it working yesterday.

I will report back tomorrow on my finding.

Massive thanks for taking the time to help me.

I might be wrong but I thought I had this issue with my sonoff / mqtt and turned my retain to true. I believe this worked as this is how I have it now. My thinking is if shutting off HA doesn’t change any MQTT messages, when HA restart and requests the payload, it will get the same payload as when turned off.

I had an issue using the firmware from @KmanOz in that on poor or weak wifi connections or electrical noise on the line the sonoff would chatter off and on. the way I fixed it was to set retain to false.

I’m not saying that your problem is the same but there seems to be some possible stability improvement when the retain flag is set to false.

I forgot to mention, if it is the problem you can clear the retain flag by sending a message with a null payload, and the retain flag set, to that topic.

You can use the mqtt.publish service from the HA developer tools and set this as the payload
{"topic":"cmnd/sonoff_front/Dimmer", "retain":true}

obviously using your topic.

4 Likes

@gpbenton you legend!
Thank you so much for pointing me in the right direction.

  1. Each time I subscribed with this command
    mosquitto_sub -v -t ‘ceiling_fan/on/set’
    I got a “true” response.
  2. When I called
    {“topic”:“ceiling_fan/on/set”, “retain”: false}
    I got a NULL in the subscriber command line, however when I stop and subscribe as per #1 it gives me the true response straight away.
  3. I tried this command, it DID retain the value set each time I subscribed (Now off instead of on by default)
    {“topic”:“ceiling_fan/on/set”, “retain”: true, “payload”: “off”}
  4. After reboot I got the default as on again??
  5. I delete the mosquitto.db persistence!
  6. Its fixed :slight_smile:

I suspect it was toggling the in memory value but when it read from the db it kept getting the old value.
I cant say for sure but it probably had trouble writing to that db file but reading was ok.

2 Likes

Hi ettman8, please tell me how to delete the mosquitto.db. I’m using Hassio on Pi. Thanks.