Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Troubleshooting
  3. Controlling Relay Actuator

Controlling Relay Actuator

Scheduled Pinned Locked Moved Troubleshooting
17 Posts 6 Posters 6.7k Views 5 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PostaanErik
    wrote on last edited by sundberg84
    #7

    Im using the example RelayWithButtonActuator:

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik Ekblad
     * 
     * DESCRIPTION
     * Example sketch for a "light switch" where you can control light or something 
     * else from both HA controller and a local physical button 
     * (connected between digital pin 3 and GND).
     * This node also works as a repeader for other nodes
     * http://www.mysensors.org/build/relay
     */ 
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    
    #define RELAY_PIN  4  // Arduino Digital I/O pin number for relay 
    #define BUTTON_PIN  3  // Arduino Digital I/O pin number for button 
    #define CHILD_ID 1   // Id of the sensor child
    #define RELAY_ON 1
    #define RELAY_OFF 0
    
    Bounce debouncer = Bounce(); 
    int oldValue=0;
    bool state;
    MySensor gw;
    MyMessage msg(CHILD_ID,V_LIGHT);
    
    void setup()  
    {  
      gw.begin(incomingMessage, AUTO, true);
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Relay & Button", "1.0");
    
     // Setup the button
      pinMode(BUTTON_PIN,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN,HIGH);
      
      // After setting up the button, setup debouncer
      debouncer.attach(BUTTON_PIN);
      debouncer.interval(5);
    
      // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID, S_LIGHT);
    
      // Make sure relays are off when starting up
      digitalWrite(RELAY_PIN, RELAY_OFF);
      // Then set relay pins in output mode
      pinMode(RELAY_PIN, OUTPUT);   
          
      // Set relay to last known state (using eeprom storage) 
      state = gw.loadState(CHILD_ID);
      digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
    }
    
    
    /*
    *  Example on how to asynchronously check for new messages from gw
    */
    void loop() 
    {
      gw.process();
      debouncer.update();
      // Get the update value
      int value = debouncer.read();
      if (value != oldValue && value==0) {
          gw.send(msg.set(state?false:true), true); // Send new state and request ack back
      }
      oldValue = value;
    } 
     
    void incomingMessage(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.isAck()) {
         Serial.println("This is an ack from gateway");
      }
    
      if (message.type == V_LIGHT) {
         // Change relay state
         state = message.getBool();
         digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         gw.saveState(CHILD_ID, state);
        
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
    
       } 
    }
    
    1 Reply Last reply
    0
    • sundberg84S Offline
      sundberg84S Offline
      sundberg84
      Hardware Contributor
      wrote on last edited by sundberg84
      #8

      I have read alot of reports here lately with this issue in both Domoticz and Vera (pushing button works, but not sending from controller to relay). I dont know why but i tried it myself with 2.0b and exact the same behaivour. It works with my 1.4 relays but not 2.0...

      I have no more info at this time but will try to figure something out when i have the time...

      Controller: Proxmox VM - Home Assistant
      MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
      MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
      RFLink GW - Arduino Mega + RFLink Shield, 433mhz

      1 Reply Last reply
      1
      • sundberg84S Offline
        sundberg84S Offline
        sundberg84
        Hardware Contributor
        wrote on last edited by sundberg84
        #9

        Have not been able to test this (@ work), but according to Domoticz github it looks like Domoticz sends V_STATUS to update a Lightning2.
        In the sketch we have message.type == V_LIGHT

        MySensors github:

        S_LIGHT, // Binary light or relay, V_STATUS (or V_LIGHT), V_WATT
        

        Domoticz github:

        SendNodeSetCommand(node_id, child_sensor_id, MT_Set, V_STATUS, lState, pChild->useAck);
        

        MySensors Github RelayWityButtonAcc

        if (message.type == V_LIGHT) {
        

        Im not that good in understanding coding, but this would be the first im going to experiment with tonight.

        Controller: Proxmox VM - Home Assistant
        MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
        MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
        RFLink GW - Arduino Mega + RFLink Shield, 433mhz

        1 Reply Last reply
        0
        • BartEB Offline
          BartEB Offline
          BartE
          Contest Winner
          wrote on last edited by BartE
          #10

          V_LIGHT and V_STATUS are complementary to each other they are both are defined as 2.

          There are however a couple of remarks regarding your code.

          1. when the button is pressed only a message is sent to the GW the relay is not toggled
          2. when a state change request is received there is no status update sent to the gatewat
          3. a minor issue debouncer.update() return true when a change change was detected so you do not have to remember the old state.

          All three comments I've processed in the sketch below you might want to try this

          /**
           * The MySensors Arduino library handles the wireless radio link and protocol
           * between your home built sensors/actuators and HA controller of choice.
           * The sensors forms a self healing radio network with optional repeaters. Each
           * repeater and gateway builds a routing tables in EEPROM which keeps track of the
           * network topology allowing messages to be routed to nodes.
           *
           * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
           * Copyright (C) 2013-2015 Sensnology AB
           * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
           *
           * Documentation: http://www.mysensors.org
           * Support Forum: http://forum.mysensors.org
           *
           * This program is free software; you can redistribute it and/or
           * modify it under the terms of the GNU General Public License
           * version 2 as published by the Free Software Foundation.
           *
           *******************************
           *
           * REVISION HISTORY
           * Version 1.0 - Henrik Ekblad
           * 
           * DESCRIPTION
           * Example sketch for a "light switch" where you can control light or something 
           * else from both HA controller and a local physical button 
           * (connected between digital pin 3 and GND).
           * This node also works as a repeader for other nodes
           * http://www.mysensors.org/build/relay
           */ 
          
          #include <MySensor.h>
          #include <SPI.h>
          #include <Bounce2.h>
          
          #define RELAY_PIN  4  // Arduino Digital I/O pin number for relay 
          #define BUTTON_PIN  3  // Arduino Digital I/O pin number for button 
          #define CHILD_ID 1   // Id of the sensor child
          #define RELAY_ON 1
          #define RELAY_OFF 0
          
          Bounce debouncer = Bounce(); 
          bool state;
          MySensor gw;
          MyMessage msg(CHILD_ID,V_LIGHT);
          
          void setup()  
          {  
            gw.begin(incomingMessage, AUTO, true);
          
            // Send the sketch version information to the gateway and Controller
            gw.sendSketchInfo("Relay & Button", "1.0");
          
           // Setup the button
            pinMode(BUTTON_PIN,INPUT);
            // Activate internal pull-up
            digitalWrite(BUTTON_PIN,HIGH);
            
            // After setting up the button, setup debouncer
            debouncer.attach(BUTTON_PIN);
            debouncer.interval(5);
          
            // Register all sensors to gw (they will be created as child devices)
            gw.present(CHILD_ID, S_LIGHT);
          
            // Make sure relays are off when starting up
            digitalWrite(RELAY_PIN, RELAY_OFF);
            // Then set relay pins in output mode
            pinMode(RELAY_PIN, OUTPUT);   
                
            // Set relay to last known state (using eeprom storage) 
            state = gw.loadState(CHILD_ID);
            digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
          }
          
          
          /*
          *  Example on how to asynchronously check for new messages from gw
          */
          void loop() 
          {
            gw.process();
            // Get the update value and if push button was release toggle the relay and send an update to the gw
            if (debouncer.update() && debouncer.read() != 0) {
                state = state ? true : false;
                gw.send(msg.set(state?false:true), true); // Send new state and request ack back
                digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
            }
          } 
           
          void incomingMessage(const MyMessage &message) {
            // We only expect one type of message from controller. But we better check anyway.
            if (message.isAck()) {
               Serial.println("This is an ack from gateway");
            }
          
            if (message.type == V_LIGHT) {
               // Change relay state
               state = message.getBool();
               digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
               // Store state in eeprom
               gw.saveState(CHILD_ID, state);
               
               // Send new state to the GW
               gw.send(msg.set(state?false:true)); 
                
               // Write some debug info
               Serial.print("Incoming change for sensor:");
               Serial.print(message.sensor);
               Serial.print(", New status: ");
               Serial.println(message.getBool());
          
             } 
          }
          
          martinhjelmareM 1 Reply Last reply
          0
          • sundberg84S Offline
            sundberg84S Offline
            sundberg84
            Hardware Contributor
            wrote on last edited by
            #11

            Sorry about that... yea see that they are the same now. Spared me some time there BartE - tnx. He is using the standard sketch and this has been working great ever since... strange.

            Controller: Proxmox VM - Home Assistant
            MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
            MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
            RFLink GW - Arduino Mega + RFLink Shield, 433mhz

            1 Reply Last reply
            0
            • BartEB BartE

              V_LIGHT and V_STATUS are complementary to each other they are both are defined as 2.

              There are however a couple of remarks regarding your code.

              1. when the button is pressed only a message is sent to the GW the relay is not toggled
              2. when a state change request is received there is no status update sent to the gatewat
              3. a minor issue debouncer.update() return true when a change change was detected so you do not have to remember the old state.

              All three comments I've processed in the sketch below you might want to try this

              /**
               * The MySensors Arduino library handles the wireless radio link and protocol
               * between your home built sensors/actuators and HA controller of choice.
               * The sensors forms a self healing radio network with optional repeaters. Each
               * repeater and gateway builds a routing tables in EEPROM which keeps track of the
               * network topology allowing messages to be routed to nodes.
               *
               * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
               * Copyright (C) 2013-2015 Sensnology AB
               * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
               *
               * Documentation: http://www.mysensors.org
               * Support Forum: http://forum.mysensors.org
               *
               * This program is free software; you can redistribute it and/or
               * modify it under the terms of the GNU General Public License
               * version 2 as published by the Free Software Foundation.
               *
               *******************************
               *
               * REVISION HISTORY
               * Version 1.0 - Henrik Ekblad
               * 
               * DESCRIPTION
               * Example sketch for a "light switch" where you can control light or something 
               * else from both HA controller and a local physical button 
               * (connected between digital pin 3 and GND).
               * This node also works as a repeader for other nodes
               * http://www.mysensors.org/build/relay
               */ 
              
              #include <MySensor.h>
              #include <SPI.h>
              #include <Bounce2.h>
              
              #define RELAY_PIN  4  // Arduino Digital I/O pin number for relay 
              #define BUTTON_PIN  3  // Arduino Digital I/O pin number for button 
              #define CHILD_ID 1   // Id of the sensor child
              #define RELAY_ON 1
              #define RELAY_OFF 0
              
              Bounce debouncer = Bounce(); 
              bool state;
              MySensor gw;
              MyMessage msg(CHILD_ID,V_LIGHT);
              
              void setup()  
              {  
                gw.begin(incomingMessage, AUTO, true);
              
                // Send the sketch version information to the gateway and Controller
                gw.sendSketchInfo("Relay & Button", "1.0");
              
               // Setup the button
                pinMode(BUTTON_PIN,INPUT);
                // Activate internal pull-up
                digitalWrite(BUTTON_PIN,HIGH);
                
                // After setting up the button, setup debouncer
                debouncer.attach(BUTTON_PIN);
                debouncer.interval(5);
              
                // Register all sensors to gw (they will be created as child devices)
                gw.present(CHILD_ID, S_LIGHT);
              
                // Make sure relays are off when starting up
                digitalWrite(RELAY_PIN, RELAY_OFF);
                // Then set relay pins in output mode
                pinMode(RELAY_PIN, OUTPUT);   
                    
                // Set relay to last known state (using eeprom storage) 
                state = gw.loadState(CHILD_ID);
                digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
              }
              
              
              /*
              *  Example on how to asynchronously check for new messages from gw
              */
              void loop() 
              {
                gw.process();
                // Get the update value and if push button was release toggle the relay and send an update to the gw
                if (debouncer.update() && debouncer.read() != 0) {
                    state = state ? true : false;
                    gw.send(msg.set(state?false:true), true); // Send new state and request ack back
                    digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
                }
              } 
               
              void incomingMessage(const MyMessage &message) {
                // We only expect one type of message from controller. But we better check anyway.
                if (message.isAck()) {
                   Serial.println("This is an ack from gateway");
                }
              
                if (message.type == V_LIGHT) {
                   // Change relay state
                   state = message.getBool();
                   digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
                   // Store state in eeprom
                   gw.saveState(CHILD_ID, state);
                   
                   // Send new state to the GW
                   gw.send(msg.set(state?false:true)); 
                    
                   // Write some debug info
                   Serial.print("Incoming change for sensor:");
                   Serial.print(message.sensor);
                   Serial.print(", New status: ");
                   Serial.println(message.getBool());
              
                 } 
              }
              
              martinhjelmareM Offline
              martinhjelmareM Offline
              martinhjelmare
              Plugin Developer
              wrote on last edited by
              #12

              @BartE

              Regarding 1.
              I think the point is to wait for the acknowledge from the controller before changing relay state, after the press of the button.

              1 Reply Last reply
              0
              • sundberg84S Offline
                sundberg84S Offline
                sundberg84
                Hardware Contributor
                wrote on last edited by
                #13

                and regarding 2 - why send a statusupdate to controller when the status comes from the controller? The ack should be enough?

                Controller: Proxmox VM - Home Assistant
                MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
                MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
                RFLink GW - Arduino Mega + RFLink Shield, 433mhz

                1 Reply Last reply
                0
                • sundberg84S Offline
                  sundberg84S Offline
                  sundberg84
                  Hardware Contributor
                  wrote on last edited by sundberg84
                  #14

                  So, recreated this now - all standard sketch (2.0b) except I used pin 5 instead of pin4 for relay (due to PCB pinout layout, should not matter).

                  Everything works great both from Domoticz and Myscontroller but there was some small issues...

                  1. Power - my pro mini was just a little to weak to trigger the relay... :( It needs 5v but my pin 5 was only giving 4.7V and some tries resulted in a unsuccessful relay switch even if the logs told me so.

                  2. Radio - at some points i had the radio to close to the gw, so it didnt get the ACK and therefore i got the error "Error sending command, check device/hardware" message in Domoticz .

                  This is from Myscontroller, sending a on command with ack:
                  0_1453664961508_1.JPG

                  Correct seriallog node when incoming command from controller
                  read: 0-0-4 s=1,c=1,t=2,pt=0,l=1,sg=0:1
                  send: 4-4-0-0 s=1,c=1,t=2,pt=0,l=1,sg=0,st=ok:1
                  Incoming change for sensor:1, New status: 1

                  What i dont know if why it works one way (command) but not the other (ack). I guess my GW radio is stronger, bigger or better powered.

                  Controller: Proxmox VM - Home Assistant
                  MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
                  MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
                  RFLink GW - Arduino Mega + RFLink Shield, 433mhz

                  1 Reply Last reply
                  0
                  • BartEB Offline
                    BartEB Offline
                    BartE
                    Contest Winner
                    wrote on last edited by
                    #15

                    @sundberg84 your right the ack should be enough

                    @martinhjelmare this should be enough, but when for some reason your network is down you'r not able to control the devices locally

                    1 Reply Last reply
                    0
                    • martinhjelmareM Offline
                      martinhjelmareM Offline
                      martinhjelmare
                      Plugin Developer
                      wrote on last edited by
                      #16

                      Yes, good point.

                      Maybe one could have a timeout event that compares button state with relay state, and after three failed comparisons, turns off the waiting for the acknowledge, as a sign of network failure.

                      1 Reply Last reply
                      0
                      • sundberg84S Offline
                        sundberg84S Offline
                        sundberg84
                        Hardware Contributor
                        wrote on last edited by
                        #17

                        @BartE, @martinhjelmare - this is sometime we should work with in the standard sketch.
                        I have one relay on the edge and sometimes its having really hard to get that ack back from the controller and it never changes. It would be good if the relay made the switch, send the on/off to controller and waited for the ack. If you dont recieve it you send again and tries for some times.

                        Controller: Proxmox VM - Home Assistant
                        MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
                        MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
                        RFLink GW - Arduino Mega + RFLink Shield, 433mhz

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        12

                        Online

                        11.7k

                        Users

                        11.2k

                        Topics

                        113.1k

                        Posts


                        Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • MySensors
                        • OpenHardware.io
                        • Categories
                        • Recent
                        • Tags
                        • Popular