PowerOnState (e.g., =1) relay settings at boot: MQTT state message to publish state?

1,624 views
Skip to first unread message

Nelis Willers

unread,
Apr 15, 2017, 4:51:05 PM4/15/17
to SonoffUsers
I am new to Home Assistant, Sonoff and Tasmota, this noob question might have an easy answer - but I can't find it.

With PowerOnState = 1 the Sonoff switch turns on at boot and stays on indefinitely. This is the expected and correct operation.

However, when used with Home Assistant, it seems that HASS is not aware that the switch is supposed to be on after boot, and forces it to OFF state a few seconds after the Sonoff boots.  Does HASS enforce its own idea of the light status at regular intervals? It seems to to be the case.

Would it be possible to send out an MQTT state message publishing the ON status so that HASS knows the required status (the Tasmota PowerOnState)? There might not always be a MQTT server around, so you should probably not publish a power on command cmnd/sonoff/power on.

The code currently appears to just switch on the relay, but with no MQTT message informing the rest of the world:

    else if (sysCfg.poweronstate == 1) {  // All on
      power = ((0x00FF << Maxdevice) >> 8);
      // will a MQTT state message here help to inform HASS?
      setRelay(power);


The use case is as follows:
I want to locate the Sonoff inline between the usual wall switch and the light. The idea is that the light can be switched on by flipping the wall switch, or by MQTT command via HASS if the wall switch is already on.
You may not always have a phone around to switch on the light, so flipping the wall switch with PowerOnState = 1 should work quite nicely: the Sonoff/Tasmota is 'transparant' by just switching on the light.
But this does not currently work, because HASS is not informed of your intentions when flipping the wall switch with PowerOnState = 1.

I am about to try and change the code but feel somewhat uncertain about the undertaking. A few pointers would be most welcome.

David Lang

unread,
Apr 15, 2017, 5:15:22 PM4/15/17
to Nelis Willers, SonoffUsers
On Sat, 15 Apr 2017, Nelis Willers wrote:

> I am new to Home Assistant, Sonoff and Tasmota, this noob question might
> have an easy answer - but I can't find it.
>
> With PowerOnState = 1 the Sonoff switch turns on at boot and stays on
> indefinitely. This is the expected and correct operation.
>
> However, when used with Home Assistant, it seems that HASS is not aware
> that the switch is supposed to be on after boot, and forces it to OFF state
> a few seconds after the Sonoff boots. Does HASS enforce its own idea of
> the light status at regular intervals? It seems to to be the case.

that sounds like typical behavior of home automation software.

> Would it be possible to send out an MQTT state message publishing the ON
> status so that HASS knows the required status (the Tasmota PowerOnState)?

the tele/ messages publish this information

> There might not always be a MQTT server around, so you should probably not
> publish a power on command cmnd/sonoff/power on.
>
> The code currently appears to just switch on the relay, but with no MQTT
> message informing the rest of the world:
>
> else if (sysCfg.poweronstate == 1) { // All on
> power = ((0x00FF << Maxdevice) >> 8);
> // *will a MQTT state message here help to inform HASS?*
> setRelay(power);
>
> The use case is as follows:
> I want to locate the Sonoff inline between the usual wall switch and the
> light. The idea is that the light can be switched on by flipping the wall
> switch, or by MQTT command via HASS if the wall switch is already on.
> You may not always have a phone around to switch on the light, so flipping
> the wall switch with PowerOnState = 1 should work quite nicely: the
> Sonoff/Tasmota is 'transparant' by just switching on the light.
> But this does not currently work, because HASS is not informed of your
> intentions when flipping the wall switch with PowerOnState = 1.
>
> I am about to try and change the code but feel somewhat uncertain about the
> undertaking. A few pointers would be most welcome.

the changes should just be in your home assistant config, have it watch for the
tele/ messages as well as the stat/ messages

when you hit the button manually, the device sends a stat/ message informing the
mqtt server that it has switched

ever loginterval seconds, the device sends tele/ messages informing the mqtt
server all about it's current status

David Lang

Nelis Willers

unread,
Apr 15, 2017, 6:16:54 PM4/15/17
to SonoffUsers, nelisw...@gmail.com, da...@lang.hm
Hi David, thanks for reply


On Saturday, 15 April 2017 23:15:22 UTC+2, David Lang wrote:
On Sat, 15 Apr 2017, Nelis Willers wrote:

>the tele/ messages publish this information
   >the changes should just be in your home assistant config, have it watch for the
>tele/ messages as well as the stat/ messages

>when you hit the button manually, the device sends a stat/ message informing the
>mqtt server that it has switched

>ever loginterval seconds, the device sends tele/ messages informing the mqtt
>server all about it's current status

The tele/ messages are published every 300 to 400 seconds, which is a bit slow for near real time? tele messages are fine for logging, but not for quick response:
23:05:12 MQTT: tele/sonoff3935/STATE = {"Time":"2017-04-15T23:05:11", "Uptime":1, "Vcc":3.139, "POWER":"OFF", "Wifi":{"AP":1, "SSID":"CJMSW", "RSSI":100}}
23:10:52 MQTT: tele/sonoff3935/STATE = {"Time":"2017-04-15T23:10:52", "Uptime":1, "Vcc":3.138, "POWER":"OFF", "Wifi":{"AP":1, "SSID":"CJMSW", "RSSI":100}}
23:16:36 MQTT: tele/sonoff3935/STATE = {"Time":"2017-04-15T23:16:36", "Uptime":1, "Vcc":3.137, "POWER":"OFF", "Wifi":{"AP":1, "SSID":"CJMSW", "RSSI":100}}

My limited understanding, looking at the MQTT message stream, is that the cmnd messages issue the command and the stat messages then respond with a confirmation of the executed command.
In this context, should one then not rather publish and look out for stat messages, rather than tele messages?
 

David Lang

unread,
Apr 15, 2017, 6:32:31 PM4/15/17
to Nelis Willers, SonoffUsers
On Sat, 15 Apr 2017, Nelis Willers wrote:

>
> The tele/ messages are published every 300 to 400 seconds, which is a bit
> slow for near real time? tele messages are fine for logging, but not for
> quick response:

Yes, the default is every 300 seconds for tele/ messages (this is configurable).
I have my devices set to 60 seconds.

> 23:05:12 MQTT: tele/sonoff3935/STATE = {"Time":"2017-04-15T23:05:11",
> "Uptime":1, "Vcc":3.139, "POWER":"OFF", "Wifi":{"AP":1, "SSID":"CJMSW",
> "RSSI":100}}
> 23:10:52 MQTT: tele/sonoff3935/STATE = {"Time":"2017-04-15T23:10:52",
> "Uptime":1, "Vcc":3.138, "POWER":"OFF", "Wifi":{"AP":1, "SSID":"CJMSW",
> "RSSI":100}}
> 23:16:36 MQTT: tele/sonoff3935/STATE = {"Time":"2017-04-15T23:16:36",
> "Uptime":1, "Vcc":3.137, "POWER":"OFF", "Wifi":{"AP":1, "SSID":"CJMSW",
> "RSSI":100}}
>
> My limited understanding, looking at the MQTT message stream, is that the
> cmnd messages issue the command and the stat messages then respond with a
> confirmation of the executed command.
> In this context, should one then not rather publish and look out for stat
> messages, rather than tele messages?

You were asking for a message from the device to your home automation software
to let you know that it's on after a 'power outage'

That's different than looking for a response to a command.

You are correct that if you are looking for feedback to know that a command has
been executed, you should look for the stat/ messages

But when you want to make sure that the home automation system has the correct
status of a device after a power outage (or restart of the home automation
system), then you should also look for the tele/ messages as well as the stat/
messages.

It could be argued either way as to what should happen after a power outage. But
with the principal that the home automation software is the ultimate authority,
you normally don't want to send a stat/ message (since that indicates that the
user changed the setting of the light), and so you have to wait for the tele/
message.

In your use case, you are hacking the process to use the power failure response
mode as a control action, so in your case a stat/ message would be appropriate,
but you should be able to get by with the tele/ message

On my system (using openhab), I was experimenting and restarting openhab
frequently and was annoyed that it didn't show me the correct status for the
lights until after they had been switched, so I have openhab configured to watch
for either the stat/ or the tele/ message to update it's internal status on the
light, and set the loginterval to 60 seconds so that within a minute of powering
a device on or restarting openhab, it has the correct status for all the
devices.

David Lang

Nelis Willers

unread,
Apr 17, 2017, 6:14:25 AM4/17/17
to SonoffUsers
Hi David

Yes, I agree. I think we are talking about different things here. I very much like the idea of using telemetry for regular positive confirmation of the availability of the Sonoff.

My need is to set PowerOnState to 1, so that: Turn relay(s) on after power on.  I can then use a wall-switch power down and then power up (the Sonoff) to maintain the old manual-style flick-the-switch operation. In other words, the user switch on the light by flipping the wall switch only, with no automation app interaction. I don't really want to pull out my phone every time I enter the room. Obviously when the wall switch is off the automation will not work.  If I do want want the automation control, I will just leave the wall switch on.

Off course the better way to achieve this is to have PIR sensor at the door's threshold, or a touch button to switch on/off the light; this is planned, but I don't have that yet.

So the light is intentionally turned on, therefore I would like to use the stat message to tell automation about the user decision. This is the equivalent of the intentional button press on the Sonoff, except that it must only happen when PowerOnState is set to on/1 and the power comes on.

I will study the code for single vutton press, this is more or less what I want to achieve.

thanks for your time and patient consideration :-)

nelis
 

David Lang

unread,
Apr 17, 2017, 8:02:14 AM4/17/17
to Nelis Willers, SonoffUsers
I'm saying that the stat message is not generated at power on time. That would
confuse home automation software that could not tell the difference between a
deliberate local state change and a power outage.

but you should be able to watch the tele/ messages in your home automation
software to notice that the light is now on (not caring what the reason for that
is) and adjust it's internal state appropriately. I do this on openhab, I don't
know how to do it on home assistant.

David Lang



On Mon, 17 Apr 2017, Nelis Willers wrote:

> Date: Mon, 17 Apr 2017 03:14:24 -0700 (PDT)
> From: Nelis Willers <nelisw...@gmail.com>
> To: SonoffUsers <sonof...@googlegroups.com>
> Subject: Re: PowerOnState (e.g.,
> =1) relay settings at boot: MQTT state message to publish state?
>> // *will a MQTT state message here help to inform HASS?*

J. Lance Cotton

unread,
Apr 17, 2017, 10:21:01 AM4/17/17
to SonoffUsers
My need is to set PowerOnState to 1, so that: Turn relay(s) on after power on.  I can then use a wall-switch power down and then power up (the Sonoff) to maintain the old manual-style flick-the-switch operation. In other words, the user switch on the light by flipping the wall switch only, with no automation app interaction. I don't really want to pull out my phone every time I enter the room. Obviously when the wall switch is off the automation will not work.  If I do want want the automation control, I will just leave the wall switch on.

Have you considered disconnecting your wall switch from the high-voltage side and connecting it between Ground and GPIO4 (Pins 4 and 5 on the Sonoff Basic header) and following the instructions at


In this way, you can keep the automation active all the time, but also have the switch continue to operate the light/appliance as normal (when you send the SwitchMode to "FOLLOW".  The Sonoff module stays powered all the time, and if the external switch changes from off to on, the module turns on the relay. If the external switch changes from on to off, the module turns off the relay.

-Lance

Nelis Willers

unread,
Apr 17, 2017, 4:01:55 PM4/17/17
to SonoffUsers
Hi Lance,

thanks for the tip! it is exactly what I want!  I missed this approach when reading through the docs. Spent some time on the SwitchMode and SwitchTopic, but missed the point that it can be used with the extra GPIO pin.  Also, the wall switch as a designed-in feature is new to me.

Thanks!

n

J. Lance Cotton

unread,
Apr 17, 2017, 4:15:28 PM4/17/17
to SonoffUsers
On Mon, Apr 17, 2017 at 3:01 PM, Nelis Willers <nelisw...@gmail.com> wrote:
thanks for the tip! it is exactly what I want!  I missed this approach when reading through the docs. Spent some time on the SwitchMode and SwitchTopic, but missed the point that it can be used with the extra GPIO pin.  Also, the wall switch as a designed-in feature is new to me.
Have you considered disconnecting your wall switch from the high-voltage side and connecting it between Ground and GPIO4 (Pins 4 and 5 on the Sonoff Basic header) and following the instructions at


 
For safety, I should add: I noted that you would disconnect your wall switch from the high-voltage side, but don't take that to mean it's operating a low-voltage -- the GND of the sonoff 3.3v side is referenced to AC Neutral -- not true Ground, so still avoid leaving contacts exposed, etc, while the lines are hot.

-Lance
--
J. Lance Cotton, KJ5O
j...@lightningflash.net
http://kj5o.lightningflash.net
Three Step Plan: 1. Take over the world. 2. Get a lot of cookies. 3. Eat the cookies.

Nelis Willers

unread,
Apr 18, 2017, 2:08:08 AM4/18/17
to SonoffUsers
Hi Lance

understood, thanks for the safety note.

The wall-switch approach requires some rewiring in my case. Our local (South Africa) wiring practice/codes require that the live wire to go down/up to/from the wall switch, with the neutral only going directly to the light fitting. So the neutral is never 'exposed' and is seldom routed along with the live wire. These are almost two separate wiring layouts, one for neutral and another for live, with all switching done on the live wires only.  The switches are enclosed in properly earthed steel boxes, so it is fairly safe.  All connections in the roof are also required to be enclosed in small boxes, used to be steel conduit and boxes, but nowadays it is all plastic.

I do note your comment that the Sonoff should be considered 'live' from a safety perspective.

thanks!

n

Johnathan Wee

unread,
Nov 11, 2018, 10:03:23 PM11/11/18
to SonoffUsers
Hi David,
Can you share how you did this on openhab?

"I have openhab configured to watch 
for either the stat/ or the tele/ message to update it's internal status on the 
light, and set the loginterval to 60 seconds so that within a minute of powering 
a device on or restarting openhab, it has the correct status for all the 
devices."

much apprecaiated.

Thanks!
Reply all
Reply to author
Forward
0 new messages