relay as a switch not as a button (domoticz)

  • @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.