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. My Project
  3. 2x Relay, 1x Reed Switch on 1 node for Home Assistant

2x Relay, 1x Reed Switch on 1 node for Home Assistant

Scheduled Pinned Locked Moved My Project
2 Posts 2 Posters 1.9k 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!
    N00b ahead warning!

    I had a lot of trouble trying to get 2 relays and a reed switch working on one node in Home Assistant, but after hours of trying i finally got some code that works

    It's very messy and is a big mash of sample code and other projects code but it works.

    Would be nice if it would remember the state after a power off but that is way beyond me

    If anyone ends up using this and cleans it up feel free to post it back here :P

    Thanks!

    /*
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * http://www.mysensors.org/build/relay
     */
    
    #define MY_DEBUG
    #define MY_RADIO_NRF24
    #define MY_REPEATER_FEATURE
    #define MY_NODE_ID 43
    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h>
    
    #define RELAY_PIN1  3
    #define RELAY_PIN2  4
    #define BUTTON_PIN1  6 
    #define CHILD_ID1 1
    #define CHILD_ID2 2
    #define CHILD_ID3 3
    #define RELAY_ON 1
    #define RELAY_OFF 0
     
    
    Bounce debouncer = Bounce();
    bool state = false;
    bool state2 = false;
    bool initialValueSent = false;
    int oldValue=-1;
    
    MyMessage msg(CHILD_ID1, V_STATUS);
    MyMessage msg2(CHILD_ID2, V_STATUS);
    MyMessage msg3(CHILD_ID3,V_TRIPPED);
    
    void setup()
    {
    
      // Make sure relays are off when starting up
      digitalWrite(RELAY_PIN1, RELAY_OFF);
      digitalWrite(RELAY_PIN2, RELAY_OFF);
      pinMode(RELAY_PIN1, OUTPUT);
      pinMode(RELAY_PIN2, OUTPUT);
      pinMode(BUTTON_PIN1,INPUT);
      
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN1,HIGH);
    
      // After setting up the button, setup debouncer
      debouncer.attach(BUTTON_PIN1);
      debouncer.interval(5);
    }
    
    void presentation()  {
      sendSketchInfo("Relay+Reed", "1.0");
      wait(100);
      present(CHILD_ID1, S_BINARY);
      wait(100);
      present(CHILD_ID2, S_BINARY);
      wait(100);
      present(CHILD_ID3, S_DOOR); 
    }
    
    void loop()
    {
      if (!initialValueSent) {
        Serial.println("Sending initial value");
        send(msg.set(state?RELAY_ON:RELAY_OFF));
        send(msg2.set(state2?RELAY_ON:RELAY_OFF));
        Serial.println("Requesting initial value from controller");
        request(CHILD_ID1, V_STATUS);
        wait(2000, C_SET, V_STATUS);
        request(CHILD_ID2, V_STATUS);
        wait(2000, C_SET, V_STATUS);
      }
      if (debouncer.update()) {
        if (debouncer.read()==LOW) {
          state = !state;
      //    // Send new state and request ack back
       //   send(msg.set(state?RELAY_ON:RELAY_OFF), true);
        }
        {
      debouncer.update();
      // Get the update value
      int value = debouncer.read();
    
      if (value != oldValue) {
         // Send in the new value
         send(msg3.set(value==HIGH ? 1 : 0));
         oldValue = value;
      }
      }
    }
    
    }
    
    void receive(const MyMessage &message) {
      if (message.isAck()) {
         Serial.println("This is an ack from gateway");
      }
    
      if (message.type == V_STATUS && message.sensor ==1) {
        if (!initialValueSent) {
          Serial.println("Receiving initial value from controller");
          initialValueSent = true;
        }
        // Change relay state
        state = (bool)message.getInt();
        digitalWrite(RELAY_PIN1, state?RELAY_OFF:RELAY_ON);
        send(msg.set(state?RELAY_ON:RELAY_OFF));
      }
       
      if (message.type == V_STATUS && message.sensor ==2) {
        if (!initialValueSent) {
          Serial.println("Receiving initial value from controller");
          initialValueSent = true;
        }
        // Change relay state
        state2 = (bool)message.getInt();
        digitalWrite(RELAY_PIN2, state2?RELAY_OFF:RELAY_ON);
        send(msg2.set(state2?RELAY_ON:RELAY_OFF));
      }
    }
    
    1 Reply Last reply
    1
    • S Offline
      S Offline
      scalz
      Hardware Contributor
      wrote on last edited by scalz
      #2

      @Cameron-Payne
      Hello and welcome!
      Great to hear your success :)
      About saving the state you shoud look at this ;) https://www.mysensors.org/download/sensor_api_20#saving-state And there are more infos about the API.
      Shortly save your state when it change, and load it in your setup.

      1 Reply Last reply
      2

      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


      3

      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