Help with MQTT, sonoff with tasmota and PowerOnState command

Hey guys,

I just got into figuring out how mqtt works so, please bare with me if i am missing something stupid :slight_smile:

I need some help on how to set up my mqtt staircase lights.
The tl;dr version is that although PowerOnState is set to 1 in the tasmota firmware, unfortunately, when switching the lights on through the wall switch, they just stay on for 4-5 seconds then go off again.

Here is my set up:

  • Two lights, one in each side of my staircase, both wired to wall switches.
  • I installed a small sonoff basic flashed with tasmota box inside each one.
  • This means the sonoff only boots when the wall switch turns on.
  • Each sonoff has a PIR sensor connected and so far everything works well.
  • Both pirs are set up as an mqtt binary sensor in my HA which turns on the lights, then off after a delay of 120sec.

I have provided my detailed configuration below, and it all works fine, when both the switches are on and the sonoffs are active, the motion sensors work great and activate them.

My desired functionality would be for the sonoffs to power up and stay on when booting up (when i turn my wall switch off and on again) - or ideally, also turn off after a time delay.

What happens now is that the lights do turn on but turn off around 4-5 sec.

00:00:00 Project sonoff Sonoff Stairs Dn (Topic sonoff_stairs, Fallback sonoff_stairs_dn, GroupTopic sonoffs) Version 5.10.0
00:00:00 WIF: Connecting to AP1 XXXXX in mode 11N as sonoff_stairs-1627...
00:00:02 RSL: stat/sonoff_stairs/RESULT = {"POWER":"OFF"}
00:00:02 RSL: stat/sonoff_stairs/POWER = OFF
00:00:03 WIF: Connected
00:00:03 DNS: Initialized

I am getting this RSL power off as soon as it boots up, Is that the problem?

Alternatively and ideally, if the sonoff could send a motion detection mqtt command when it boots up, that would be great as it would also respect the two minute timer and turn off again. Is there a way to send an mqtt command on boot? I understand that birth message is something like that,but that is issued broker-side right? is there something similar on client side?


I’m going to provide my full code, probably going to help someone:
configuration.yaml:

mqtt:

(using the built in broker)

switches.yaml:

  - platform: mqtt
    name: "sonoff stairs"
    state_topic: "stat/sonoff_stairs/POWER"
    command_topic: "cmnd/sonoff_stairs/power"
    payload_on: "ON"
    payload_off: "OFF"

binary_sensors.yaml:

  - platform: mqtt
    name: "sonoff motion stairs"
    state_topic: "cmnd/motion/stairs/+/POWER1"
    payload_on: "ON"
    payload_off: "OFF"
    qos: 0
    device_class: motion

Both my sonoffs are listening to cmnd/sonoff_stairs/power
Both switchmode1 1
And both sensors broadcast cmnd/motion/stairs/up/POWER1 and cmnd/motion/stairs/dn/POWER1 by switchtopic motion/stairs/up and switchtopic motion/stairs/dn in the console as this guy.
I have also set PowerOnState 1 and PowerRetain 0 on both as per the tasmota guide

my automations.yaml:

### STAIRS PIR
- action:
  - alias: Turn On stairs lights
    service: switch.turn_on
    entity_id: switch.sonoff_stairs
  alias: Motion stairs ON
  id: '1506607652748'
  trigger:
  - entity_id: binary_sensor.sonoff_motion_stairs
    from: 'off'
    platform: state
    to: 'on'
    
- action:
  - alias: Turn off stairs lights
    service: switch.turn_off
    entity_id: switch.sonoff_stairs
  alias: Motion stairs OFF 
  condition: []
  trigger:
  - entity_id: binary_sensor.sonoff_motion_stairs
    platform: state
    to: 'off'
    for:
      seconds: 120

The first suspect is that something is sending an MQTT message to turn it off at this point. Can you subscribe to the topics with something to print a flow of MQTT messages to see if that is happening?

Will try it and report the log back. Thanks

So if i understand correctly, i have launched mqttboxon windows, and connected to the broker.
Subscribed to stat/#, tele/#and cmnd/# , here is what i get on connection:

on tele/#:

Online
qos : 0, retain : true, cmd : publish, dup : false, topic : tele/sonoff_stairs/LWT, messageId : , length : 30, Raw payload : 79110108105110101

and when i turn one wall switch off and back on again:
on tele/#:

{"Time":"2018-01-16T12:47:18", "Switch1":"OFF"}
qos : 0, retain : false, cmd : publish, dup : false, topic : tele/sonoff_stairs/SENSOR, messageId : , length : 74, Raw payload : 1233484105109101345834504849564548494549548449505852555849563444323483119105116991044934583479707034125
{"Time":"2018-01-16T12:47:18", "Uptime":0, "Vcc":3.198, "POWER":"OFF", "Wifi":{"AP":1, "SSId":"XXXXX", "RSSI":62, "APMac":"XXXXXX"}}
qos : 0, retain : false, cmd : publish, dup : false, topic : tele/sonoff_stairs/STATE, messageId : , length : 169, Raw payload : 12334841051091013458345048495645484945495484495058525558495634443234851121161051091013458484432348699993458514649575644323480798769823458347970703444323487105102105345812334658034584944323483837310034583480971081011113444323482838373345854504432346580779799345834575658686958684858565558545458686934125125
Offline
qos : 1, retain : false, cmd : publish, dup : false, topic : tele/sonoff_stairs/LWT, messageId : 1, length : 33, Raw payload : 79102102108105110101
{"RestartReason":"Power on"}
qos : 0, retain : false, cmd : publish, dup : false, topic : tele/sonoff_stairs/INFO3, messageId : , length : 54, Raw payload : 1233482101115116971141168210197115111110345834801111191011143211111034125
{"WebServerMode":"Admin", "Hostname":"sonoff_stairs-4940", "IPAddress":"sonoffIPaddress"}
qos : 0, retain : false, cmd : publish, dup : false, topic : tele/sonoff_stairs/INFO2, messageId : , length : 110, Raw payload : 12334871019883101114118101114771111001013458346510010910511034443234721111151161109710910134583411511111011110210295115116971051141154552575248344432347380651001001141011151153458344948464846484650505134125
{"Module":"Sonoff Basic", "Version":"5.9.1", "FallbackTopic":"sonoff_stairs_up", "GroupTopic":"sonoffs"}
qos : 0, retain : false, cmd : publish, dup : false, topic : tele/sonoff_stairs/INFO1, messageId : , length : 130, Raw payload : 123347711110011710810134583483111110111102102326697115105993444323486101114115105111110345834534657464934443234709710810898979910784111112105993458341151111101111021029511511697105114115951171123444323471114111117112841111121059934583411511111011110210211534125
Online
qos : 0, retain : false, cmd : publish, dup : false, topic : tele/sonoff_stairs/LWT, messageId : , length : 30, Raw payload : 79110108105110101

and on cmnd/#

OFF
qos : 0, retain : false, cmd : publish, dup : false, topic : cmnd/motion/stairs/up/POWER1, messageId : , length : 33, Raw payload : 797070
ON
qos : 0, retain : false, cmd : publish, dup : false, topic : cmnd/motion/stairs/up/POWER1, messageId : , length : 32, Raw payload : 7978
qos : 0, retain : false, cmd : publish, dup : false, topic : cmnd/sonoff_stairs/POWER, messageId : , length : 26, Raw payload :
ON
qos : 0, retain : false, cmd : publish, dup : false, topic : cmnd/motion/stairs/up/POWER1, messageId : , length : 32, Raw payload : 7978

They are in backwards chronological order btw

(edit: masked ssid and ip addresses )

Any insights?

Can you just do this, rather than switching the lights on and off?

If you subscribe to the topic # you will get all the messages in one trace, which will make it easier to correlate.

Subscription to #:

1. OFF
2. qos : 0, retain : false, cmd : publish, dup : false, topic : cmnd/motion/stairs/up/POWER1, messageId : , length : 33, Raw payload : 797070
3. ON
4. qos : 0, retain : false, cmd : publish, dup : false, topic : cmnd/motion/stairs/up/POWER1, messageId : , length : 32, Raw payload : 7978
5. {"Time":"2018-01-16T13:21:15", "Switch1":"OFF"}
6. qos : 0, retain : false, cmd : publish, dup : false, topic : tele/sonoff_stairs/SENSOR, messageId : , length : 74, Raw payload : 1233484105109101345834504849564548494549548449515850495849533444323483119105116991044934583479707034125
7. 
8. ## Switch1 is the motion detection, as i went near to flip the switch, so from this point upwards should be irrelevant (i think)
9. 
10. {"Time":"2018-01-16T13:21:15", "Uptime":0, "Vcc":3.196, "POWER":"OFF", "Wifi":{"AP":1, "SSId":"XXXXX", "RSSI":56, "APMac":"XXXXXXXXX"}}
11. qos : 0, retain : false, cmd : publish, dup : false, topic : tele/sonoff_stairs/STATE, messageId : , length : 169, Raw payload : 12334841051091013458345048495645484945495484495158504958495334443234851121161051091013458484432348699993458514649575444323480798769823458347970703444323487105102105345812334658034584944323483837310034583480971081011113444323482838373345853544432346580779799345834575658686958684858565558545458686934125125
12. Offline
13. qos : 1, retain : false, cmd : publish, dup : false, topic : tele/sonoff_stairs/LWT, messageId : 1, length : 33, Raw payload : 79102102108105110101
14. OFF
15. qos : 0, retain : false, cmd : publish, dup : false, topic : stat/sonoff_stairs/POWER, messageId : , length : 29, Raw payload : 797070
16. {"POWER":"OFF"}
17. qos : 0, retain : false, cmd : publish, dup : false, topic : stat/sonoff_stairs/RESULT, messageId : , length : 42, Raw payload : 12334807987698234583479707034125
18. {"RestartReason":"Power on"}
19. qos : 0, retain : false, cmd : publish, dup : false, topic : tele/sonoff_stairs/INFO3, messageId : , length : 54, Raw payload : 1233482101115116971141168210197115111110345834801111191011143211111034125
20. {"WebServerMode":"Admin", "Hostname":"sonoff_stairs-4940", "IPAddress":"XXXXXXX"}
21. qos : 0, retain : false, cmd : publish, dup : false, topic : tele/sonoff_stairs/INFO2, messageId : , length : 110, Raw payload : 12334871019883101114118101114771111001013458346510010910511034443234721111151161109710910134583411511111011110210295115116971051141154552575248344432347380651001001141011151153458344948464846484650505134125
22. {"Module":"Sonoff Basic", "Version":"5.9.1", "FallbackTopic":"sonoff_stairs_up", "GroupTopic":"sonoffs"}
23. qos : 0, retain : false, cmd : publish, dup : false, topic : tele/sonoff_stairs/INFO1, messageId : , length : 130, Raw payload : 123347711110011710810134583483111110111102102326697115105993444323486101114115105111110345834534657464934443234709710810898979910784111112105993458341151111101111021029511511697105114115951171123444323471114111117112841111121059934583411511111011110210211534125
24. qos : 0, retain : false, cmd : publish, dup : false, topic : cmnd/sonoff_stairs/POWER, messageId : , length : 26, Raw payload :
25. Online
26. qos : 0, retain : false, cmd : publish, dup : false, topic : tele/sonoff_stairs/LWT, messageId : , length : 30, Raw payload : 79110108105110101
27. ON
28. qos : 0, retain : false, cmd : publish, dup : false, topic : cmnd/motion/stairs/up/POWER1, messageId : , length : 32, Raw payload : 7978
29. 
30. ## Wall switch off and on here
31. 
32. Online
33. qos : 0, retain : true, cmd : publish, dup : false, topic : tele/sonoff1/LWT, messageId : , length : 24, Raw payload : 79110108105110101
34. Offline
35. qos : 0, retain : true, cmd : publish, dup : false, topic : tele/sonoff_stairs/LWT, messageId : , length : 31, Raw payload : 79102102108105110101
36. HBMQTT version 0.9.1
37. qos : 0, retain : true, cmd : publish, dup : false, topic : $SYS/broker/version, messageId : , length : 41, Raw payload : 72667781848432118101114115105111110324846574649

edit: added line numbering so we could work better :slight_smile:

The trace is missing the end of the line

qos : 0, retain : false, cmd : publish, dup : false, topic : cmnd/sonoff_stairs/POWER, messageId : , length : 26, Raw payload :

which may be the critical piece - I can’t see any other command, and if this off we know where to start looking.

You are referring to line 23 right?
I just replicated it, it appears again on switch off/on
How to i further investigate that?

BTW, what about line 15, i think that’s what’s turning it off

(added line numbering, keep in mind they are backwards)

stat/sonoff_stairs/POWER
I think that is the status message back from the sonoff.

You’re absolutely right, my mistake.
So, what could be causing it?

The first thing to find out is the missing contents of line 24. The length indicates that it could be 3 letters (4 less than online), but it would be nice to be certain.

Are you sure it wan’t a copy/paste error? Windows is quite bad a missing bits out of long pastes.

Afraid not, just checked again by re-doing the whole process:
image

I have just realized it is stranger than I thought. The mysterious message is cmnd/sonoff_stairs/POWER whereas your configuration has:

command_topic: "cmnd/sonoff_stairs/power"

without the capitals. If you are sure the sonoffs are listening for a lower case POWER it won’t have any effect. But I don’t know where its coming from.

Nice catch…
Hopefully someone with more tasmota knowledge could shed some light.

It must be from the sonoff itself, because this is what i get in its console when booting up Line #9:

00:00:00 Project sonoff Sonoff Stairs Up (Topic sonoff_stairs, Fallback sonoff_stairs_up, GroupTopic sonoffs) Version 5.9.1
00:00:00 WIF: Connecting to AP1 XXXXX in mode 11N as sonoff_stairs-4940...
00:00:03 WIF: Connected
00:00:03 DNS: Initialized
00:00:03 HTP: Web server active on sonoff_stairs-4940.local with IP address XXXXXX
15:56:22 MQT: Attempting connection...
15:56:24 MQT: Connected
15:56:24 MQT: tele/sonoff_stairs/LWT = Online (retained)
15:56:24 MQT: cmnd/sonoff_stairs/POWER =
15:56:24 MQT: tele/sonoff_stairs/INFO1 = {"Module":"Sonoff Basic", "Version":"5.9.1", "FallbackTopic":"sonoff_stairs_up", "GroupTopic":"sonoffs"}
15:56:24 MQT: tele/sonoff_stairs/INFO2 = {"WebServerMode":"Admin", "Hostname":"sonoff_stairs-4940", "IPAddress":"XXXXXX"}
15:56:24 MQT: tele/sonoff_stairs/INFO3 = {"RestartReason":"Software/System restart"}
15:56:26 MQT: stat/sonoff_stairs/RESULT = {"POWER":"OFF"}
15:56:26 MQT: stat/sonoff_stairs/POWER = OFF


It says here that the mqtt retain will always overwrite the PowerOnState.

Yes but,

So… if i understand corectly from the documentation you linked, i should be ok.

No, because if you turn the switch off in HASS that message will retain on the broker. The switch reads that message when it turns on again and thinks it has to turn off. That’s why there’s a delay of 5-10 sec.

Just turned the light ON through home assistant, and went and flipped the switch off and on (so the HA message should be ON right?)

Now i am getting no light at all on boot up, stays off completely.

Here is the console output:

00:00:00 Project sonoff Sonoff Stairs Up (Topic sonoff_stairs, Fallback sonoff_stairs_up, GroupTopic sonoffs) Version 5.9.1
00:00:00 WIF: Connecting to AP1 XXXXX in mode 11N as sonoff_stairs-4940...
00:00:02 RSL: stat/sonoff_stairs/RESULT = {"POWER":"OFF"}
00:00:02 RSL: stat/sonoff_stairs/POWER = OFF
00:00:03 WIF: Connected
00:00:03 DNS: Initialized
00:00:03 HTP: Web server active on sonoff_stairs-4940.local with IP address xxxxx
00:00:04 MQT: Attempting connection...
16:08:29 MQT: Connected
16:08:29 MQT: tele/sonoff_stairs/LWT = Online (retained)
16:08:29 MQT: cmnd/sonoff_stairs/POWER = 
16:08:29 MQT: tele/sonoff_stairs/INFO1 = {"Module":"Sonoff Basic", "Version":"5.9.1", "FallbackTopic":"sonoff_stairs_up", "GroupTopic":"sonoffs"}
16:08:29 MQT: tele/sonoff_stairs/INFO2 = {"WebServerMode":"Admin", "Hostname":"sonoff_stairs-4940", "IPAddress":"xxxxxx"}
16:08:29 MQT: tele/sonoff_stairs/INFO3 = {"RestartReason":"Power on"}
16:08:30 MQT: stat/sonoff_stairs/RESULT = {"POWER":"OFF"}
16:08:30 MQT: stat/sonoff_stairs/POWER = OFF

edit: misstyped off, changed to ON

Log in to the admin panel on the switch. Go to console and type: PowerOnState.
It should list the state. You can also set the state by typing PowerOnState 1.

But again, if the switch is switched to off in HASS it will switch the switch to off, when the lights turn back on with the wall switch.

Bottom line is that the sonoff device reads the last retained state from the MQTT server, and that’s what it causing the device to turn off.

I did some testing and seems if you emulate the HUE and disable MQTT on the Sonoff, then the device won’t read the last state from hass.

They’re already set on PowerOnState 1.
Also:

(misstyped on my previous post), i set the lights on from HA before flipping the switch. So if there is a retained message it should be ON, unless HA issues off as soon as the sonoff turns off and loses connection?