relay as a switch not as a button (domoticz)

  • Hi,
    I need help for the 'relay Switch' code.
    you can help with this code, I would like to use relay as a switch not as a button .
    (I'm Italian, I'm sorry for the bad English).
    thanks to you.

  • @ago1980 Help us understand your question a little better. I am not sure what you mean by use the relay as a switch. What kind of switch?

  • @dbemowsk hello I mean switches from classics from home. Thanks

  • Hero Member

    Hi @ago1980 Is it the RelayWithButtonActuator sketch you are looking to modify so it can be used with a toggle switch instead of a push button?

  • @Boots33 I would like to use with switch, I find nothing on.

  • Hero Member

    @ago1980 You can find the relay with button sketch Here

    That sketch uses a push button, to use that sketch with a toggle switch you should just need to make a couple of small changes in the loop part of the sketch.

    void loop() 
      // Get the update value
      int value =;
      if (value != oldValue && value==0) {
          send(msg.set(state?false:true), true); // Send new state and request ack back
      oldValue = value;
    if (value != oldValue) {
          send(msg.set(state?false:true), true); // Send new state and request ack back
          oldValue = value;

  • @Boots33 ok, but then the status is updated Domoticz?
    thank you

  • Hero Member

    @ago1980 Yes it should still update the status in domoticz when you use the switch.

  • @Boots33 ok, tomorrow I try and let me know. Thanks so much

  • @dbemowsk ok thanks clear code explanation, but I did not understand well what to do to hardware part.

  • @ago1980 So in the diagram below I show a wall switch and the push button switch that is normally used in this example. Simply remove the push button and attach the wall switch.

    Does this clear up the confusion?

  • @dbemowsk So you just have to remove the button and put the switch on? or put the switch in parallel with the button?Thank you

  • @ago1980 Yes. If you use the switch, you will need to use the code changes that I pointed out in the other thread.

  • @ago1980 With the switch in place you don't want to run it in parallel with the button. When the switch is in the on position, the connection will be shorted in which case the push button would not work. This is the reason for the code change. It is to detect when the switch changed from off to on, or on to off.

  • I have a problem when the radio nrf24l01 loses contact with the gateway does not work the node, how can I do? thank you

  • @ago1980 DO you have a capacitor on the nRF radio? How far away from the gateway is the node? Is there anything in between the gateway and the node such as windows or walls? If it is really far from the gateway, you will probably need a repeater node in between.

  • @dbemowsk I mean if GATEWAY does not work for some problem the node does not work does not turn on light

  • @ago1980 Are you sure the problem is with the gateway? Can you post pictures of your node and your gateway? Having those will allow us to help you better.

  • Hero Member

    @ago1980 said in relay as a switch not as a button (domoticz):

    I mean if GATEWAY does not work for some problem the node does not work does not turn on light

    The original sketch relies on an ack from the gateway to trigger the relay, so if the gateway is not available the relay state cannot be changed by the local switch or even the controller.

    Under most circumstances it would be desirable to have the local switch function no matter what the state of the network is. To do this you will need to modify the sketch so it no longer relies on the ack to make the change.

    In MySensors 2.1.1 by default a node will not boot through to the loop section of your sketch if it cannot find the gateway, so the first thing you need to do is force it to move on.

    The line shown below will do just that. The number at the end is how long you want the node to wait for an uplink to be established before it will move on to the rest of your sketch. It is in milliseconds, so in the example below it will wait 5 seconds. This line needs to be inserted near the top of your sketch before the #include <MySensors.h> line.


    After adding that you will then need to change the sketch so it no longer relies on the ack to change the relay state. that is pretty straight forward and once done your relay will then be able to be switched by the local switch no mater what the uplink status is.

    The next problem you will encounter is trying to keep your controller in sync with the local node. As you can now change the state of the relay without a connection to the controller it may loose its sync to the node. So you may find your controller thinks the relay is on when it is actually off. This is where it gets a little trickier so if it is important to you that the controller stays in sync you may need to experiment a bit to find what will give you the best results.

    I have some examples of relay nodes that explore ways to switch locally and try to maintain sync with the controller.
    The last sketch I posted there uses requestTime() to check to see if the controller is available and though there is a trade off in how quickly you can flick the switch back and forward it at least confirms that the controller is available. Other methods may only check for the gateways presence.

    Have a look you may find something of use.

    Synchronising Light switch

    Its a bit of an old post but some of these issues (using a toggle switch and making the switch always work) were also discussed in this post

  • @boots33 ok, thanks, so if I use switches I should not have problems? I follow this thread

  • Hero Member

    @ago1980 said in relay as a switch not as a button (domoticz):

    so if I use switches I should not have problems?

    Other than the issues I have outlined in my post above the toggle switch will work just like the button.

    The basic re-write of the original button with relay sketch so it will work with a toggle switch and no uplink to the gateway is shown below. Remember it does not try and keep in sync with the controller other than notifying it of a change by the switch.
    As @dbemowsk has already said you should also try and find out why the node is losing its uplink to the gateway.

    Sketch untested but I think should be ok

       Relay with toggle switch sketch
       modified to work with no uplink
       to gateway
       Toggle switch connected between pin3 and ground.
    #define MY_DEBUG                               // Enable debug prints to serial monitor
    #define MY_RADIO_NRF24                         // Enable and select radio type attached
    #define MY_TRANSPORT_WAIT_READY_MS 5000        //set how long to wait for transport ready in milliseconds
    #include <MySensors.h>
    #include <Bounce2.h>
    #define RELAY_PIN  4      // Arduino Digital I/O pin number for relay 
    #define SWITCH_PIN  3     // Arduino Digital I/O pin number for switch 
    #define CHILD_ID 1        // Id of the sensor child
    #define RELAY_ON 1
    #define RELAY_OFF 0
    Bounce debouncer = Bounce();
    int oldswitchState = 0;
    bool state = false;
    bool firstStart = true;
    MyMessage msg(CHILD_ID, V_STATUS);
    void setup(){
      pinMode(SWITCH_PIN, INPUT_PULLUP);           // Setup the button pin, Activate internal pull-up  
      debouncer.attach(SWITCH_PIN);                // After setting up the button, setup debouncer
      pinMode(RELAY_PIN, OUTPUT);                 // set relay pin in output mode
      digitalWrite(RELAY_PIN, RELAY_OFF);         // Make sure relay is off when starting up
    void presentation()  {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Relay & Toggle", "1.0");
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID, S_BINARY);
    void loop(){
    if (firstStart) {                            // this code is only run once at startup
      oldswitchState =;         // set oldswitchState to the current toggle switch state
      send(msg.set(false), false);                              // notify controller of current state no ack 
      firstStart = false;                       // set firstStart flag false to prevent code from running again
      int switchState =;                               // Get the update value
      if (switchState != oldswitchState) {                                    // check for new throw of toggle switch
        state =  !state;                                          // Toggle the state
        digitalWrite(RELAY_PIN, state ? RELAY_ON : RELAY_OFF);    // switch the relay to the new state
        send(msg.set(state), false);                              // notify controller of current state no ack   
       oldswitchState = switchState;
    /*-------------------start of functions--------------------------*/
    void receive(const MyMessage &message) {
      if (message.type == V_STATUS) {                                   // check to see if incoming message is for a switch                                      
            state = message.getBool();                                  // get the new state
            digitalWrite(RELAY_PIN, state ? RELAY_ON : RELAY_OFF);      // switch relay to new state
            /*---- Write some debug info----*/
            Serial.print("Incoming change for sensor:");
            Serial.print(", New status: ");

  • @boots33 ok thanks does it work, wanting to add another relay?

  • Hero Member

    @ago1980 It should work but as I said i have not tested it. You may still need to tweak it to suit your setup.

    To add extra relays I usually just duplicate the code if it is only two relays but more than that you will probably want to use a loop and perhaps an array to assign the variables etc. You will also need to add some code to your void receive function to sort out which switch/relay the incoming message is for.

    The RelayActuator sketch in the MySensors examples shows a way to add more than one relay.

    You could also have a look at my AC power controller project for an idea of how to use a case statement in the receive function to filter the incoming messages.