external Switches and Buttons

1,931 views
Skip to first unread message

Christoph Wempe

unread,
Mar 22, 2017, 4:45:19 PM3/22/17
to SonoffUsers
I flashed the latest firmware on my Wemos D1 mini pro.
Then I configured the module type as "Wemos" and the GPIOs as following:
GPIO12: 17 Relay1
GPIO13: 09 Switch1
GPIO14: 04 DS18x20
GPIO15: 10 Switch2

I want du use the Switches as external Switches.
For testing I am just connecting a cable between GND and GPIO13/15.
I would expect to see something like 
stat/wemos02/SWITCH1 ON
stat/wemos02/SWITCH1 OFF
stat/wemos02/SWITCH2 ON
stat/wemos02/SWITCH2 OFF

Instead I see 
stat/wemos02/POWER ON
stat/wemos02/POWER OFF
because the Relay1 ist turned on/off.

But only when connecting Switch1.
Changing SwitchMode does change the behavier of Switch1, but it still controls Relay1.

1. What am I doing wrong?
2. I think Relay1 ist connected to Switch1 by the firmware.
Can I change that?
3. What is the difference between Switch- and Button-Sonsors?

David Lang

unread,
Mar 22, 2017, 5:37:40 PM3/22/17
to Christoph Wempe, SonoffUsers
you need to change switchmode1 and switchmode2 not just switchmode

buttons are less flexible than switches, (they don't have switchmode) and are
affected by buttontopic instead of switchtopic.

historically, buttons were the built-in momentary buttons and not user
configurable, while switches were the add-ons.

They generate power commands not switch commands.

If you don't set switchtopic, they will control the local relay, if you do set
switchtopic you will get cmnd/*/power[12] outputs as you switch them to ground.

David Lang

Christoph Wempe

unread,
Mar 22, 2017, 5:42:54 PM3/22/17
to SonoffUsers, christo...@gmail.com, da...@lang.hm
I used switchmode1 and switchmode2.

But I think "switchtopic" is the part I missed. :9

Thanks

Christoph Wempe

unread,
Mar 22, 2017, 6:47:18 PM3/22/17
to SonoffUsers, christo...@gmail.com, da...@lang.hm
I finally got it working.

  • Not all GPIOs in the Wemos D1 min seem to be able to be used in Switch mode.
  • I successfully use GPIO1, 3, 4 and 12 now.
  • SwitchMode[x] 2
  • SwitchTopic[x] 1



I still got two questions:

  1.  Is the following page linked in the Wiki?
    https://github.com/arendst/Sonoff-Tasmota/wiki/Expanding-Sonoffs
    I did not find it when I was looking for my problem.
    Only when I searched through the repository I found it.
  2. When switching my new switches, I get the following output in mqtt (many Command messages).
    I am not sure what that means or if this is even supposed to happen.
cmnd/wemos02/POWER1 ON
stat/wemos02/COMMANDS1 {"Commands1":"Status, SaveData, SaveSate, Sleep, Upgrade, Otaurl, Restart, Reset, WifiConfig, Seriallog, Syslog, LogHost, LogPort, SSId1, SSId2, Password1, Password2, AP, Hostname, Module, Modules, GPIO, GPIOs"}
stat/wemos02/COMMANDS2 {"Commands2":"Mqtt, MqttHost, MqttPort, MqttUser, MqttPassword, MqttClient, Topic, ButtonTopic, ButtonRetain, SwitchTopic, SwitchRetain, PowerRetain, GroupTopic, Units, Timezone, LedState, LedPower, TelePeriod"}
stat/wemos02/COMMANDS3 {"Commands3":"Power1, Power2, Light1 Light2, PowerOnState, PulseTime, BlinkTime, BlinkCount, ButtonRestrict, NtpServer, Weblog, Webserver, WebPassword, Emulation, SwitchMode"}
cmnd/wemos02/POWER1 OFF
stat/wemos02/COMMANDS1 {"Commands1":"Status, SaveData, SaveSate, Sleep, Upgrade, Otaurl, Restart, Reset, WifiConfig, Seriallog, Syslog, LogHost, LogPort, SSId1, SSId2, Password1, Password2, AP, Hostname, Module, Modules, GPIO, GPIOs"}
stat/wemos02/COMMANDS2 {"Commands2":"Mqtt, MqttHost, MqttPort, MqttUser, MqttPassword, MqttClient, Topic, ButtonTopic, ButtonRetain, SwitchTopic, SwitchRetain, PowerRetain, GroupTopic, Units, Timezone, LedState, LedPower, TelePeriod"}
stat/wemos02/COMMANDS3 {"Commands3":"Power1, Power2, Light1 Light2, PowerOnState, PulseTime, BlinkTime, BlinkCount, ButtonRestrict, NtpServer, Weblog, Webserver, WebPassword, Emulation, SwitchMode"}
cmnd/wemos02/POWER3 ON
stat/wemos02/COMMANDS1 {"Commands1":"Status, SaveData, SaveSate, Sleep, Upgrade, Otaurl, Restart, Reset, WifiConfig, Seriallog, Syslog, LogHost, LogPort, SSId1, SSId2, Password1, Password2, AP, Hostname, Module, Modules, GPIO, GPIOs"}
stat/wemos02/COMMANDS2 {"Commands2":"Mqtt, MqttHost, MqttPort, MqttUser, MqttPassword, MqttClient, Topic, ButtonTopic, ButtonRetain, SwitchTopic, SwitchRetain, PowerRetain, GroupTopic, Units, Timezone, LedState, LedPower, TelePeriod"}
stat/wemos02/COMMANDS3 {"Commands3":"Power1, Power2, Light1 Light2, PowerOnState, PulseTime, BlinkTime, BlinkCount, ButtonRestrict, NtpServer, Weblog, Webserver, WebPassword, Emulation, SwitchMode"}
cmnd/wemos02/POWER3 OFF
stat/wemos02/COMMANDS1 {"Commands1":"Status, SaveData, SaveSate, Sleep, Upgrade, Otaurl, Restart, Reset, WifiConfig, Seriallog, Syslog, LogHost, LogPort, SSId1, SSId2, Password1, Password2, AP, Hostname, Module, Modules, GPIO, GPIOs"}
stat/wemos02/COMMANDS2 {"Commands2":"Mqtt, MqttHost, MqttPort, MqttUser, MqttPassword, MqttClient, Topic, ButtonTopic, ButtonRetain, SwitchTopic, SwitchRetain, PowerRetain, GroupTopic, Units, Timezone, LedState, LedPower, TelePeriod"}
stat/wemos02/COMMANDS3 {"Commands3":"Power1, Power2, Light1 Light2, PowerOnState, PulseTime, BlinkTime, BlinkCount, ButtonRestrict, NtpServer, Weblog, Webserver, WebPassword, Emulation, SwitchMode"}
cmnd/wemos02/POWER2 ON
stat/wemos02/COMMANDS1 {"Commands1":"Status, SaveData, SaveSate, Sleep, Upgrade, Otaurl, Restart, Reset, WifiConfig, Seriallog, Syslog, LogHost, LogPort, SSId1, SSId2, Password1, Password2, AP, Hostname, Module, Modules, GPIO, GPIOs"}
stat/wemos02/COMMANDS2 {"Commands2":"Mqtt, MqttHost, MqttPort, MqttUser, MqttPassword, MqttClient, Topic, ButtonTopic, ButtonRetain, SwitchTopic, SwitchRetain, PowerRetain, GroupTopic, Units, Timezone, LedState, LedPower, TelePeriod"}
stat/wemos02/COMMANDS3 {"Commands3":"Power1, Power2, Light1 Light2, PowerOnState, PulseTime, BlinkTime, BlinkCount, ButtonRestrict, NtpServer, Weblog, Webserver, WebPassword, Emulation, SwitchMode"}

 

Christoph Wempe

unread,
Mar 22, 2017, 7:05:15 PM3/22/17
to SonoffUsers, christo...@gmail.com, da...@lang.hm
Third question:

I defined a Relay1 now.
But when switching Switch1, it sends a command to the relay1.

I think I understand the system now. :)
The command a Switch sends is supposed to send a command to se same (switchtopic 1) or another (switchtopic <yyy>) Sonoff-device.

When I set "SwitchTopic1 test1" I get this.
cmnd/test1/POWER1 ON
cmnd/test1/POWER1 OFF

And since this is not the name/topic of the device itself the relay does not get triggered.


Am I the only one who had difficulties to understand the concept? :)
I could try to enhance the wiki with some examples about this topic.
Unless its is not needed or somebody with better understanding about this topic would like to do it. :)

David Lang

unread,
Mar 22, 2017, 7:18:30 PM3/22/17
to Christoph Wempe, SonoffUsers
On Wed, 22 Mar 2017, Christoph Wempe wrote:

>
>
> 1. Is the following page linked in the Wiki?
> https://github.com/arendst/Sonoff-Tasmota/wiki/Expanding-Sonoffs
> I did not find it when I was looking for my problem.
> Only when I searched through the repository I found it.

yes, it's one of the links at the bottom of the sidebar.

> 2. When switching my new switches, I get the following output in mqtt
> (many Command messages).
> I am not sure what that means or if this is even supposed to happen.

This indicates that you are getting invalid commands.

make sure that switchtopic is different from topic (otherwise you are sending
commands to yourself through the mqtt server)

increase log level to see exactly what's happening.

David Lang

David Lang

unread,
Mar 22, 2017, 7:22:42 PM3/22/17
to Christoph Wempe, SonoffUsers
On Wed, 22 Mar 2017, Christoph Wempe wrote:

> Third question:
>
> I defined a Relay1 now.
> But when switching Switch1, it sends a command to the relay1.
>
> I think I understand the system now. :)
> The command a Switch sends is supposed to send a command to se same
> (switchtopic 1) or another (switchtopic <yyy>) Sonoff-device.

more precisely, with switchtopic <yyy> it sends a message to the mqtt server for
any other devices (or home automation server) to listen to and act on. The
common case is to have the home automation software watch for these and have
rules to trigger other things.

> When I set "SwitchTopic1 test1" I get this.
> cmnd/test1/POWER1 ON
> cmnd/test1/POWER1 OFF
>
> And since this is not the name/topic of the device itself the relay does
> not get triggered.
>
>
> Am I the only one who had difficulties to understand the concept? :)
> I could try to enhance the wiki with some examples about this topic.
> Unless its is not needed or somebody with better understanding about this
> topic would like to do it. :)

Please add an example page to the wiki. I touched on this when I made the
Expanding page (the link is "wiring additional things..." in the sidebar, but
hadn't actually hooked up any switches at the time.

a concise "here's how to hook up a switch" would be good.

David Lang

Pǝtǝr FlCKlNGER

unread,
Mar 23, 2017, 8:45:28 AM3/23/17
to SonoffUsers, christo...@gmail.com, da...@lang.hm
Am Donnerstag, 23. März 2017 00:05:15 UTC+1 schrieb Christoph Wempe:
The command a Switch sends is supposed to send a command to se same (switchtopic 1) or another (switchtopic <yyy>) Sonoff-device.

When I set "SwitchTopic1 test1" I get this.
cmnd/test1/POWER1 ON
cmnd/test1/POWER1 OFF

And since this is not the name/topic of the device itself the relay does not get triggered.

My experiments got the same result.
 
Am I the only one who had difficulties to understand the concept? :)

absolutely not! I also had many difficulities with it. Till now, the deeper purpose of this approach is hiding from me. I don't understand the logic behind, but I guess there must be a deeper meaning.

At the moment I understand the following:
If switchtopic is NOT set, then the relay will follow the state of the switch and sends a mqtt message in case of a change. If I set switchtopic to a value different to PROJECT name (is this right?) then it only sends a mqtt message and the relay has to be controlled by another returning mqtt message - or another device can be controlled.

To set a switchtopic breaks the internal connection to the relay?

But what, if there are more than one switches connected? And if more the one relay exists? Could this be handled at the moment?

I could try to enhance the wiki with some examples about this topic.

This would be great!

David Lang

unread,
Mar 23, 2017, 10:01:39 AM3/23/17
to Pǝtǝr FlCKlNGER, SonoffUsers, christo...@gmail.com
On Thu, 23 Mar 2017, 'Pǝtǝr FlCKlNGER' via SonoffUsers wrote:

> At the moment I understand the following:
> If switchtopic is NOT set, then the relay will follow the state of the
> switch and sends a mqtt message in case of a change. If I set switchtopic
> to a value different to PROJECT name (is this right?) then it only sends a
> mqtt message and the relay has to be controlled by another returning mqtt
> message - or another device can be controlled.
>
> To set a switchtopic breaks the internal connection to the relay?
>
> But what, if there are more than one switches connected? And if more the
> one relay exists? Could this be handled at the moment?

Yes, when there are multiple relays or switches, you get power1, power2, etc in
the messages instead of just power

This works well with switchtopic/buttontopic set, but there is a little bit of
a problem with buttons if only one is set (from the initial defaults that #
buttons == # relays)

David Lang

Christoph Wempe

unread,
Mar 29, 2017, 5:17:31 PM3/29/17
to SonoffUsers, christo...@gmail.com, da...@lang.hm
This is what I came up with.


Feel free to criticize or improve. :)

I am not sure where this should be linked in the wiki.

Ruben Tavares

unread,
Mar 30, 2017, 11:02:15 AM3/30/17
to SonoffUsers, christo...@gmail.com, da...@lang.hm
Great article :D

Pǝtǝr FlCKlNGER

unread,
Mar 31, 2017, 8:10:36 AM3/31/17
to SonoffUsers, christo...@gmail.com, da...@lang.hm
perfect!

much thanks for your explanation!
Message has been deleted
Message has been deleted

hooverke...@gmail.com

unread,
Jul 21, 2018, 5:21:07 PM7/21/18
to SonoffUsers
Hello, everyone - I am trying to get 4 switch inputs to work on my Wemos D1 mini. Here's where I am:

4 switches are wired into GPIO 4, 5, 12, and 14
Switches are all configured and working as expected (tele state shows all 4 correctly)
There are no other devices on the wemos, I'm only using it to monitor 4 switches and send mqtt commands to openhab

I've set the following commands in the console:
 -switchmode1 2
 -switchmode2 2
 -switchmode3 2
 -switchmode4 2
 -switchtopic wemos2

And I've tried to do as described above - but "switchtopic1 wemos1" and "switchtopic2 wemos2" aren't changing the specific switches. The final goal is to have the 4 switches wired to the wemos (done) and send mqtt commands each time they are switched either on or off.

If anyone's still listening on this thread I'd really appreciate some help! Thanks!

Philip Knowles

unread,
Jul 21, 2018, 6:27:17 PM7/21/18
to hooverke...@gmail.com, SonoffUsers

I think you’ll need to use rules to get that to work properly. Use the switch as a trigger for the rule to send the switch status via MQTT. There’s information in the examples in the Tasmota wki.

 

Regards

 

Phil

 

Sent from Mail for Windows 10

--
You received this message because you are subscribed to the Google Groups "SonoffUsers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonoffusers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

 

PF

unread,
Jul 22, 2018, 8:22:43 AM7/22/18
to SonoffUsers
Seems like they don't behave right until you setup a relay for each switch even if you don't use it. That is how I do it on door switch monitors. (Never use the relay with those)
Reply all
Reply to author
Forward
0 new messages