Light turns on randomly

Hi there

I have a couple of sonoff devices that I’ve flashed with Tasmota. One of them (a flashed Sonoff S20 socket) is plugged in to a bedside lamp. A few times I’ve noticed, coming back in to the bedroom, that the light is switched on, but just assumed that perhaps I’d left it on, however my wife seemed to think that it had tuned itself on.

Fast forward to today; I’m at home, in the bedroom, and “tick” - the light comes on!

As luck would have it, I’d switched debug logging on for mqtt yesterday, but the logs don’t really give me any info:
2018-06-28 10:19:42 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on stat/sonoff-3/POWER: b'ON'

What I’d like to be able to do is log where the mqtt message is coming from, i.e. what sent the message, either an IP, or a device name, or something…

Whilst I’m at it, is it possible to log only a specific entity? it doesn’t look like it from the logs, and my log file is getting pretty big as I have a couple of cameras that send various status info back frequently with mqtt.

Thanks in advance!

Is the problem one in a weak wifi spot and is triggering when it reconnects?

Yeah mine turn on when they connect to WiFi too…

Interesting. There is strong WiFi there, but only if I’ve set it up to connect to the router in the extension rather than the main one.

Off to check, methinks…

Well, at the moment it’s connected to the extension network, but did have the main network setup as AP2. I’ve now deleted that, so the only option is the extension one, and we’ll see if that makes a difference.

They shouldn’t do that unless you have it configured to do so.

What configuration do you have in HomeAssistant for QOS and retain? What options do you have set in Tasmota for powerretain, and poweronstate? Go to the tasmota console and type those commands to see what is set.

It sounds like a combination of things might be happening, the device is rebooting and there is retained message set to ON out there still or you have the power on state set to ON and it reboots after losing a connection to wifi for so long.

Did you build the Tasmota firmware yourself or did you flash the recompiled binaries from their github page?

in addition to the MQTT retain option, setting “PowerRetain on” through the tasmota webpage console help my WiFi (re-)connect issue. You may want to check what state is your PowerRetain.
Hope that helps.

Thanks both. I’ve checked the Tasmota webpage for these settings:

14:40:01 CMD: powerretain
14:40:01 MQT: stat/sonoff-3/RESULT = {"PowerRetain":"OFF"}
14:40:11 CMD: poweronstate
14:40:11 MQT: stat/sonoff-3/RESULT = {"PowerOnState":3}

The sonoff was flashed using one of their binaries from the GH page.

This is the config in HA for this switch:

  - platform: mqtt
    name: Bedside
    command_topic: "cmnd/sonoff-3/power"
    state_topic: "stat/sonoff-3/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true

This is the same HA config as some cabinet lights elsewhere, which don’t seem to have the problem (but are only a couple of metres from the router).

This is why I asked about the wifi, if they were getting a weak signal, say 10-15% as tasmota measures it, a wrongly configured retain would cause this. Better to sort the wifi first though and then worry the other as it would just be hiding the real issue.

PowerRetain should be On in Tasmota and retain: false in Home Assistant. You’ll need to clear the retained topic cmnd/sonoff-3/power once you fix the configs.

I use PowerRetain 1, PowerOnState 3 and retain:true and everything keeps perfectly in sync through power cuts, restarts etc etc.

Do you have HA setup for remotely configuring/using it? I mean any kind of connection to the internet. Open ports etc.

Same here, powerretain 1 and retain true. Otherwise things would get out of sync during any restarts of HA. I played around with many of the combinations before I settled on this and it works like a charm. I also use the PowerOnState 3 so if the power blinks or the device happens to crash and reboot then the light is in the same state. The only downside to this is if you have one rebooting and the lamp is on, when the device cycles the relay drops for a second then kicks back in when the app boots as the relay isn’t a latching type.

Some other ideas to narrow this down is you can monitor the WiFi RSSI via MQTT by pulling it out of the tele message. Go view the console when you see the light change? Or even have the switch log to a syslog server to see what’s going on, if indeed the thing is rebooting, crashing or whatever.

It was only after rebooting the sonoff that I thought about the info that gets shown in the console section of the webpage. If/when it happens again I’ll take a look there, and see if there’s any corresponding reboot/wifi connection drop.

For now I’ve set powerretain ON, left retain:true in HA, and perhaps those (along with only giving it the option of the closest wifi router) will mean that I’ll never see the problem again!

Thanks everyone.

Things can get out of sync if you set retain true in Home Assistant. What will happen is that when wifi reconnects, it will execute the retained command. If the device is not in the same state as the retained command, you’ll get an unwanted trigger of the device. If you don’t set retain to true in Home Assistant, there will be no command retained to execute when wifi reconnects. The issue can be exacerbated if you press the button on the device when wifi is not connected.

Setting PowerRetain to On ensures that Home Assistant knows the device’s state on startup.

Yes, it is more of a pick your poison with a middle man on who takes priority of being correct as many different scenarios can happen.

OK, it happened again, and this time I checked the logs in the console. Basically it appears to have lost connection with hassio, or at least the mqtt broker:

23:56:43 MQT: stat/sonoff-3/RESULT = {"POWER":"ON"}
23:56:43 MQT: stat/sonoff-3/POWER = ON (retained)
23:59:35 MQT: stat/sonoff-3/RESULT = {"POWER":"OFF"}
23:59:35 MQT: stat/sonoff-3/POWER = OFF (retained)
00:01:40 MQT: tele/sonoff-3/STATE = {"Time":"2018-06-29T00:01:40","Uptime":"0T11:20:20","Vcc":3.461,"POWER":"OFF","Wifi":{"AP":1,"SSId":"Extension Network","RSSI":90,"APMac":"D4:6E:0E:35:F6:82"}}
00:02:00 MQT: tele/sonoff-3/UPTIME = {"Time":"2018-06-29T00:02:00","Uptime":"0T11:20:40"}
00:06:40 MQT: tele/sonoff-3/STATE = {"Time":"2018-06-29T00:06:40","Uptime":"0T11:25:20","Vcc":3.460,"POWER":"OFF","Wifi":{"AP":1,"SSId":"Extension Network","RSSI":86,"APMac":"D4:6E:0E:35:F6:82"}}
00:11:40 MQT: tele/sonoff-3/STATE = {"Time":"2018-06-29T00:11:40","Uptime":"0T11:30:20","Vcc":3.461,"POWER":"OFF","Wifi":{"AP":1,"SSId":"Extension Network","RSSI":92,"APMac":"D4:6E:0E:35:F6:82"}}
00:16:40 MQT: tele/sonoff-3/STATE = {"Time":"2018-06-29T00:16:40","Uptime":"0T11:35:20","Vcc":3.462,"POWER":"OFF","Wifi":{"AP":1,"SSId":"Extension Network","RSSI":92,"APMac":"D4:6E:0E:35:F6:82"}}
00:21:40 MQT: tele/sonoff-3/STATE = {"Time":"2018-06-29T00:21:40","Uptime":"0T11:40:20","Vcc":3.460,"POWER":"OFF","Wifi":{"AP":1,"SSId":"Extension Network","RSSI":96,"APMac":"D4:6E:0E:35:F6:82"}}
00:26:40 MQT: tele/sonoff-3/STATE = {"Time":"2018-06-29T00:26:40","Uptime":"0T11:45:20","Vcc":3.459,"POWER":"OFF","Wifi":{"AP":1,"SSId":"Extension Network","RSSI":86,"APMac":"D4:6E:0E:35:F6:82"}}
00:31:40 MQT: tele/sonoff-3/STATE = {"Time":"2018-06-29T00:31:40","Uptime":"0T11:50:20","Vcc":3.461,"POWER":"OFF","Wifi":{"AP":1,"SSId":"Extension Network","RSSI":88,"APMac":"D4:6E:0E:35:F6:82"}}
00:36:40 MQT: tele/sonoff-3/STATE = {"Time":"2018-06-29T00:36:40","Uptime":"0T11:55:20","Vcc":3.459,"POWER":"OFF","Wifi":{"AP":1,"SSId":"Extension Network","RSSI":90,"APMac":"D4:6E:0E:35:F6:82"}}
00:37:44 MQT: Attempting connection...
00:37:49 MQT: Connect failed to, rc -2. Retry in 10 sec
00:38:00 MQT: Attempting connection...
00:38:05 MQT: Connect failed to, rc -2. Retry in 10 sec
00:38:16 MQT: Attempting connection...
00:38:21 MQT: Connect failed to, rc -2. Retry in 10 sec
00:38:32 MQT: Attempting connection...
00:38:32 MQT: Connected
00:38:32 MQT: tele/sonoff-3/LWT = Online (retained)
00:38:32 MQT: cmnd/sonoff-3/POWER = 
00:38:32 MQT: stat/sonoff-3/RESULT = {"POWER":"ON"}
00:38:32 MQT: stat/sonoff-3/POWER = ON (retained)
00:38:40 MQT: stat/sonoff-3/RESULT = {"POWER":"OFF"}
00:38:40 MQT: stat/sonoff-3/POWER = OFF (retained)

You can see me turning off the light around midnight, and it showing POWER = OFF (retained) , but then when it recovered it seems to have ignored this and switched the damn thing back on.

Any ideas what’s going on here?

I just checked my settings and those are what mine are set to as well.

It still will switch on when it connects to WiFi for me but otherwise work correctly

Give the retain: false in the YAML a try as it sounds like things are getting out of sync and the retain power message is staying MQTT as on for some reason.