Mosquitto on HA, automatically power off the light after a few seconds

Hello community, I’m using Tasmota on Sonoff Basic version 5.12.0 integrated in HA. Everything works perfect, but there is one issue who are driving me crazy. If I used the wall switch to power off the Sonoff, then Power On again, the light will switch on correctly, but after a few seconds it switch off automatically.
On Sonoff I have:
PowerRetain On
PowerOnState 1 (Turn relay(s) on after power on)

But wait, if I stop the mosquitto service on HA, I don’t have this behaviour. After wall switch on, the light stays on. And doesn’t matter if the sonoff is configured on configuration.yaml on HA. Btw, this is the HA config (even I tried to remove it and restart HA, but the issue still persist, as I said the only way is stop mosquitto service)

  - platform: mqtt
    name: "Cucina"
    state_topic: "stat/cucina/POWER"
    command_topic: "cmnd/cucina/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true 
    availability_topic: "tele/cucina/LWT"
    payload_available: "Online"
    payload_not_available: "Offline"

Could someone point me to a solution? Thank you.

DAMN! My bad. It was the mqtt config on confgiuration.yaml! I’m sorry to wasted forum space. BTW, I had:

mqtt:
  broker: localhost
  port: 1883  
  client_id: HA     
  keepalive: 60
  username: mqttuser
  password: ******
  protocol: 3.1
  birth_message:
    topic: "tele/hass1/LWT"
    payload: "Online"
    qos: 1
    retain: true
  will_message:
    topic: "tele/hass1/LWT"
    payload: "Offline"
    qos: 1
    retain: true

And just configure as follow solve te issue:

mqtt:
  broker: localhost  
  port: 1883  
  username: mqttuser  
  password: ********  

Now I have only to understand what’s wrong on previous configuration, and what’s the config row who switch off the light after power on. I’ll try to add one by one.

If you publish the command as retained message the sonoff will get that immediately when powering up and act accordingly. In your case it gets the off command.

I would change the state message as retained and command not.

Or then you need to figure out another way of switching instead of the wall switch.

Did you mean on single light config? I have the following config for each sonoff/light, could you please help me to apply your advice?

  - platform: mqtt
    name: "Ingresso 1"
    state_topic: "stat/ingresso1/POWER"
    command_topic: "cmnd/ingresso1/POWER"
    qos: 1                    
    payload_on: "ON"
    payload_off: "OFF"
    retain: true
    availability_topic: "tele/ingresso1/LWT"
    payload_available: "Online"
    payload_not_available: "Offline"

Not a waste of space because I have this problem too :-). But didn’t put much time to solve it. Please, may you explain what you did? How it was, and how it is now?

Having retain using Mqtt meens that the server will remember the message. You should enable PowerRetain on the sonoff, that will only remember the state, so if you restart HA it will still have the correct state.
But you shouldn’t have retain in HA since it then will remember the command, and when sonoff boots it will turn on as you wish but when it connects to the server it will get the retained off message and turn of after a couple of seconds.

Thank you for explanation, I just remove all the "retain: true" commands in every switch configuration and seem it works. Except for only one Sonoff/light who still switch off after power on! DAMN!
This is the config (absolutely the same for other sonoff), I’m gettin crazy to understand what’s wrong :thinking:

- platform: mqtt
  name: "Ingresso 2"
  state_topic: "stat/ingresso2/POWER"
  command_topic: "cmnd/ingresso2/POWER"
  qos: 1
  payload_on: "ON"
  payload_off: "OFF"
  availability_topic: "tele/ingresso2/LWT"
  payload_available: "Online"
  payload_not_available: "Offline"

On sonoff console:

MQT: stat/ingresso2/STATUS = {“Status”:{“Module”:1,“FriendlyName”:“ingresso2”,“Topic”:“ingresso2”,“ButtonTopic”:“0”,“Power”:0,“PowerOnState”:1,“LedState”:1,“SaveData”:1,“SaveState”:1,“ButtonRetain”:0,“PowerRetain”:1}}

Then, if I restart mosquitto service on HA, this is the only lamp who will be powered off (in case it’s on)

You also need to remove the retained messages from the broker’s database, otherwise it will continue sending the last message sent with the retain flag set. You need to send a message to each topic with the retain flag set and a null payload such as

$ mosquito_pub -t topic -r -n

Thank you, but still have the issue, I also tried to stop HA and mosquitto service, then delete the HA DB (home-assistant_v2.db), start again, but everytime the sonoff connect to HA, it will be switched off! DAMN!!!
Thia is the command I used on HA:

mosquitto_pub -d -h localhost -u mqttuser -P 12345 -t ingresso2 -r -n
Client mosqpub/5749-raspberryp sending CONNECT
Client mosqpub/5749-raspberryp received CONNACK
Client mosqpub/5749-raspberryp sending PUBLISH (d0, q0, r1, m1, 'ingresso2', ... (0 bytes))
Client mosqpub/5749-raspberryp sending DISCONNECT

You need to specify all of the command topic.

Sorry, could you please specify with an example what you wrote? I don’t understand it at all :slight_smile:

You probably haven’t turned that specific sonoff on and off via HA after you changed the config?

It isn’t enough to just change the config, you to send a new message to the mqtt server by example you can turn the sonoff off and on in HA that will change the topic to not retaining the message. If you don’t you could still have an old message retained in the mqtt server.

Otherwise you could use the command you tried above but the topic has to be cmnd/ingresso2/POWER

Thank you to point me to a solution :+1: Here the mosquitto commands I used, maybe can be useful to others. Since the stored MQTT message will always override the PowerOnState, with the following command we can check all the stored mqtt messages:

mosquitto_sub -d -h localhost -u [username] -P [password] -t cmnd/+/POWER

In my case this is the result (I also discover some old sonoff I don’t use anymore, coming most probably from my past tests :smiley:):

Client mosqsub/9361-raspberryp received PUBLISH (d0, q0, r1, m0, 'cmnd/SONOFF10/POWER', ... (2 bytes)) ON
Client mosqsub/9361-raspberryp received PUBLISH (d0, q0, r1, m0, 'cmnd/sonoff-workbench/POWER', ... (2 bytes)) ON
Client mosqsub/9361-raspberryp received PUBLISH (d0, q0, r1, m0, 'cmnd/ingresso2/POWER', ... (3 bytes)) OFF
Client mosqsub/9361-raspberryp received PUBLISH (d0, q0, r1, m0, 'cmnd/Sonoff_POW/POWER', ... (3 bytes)) OFF

Well, now, to delete stored message/s, we can execute:

mosquitto_pub -p 1883 -u [username] -P [password] -d -n -r -t cmnd/ingresso2/POWER

Lucas

2 Likes

Yup. I did that but address not available. How to fix?

This looks like hassio weirdness. You are probably better off installing mosquitto_pub on your windows machine and running the command from there. Just remember to add the -h parameter to specify the host where mosquitto is running.

Try to add:

-h 127.0.0.1

or

-h [ipraspberry]

Or you can also use mqtt-spy => http://kamilfb.github.io/mqtt-spy/ from remote machine
It’s java based, so multi platform.

Now i have it all i did was update 0.85.1
Mqtt 4

My problem was Tasmota 6.4.1 it kept disconnecting and as soon as it connects it sends the retain message again and it switched off. went back to tasmota 6.2.1 and is working now.
here was my old sonoff console info:

19:08:36 MQT: Attempting connection…
19:08:36 MQT: Connected
19:08:36 MQT: tele/sonoff02/LWT = Online (retained)
19:08:36 MQT: cmnd/sonoff02/POWER =
19:08:37 MQT: stat/sonoff02/RESULT = {“POWER”:“ON”}
19:08:37 MQT: stat/sonoff02/POWER = ON
19:08:56 WIF: Connecting to AP1 Unifi in mode 11N as sonoff02-3593…
19:09:00 WIF: Connected
19:09:00 DNS: Initialized
19:09:01 MQT: Attempting connection…
19:09:01 MQT: Connected
19:09:01 MQT: tele/sonoff02/LWT = Online (retained)
19:09:01 MQT: cmnd/sonoff02/POWER =
19:09:01 MQT: stat/sonoff02/RESULT = {“POWER”:“ON”}
19:09:01 MQT: stat/sonoff02/POWER = ON

now its:
00:00:00 Project sonoff Bedroom light/TEMP (Topic sonoff02, Fallback DVES_AF6E09, GroupTopic sonoffs) Version 6.2.1-2_3_0
00:00:00 WIF: Connecting to AP1 Unifi in mode 11N as sonoff02-3593…
00:00:03 WIF: Connected
00:00:03 DNS: Initialized
00:00:03 HTP: Web server active on sonoff02-3593.local with IP address 192.168.2.14
00:00:04 MQT: Attempting connection…
00:00:05 MQT: Connected
00:00:05 MQT: tele/sonoff02/LWT = Online (retained)
00:00:05 MQT: cmnd/sonoff02/POWER =
00:00:05 MQT: tele/sonoff02/INFO1 = {“Module”:“Sonoff Basic”,“Version”:“6.2.1”,“FallbackTopic”:“DVES_AF6E09”,“GroupTopic”:“sonoffs”}
00:00:05 MQT: tele/sonoff02/INFO2 = {“WebServerMode”:“Admin”,“Hostname”:“sonoff02-3593”,“IPAddress”:“192.168.2.14”}
00:00:05 MQT: tele/sonoff02/INFO3 = {“RestartReason”:“Software/System restart”}
00:00:05 MQT: stat/sonoff02/RESULT = {“POWER”:“ON”}
00:00:05 MQT: stat/sonoff02/POWER = ON
00:00:05 MQT: stat/sonoff02/RESULT = {“POWER”:“ON”}
00:00:05 MQT: stat/sonoff02/POWER = ON
19:11:51 MQT: tele/sonoff02/STATE = {“Time”:“2019-01-21T19:11:51”,“Uptime”:“0T00:00:13”,“Vcc”:3.197,“POWER”:“ON”,“Wifi”:{“AP”:1,“SSId”:“Unifi”,“RSSI”:68,“APMac”:“04:18:D6:0B:0E:B3”}}
19:11:51 MQT: tele/sonoff02/SENSOR = {“Time”:“2019-01-21T19:11:51”,“DHT11”:{“Temperature”:25.5,“Humidity”:59.0},“TempUnit”:“C”}
19:16:41 MQT: stat/sonoff02/RESULT = {“POWER”:“OFF”}
19:16:41 MQT: stat/sonoff02/POWER = OFF

All good now

Tasmota 6.4.0 and 6.3.x also does not work for me