How to toggle light on and off , between Cayenne app and momentary switch, for home automation?


Trying to do some home automation and I can’t figure out how to get Cayenne app on my Raspberry Pi to do what I want it to do. I want to be able to turn a light on and off from the Cayenne app on my phone and be able to press a momentary switch in that room to do the same thing.The momentary switch has to latch to keep the light on too. So if I turn the light on at the momentary switch in the room and say I leave my house and forgot to turn the light off I can just turn if off using the app. I want to be able to toggle the light on and off, between the app and momentary switch.


I used the remote Pi3 GPIO (x) as a binary switch input-
wired to a standard light switch,

and I use GPIO (y) as a binary output to a generic power relay.

when the light switch is on, the GPIO (x) (usually floating high)
line is shorted to (-).

If GPIO (x) = 0, turn GPIO (y) on
If GPIO (x) = 1, turn GPIO (y) off

-concurrently, you have the Cayenne App,
controlling GPIO (y) via WiFi…
in THEORY, it works.

(I’m using this way right now)
Think I’ll make a short video…


-you could 3.way the remote Cayenne relay,
and add a standard 3-way light switch.


OR use the brilliant solution

offered by @joaoduarte_89

-using a remote GPIO-out pin
to toggle a remote associated GPIO-in.
that, in turn, sends an “off”,
and you have a momentary contact!

Here’s a video

showing the “software” 3-way switch.
Either the physical switch or the Cayenne can control the light-


The video does do want I want but I need to see how he did it. I cant find anything on the his youtube account. The Cayenne software doesn’t have a way to make it a three way from what I’ve seen. Is it possible to make a trigger that just changes the state of an GPIO output?


Look up 3-way switch diagrams. when I install 3 way switches I start with the travel wire. Start your wiring at the wall switch be sure that you have a 3 conductor (red,black,white and ground) romex running between wall switch and relay. Hook up the hot at the wall switch first assuming you have shut off the circuit breaker :slight_smile: . look up these images for reference,;_ylt=A0LEV0HpixNY8HUAuYxXNyoA;_ylu=X3oDMTE0MnY2MjN0BGNvbG8DYmYxBHBvcwMxBHZ0aWQDQjE4NzlfMQRzZWMDcGl2cw--?p=3-way+light+switch+diagram&fr=mcafee&fr2=piv-web#id=1&

You may have to sacrifice a 3-way switch to get an idea how the terminals are switched internally so you can wire your relay appropriately unless you can find an image, just drill out the rivets. I think the travel wire goes to the center tap of the relay. When 1 switch is switched off it passes hot to the other switch(relay) so that switch can get voltage to turn on the light so forth as so on.

Best image I could find:

Hope this helps a little more and not confused you more. W


Um- I gave you screen shots, and the 2 trigger rules to make it happen-
The video shows SOFTWARE switching- (low voltage switch)
ANY remote Cayenne device can trigger the light-
using the If Then argument.
The relay just follows the last sent command-

I showed when the light switch was off, -and I triggered the relay via my android.
The physical switch still showed off, but the relay was on.
When I turned the physical switch on, -nothing happened, because the light was already on.
The app correctly shows the states of all the inputs and outputs.
Stop the video to observe the screenshot more closely.


BTW, -if you can’t confidently wire
a line-voltage3-way or 4-way switch,
you probably shouldn’t do it…
at the very least,
don’t do it with the line current on!


Thanks for the info wmontg. High tech thanks too, I finally got around to actually wiring an led to my pi to actually see everything working. I already had the triggers set up like the ones you sent me. The only problem I thought I would have was turning the light off at the switch in the room while it was already being turned on from the app. I decided to do away with a momentary switch too. After testing everything I figured out that I could turn the light off at the switch if I just turned it on then back off. Doing that shut the light off even though the app had it turned on. Thank you for your concern on wiring a three way switch too. If I didn’t know how to wire that I won’t be a very good electrician lol. One reason I’m wanting to do this is because is I’m trying to find a affordable home automation for some of my customers.


Yup. :slight_smile: Software 3-way, or 4-way, or whatever way.
Latest command to be sent gets executed,
regardless of the GPIO input.

The problem with software switches vs. hardwired 3-way is-
with LV, you can’t control a series (standard) dimmer,
with LV, if you system fails,
you have no switching control.

With HV 3-way, you still have
physical switching control
and you can use standard 3-way dimmers.

for HIGH Voltage 3-way switching,
High Voltage RELAY outputs-
Use the Normally Open (N/O), black traveler on #14-3 romex
Normally Closed (N/C) , red traveler on #14-3 romex
The Line-In neutral (white) simply passes from the Neutral Line in
to the #14 romex white Neutral line out…
to the white #14 romex of the remote Neutral line load.
Connect your Line Power(in) BLACK to the ©, relay Commuter.
Now, power will flow
to the RED traveler when the relay is relaxed,
and the black traveler when the relay is energized.

At the OTHER end of the 3-way,
your #14-3 romex red and black will plug into
the 3-way Switch Traveler screw lugs…
The BLACK screw on the 3-way is the Commuter ©.
That screw will attach to (P-load)
go to your LOAD (light, whatever).
Tie the white Neutrals together.
If you want to add an additional physical 4-way switch,
just run the red and black #14 romex travelers
to the respective in/out screw lugs on the 4-way switch.
You can add as many functional 4-way switches as you need.

So- 1 of the 3-ways must be the Form C of the Cayenne project,
and 1 must be the physical 3-way to complete the circuit.
You can add additional physical 4-way switches as needed.

See- on a 3-way, one of the screw terminals is painted black. That is the Commuter.

Here’s a 4-way switch. Travelers in, travelers out…


Could some of you help me? The triggers don’t work for me.It just doesn’t do anything even though the button is pushed.

the code:
#include <CayenneMQTTESP8266.h>
#include <DHT.h>
#define CAYENNE_PRINT Serial

char ssid[] = "im";
char password[] = "not clever";

char username[]="k";
char mqtt_password[] = "boi";
char client_id[] = "thanks";
int buttonstate;

void setup() {
  // put your setup code here, to run once:
pinMode(D6, OUTPUT);
digitalWrite(D6, LOW);
pinMode(D5, OUTPUT);
digitalWrite(D5, LOW);
pinMode(D7, INPUT);

void loop() {
  // put your main code here, to run repeatedly:
int  buttonstate = digitalRead(D7);
if(buttonstate == HIGH) {
  Cayenne.virtualWrite(2, HIGH);
}else {
  Cayenne.virtualWrite(2, LOW);

  digitalWrite(D6, getValue.asInt());
  digitalWrite(D5, getValue.asInt());

Toggling Button State

Note to yourself, be sure to edit out user name, password and client id.:blush: