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. Issue with Relay + Scene selector sketch

Issue with Relay + Scene selector sketch

Scheduled Pinned Locked Moved Troubleshooting
1 Posts 1 Posters 631 Views 1 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.
  • Cameron PayneC Offline
    Cameron PayneC Offline
    Cameron Payne
    wrote on last edited by
    #1

    Hey all ,
    Im having a strange issue, i combined the relay and scene sector sketch and everything works well the buttons trigger and the scene and i can control the relay. The only problem is that when i control the relay ( In home Assistant ) it also activates the scene :/
    I have attached my sketch and home assistant automation
    Any help would be greatly appreciated :D

    Sketch

    // Override Setting for Manual Node ID to 2
    #define MY_NODE_ID 15
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    
    // Enable repeater functionality for this node
    #define MY_REPEATER_FEATURE
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <Keypad.h>
    
    #define RELAY_1  3          // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 2  // Total number of attached relays: 4
    
    // Opto Relay Module I was using Active Low - Low (0):ON, High (1): OFF
    #define RELAY_ON 0          // GPIO value to write to turn on attached relay
    #define RELAY_OFF 1         // GPIO value to write to turn off attached relay
    #define SN "Scene Controller"
    #define SV "2.0"
    #define KEYPAD_CHILD_ID 95
    
    bool initialValueSent = false;
    
    //Init MyMessage for Each Child ID
    MyMessage msg1(1, V_LIGHT);
    MyMessage msg2(2, V_LIGHT);
    MyMessage scene(KEYPAD_CHILD_ID, V_SCENE_ON); //scene ON
    MyMessage scene2(KEYPAD_CHILD_ID, V_SCENE_OFF); //scene OFF
    
    const byte ROWS = 4; //four rows
    const byte COLS = 1; //one column
    char keys[ROWS][COLS] = {
      {'1'},
      {'2'},
      {'3'},
      {'4'}
    };
    
    byte rowPins[ROWS] = {A3, A4, A1, A2}; //connect to the row pinouts of the keypad
    byte colPins[COLS] = {A5}; //connect to the column pinouts of the keypad
    
    Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
    byte lastState;
    
    void before() { 
      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
        // Then set relay pins in output mode
        pinMode(pin, OUTPUT);   
        // Set relay to last known state (using eeprom storage) 
        digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
      }
    }
    
    void setup() {
      
    }
    
    void presentation()  
    {   
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Relay", "1.0");
    
      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
        // Register all sensors to gw (they will be created as child devices)
        present(sensor, S_LIGHT);
        present(KEYPAD_CHILD_ID, S_SCENE_CONTROLLER);
        keypad.addEventListener(keypadEvent);
      }
    }
    
    
    void loop() 
    {
      if (!initialValueSent) {
        Serial.println("Sending initial value");
        send(msg1.set(loadState(1)?RELAY_OFF:RELAY_ON),true);
        wait(1000);
        send(msg2.set(loadState(2)?RELAY_OFF:RELAY_ON),true);
        wait(1000);
        Serial.println("Sending initial value: Completed");
        wait(5000);
      }
      {
      char key = keypad.getKey();
    }
    }
    
    void receive(const MyMessage &message) {
      Serial.println("=============== Receive Start =======================");
      if (message.isAck()) {
         Serial.println(">>>>> ACK <<<<<");
         Serial.println("This is an ack from gateway");
         Serial.println("<<<<<< ACK >>>>>>");
      }
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_LIGHT) {
        Serial.println(">>>>> V_LIGHT <<<<<");
        if (!initialValueSent) {
          Serial.println("Receiving initial value from controller");
          initialValueSent = true;
        }
         // Update relay state to HA
         digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
         switch (message.sensor) {
            case 1:
              Serial.print("Incoming change for sensor 1");
              send(msg1.set(message.getBool()?RELAY_OFF:RELAY_ON));
              break;
            case 2:
              Serial.print("Incoming change for sensor 2");
              send(msg2.set(message.getBool()?RELAY_OFF:RELAY_ON));
              break;                   
            default: 
              Serial.println("Default Case: Receiving Other Sensor Child ID");
            break;
         }
         // Store state in Arduino eeprom
         saveState(message.sensor, message.getBool());
         Serial.print("Saved State for sensor: ");
         Serial.print( message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
         Serial.println("<<<<<< V_LIGHT >>>>>>");
       } 
       Serial.println("=============== Receive END =======================");
      
    }
    void keypadEvent(KeypadEvent key) {
      switch (keypad.getState()) {
    
        case PRESSED:
          lastState = 1;
          break;
    
        case HOLD:
          lastState = 2;
          break;
    
        case RELEASED:
          int keyInt = key - '0'; //Quick way to convert Char to Int so it can be sent to controller
          if (lastState == 2) {
            //keyInt = keyInt + 4; //If button is held, add 4.  If using more than 4 buttons this number will need to be changed
            send(scene2.set(keyInt));
          } else {
          send(scene.set(keyInt));
          }
          //break;
     }
    }```
    
    Home Assistant 
    
    

    automation:

    • alias: "scene selector"
      trigger:

      • platform: state
        entity_id: sensor.relay_15_95
        to: '1'
        action:
      • service: light.turn_off
        data:
        entity_id: light.cams_room_tv
    • alias: "scence selector2"
      trigger:

      • platform: state
        entity_id: sensor.relay_15_95_2
        to: '1'
        action:
      • service: light.turn_on
        data:
        entity_id: light.cams_room_tv
    1 Reply Last reply
    0

    Hello! It looks like you're interested in this conversation, but you don't have an account yet.

    Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

    With your input, this post could be even better 💗

    Register Login
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    27

    Online

    12.0k

    Users

    11.2k

    Topics

    113.4k

    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