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. Relay with button example sketch, works...but not as expected

Relay with button example sketch, works...but not as expected

Scheduled Pinned Locked Moved Troubleshooting
15 Posts 4 Posters 5.3k Views 2 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.
  • R Offline
    R Offline
    rvendrame
    Hero Member
    wrote on last edited by
    #4

    Try changing this line

    if (value != oldValue && value==0) {
    

    to this

    if (value != oldValue) {
    

    Home Assistant / Vera Plus UI7
    ESP8266 GW + mySensors 2.3.2
    Alexa / Google Home

    B 1 Reply Last reply
    0
    • B Offline
      B Offline
      BulldogLowell
      Contest Winner
      wrote on last edited by BulldogLowell
      #5

      @roffy said:

      gw.send(msg.set(state?false:true), true);

      also, are you aware that you are not changing the variable "state" with a button press?

      nor are you affecting the relay, fyi...

      void loop() 
      {
        gw.process();
        debouncer.update();
        // Get the update value
        int value = debouncer.read();
        if (value != oldValue && value==0)
        {
            state = !state;
            digitalWrite(RELAY_PIN, state);
            gw.send(msg.set(state, true)); // Send new state and request ack back
        }
        oldValue = value;
      } 
      
      1 Reply Last reply
      0
      • B Offline
        B Offline
        BulldogLowell
        Contest Winner
        wrote on last edited by
        #6

        @roffy said:

        gw.saveState(CHILD_ID, state);

        void loop() 
        {
          gw.process();
          debouncer.update();
          // Get the update value
          int value = debouncer.read();
          if (value != oldValue && value==0)
          {
              state = !state;
              digitalWrite(RELAY_PIN, state);
              gw.saveState(CHILD_ID, state);// forgot to save the new state to EEPROM
              gw.send(msg.set(state, true)); // Send new state and request ack back
          }
          oldValue = value;
        } 
        

        gw.saveState(CHILD_ID, state);

        1 Reply Last reply
        0
        • R rvendrame

          Try changing this line

          if (value != oldValue && value==0) {
          

          to this

          if (value != oldValue) {
          
          B Offline
          B Offline
          BulldogLowell
          Contest Winner
          wrote on last edited by
          #7

          @rvendrame said:

          Try changing this line

          if (value != oldValue && value==0) {
          

          to this

          if (value != oldValue) {
          

          I don't think you want to affect a state change on both sides of the button press....

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rvendrame
            Hero Member
            wrote on last edited by
            #8

            @BulldogLowell said:

            I don't think you want to affect a state change on both sides of the button press....

            ... if you are thinking in a pushbutton , yes I agree with you --- But what about a regular on-off switch?

            Home Assistant / Vera Plus UI7
            ESP8266 GW + mySensors 2.3.2
            Alexa / Google Home

            1 Reply Last reply
            0
            • B Offline
              B Offline
              BulldogLowell
              Contest Winner
              wrote on last edited by
              #9

              @rvendrame said:

              @BulldogLowell said:

              I don't think you want to affect a state change on both sides of the button press....

              ... if you are thinking in a pushbutton , yes I agree with you --- But what about a regular on-off switch?

              For an on/off switch? well you would probably not have that on a device that is also controlled remotely/wirelessly.

              1 Reply Last reply
              0
              • R Offline
                R Offline
                rvendrame
                Hero Member
                wrote on last edited by
                #10

                @BulldogLowell said:

                For an on/off switch? well you would probably not have that on a device that is also controlled remotely/wirelessly.

                Why not? I do have the original on/off switch from my lights for example. I want to control them via automation but also via wall switch...

                Home Assistant / Vera Plus UI7
                ESP8266 GW + mySensors 2.3.2
                Alexa / Google Home

                B 1 Reply Last reply
                0
                • R rvendrame

                  @BulldogLowell said:

                  For an on/off switch? well you would probably not have that on a device that is also controlled remotely/wirelessly.

                  Why not? I do have the original on/off switch from my lights for example. I want to control them via automation but also via wall switch...

                  B Offline
                  B Offline
                  BulldogLowell
                  Contest Winner
                  wrote on last edited by
                  #11

                  @rvendrame said:

                  @BulldogLowell said:

                  For an on/off switch? well you would probably not have that on a device that is also controlled remotely/wirelessly.

                  Why not? I do have the original on/off switch from my lights for example. I want to control them via automation but also via wall switch...

                  yeah, but every wall switch I have, which are integrated into my home automation, are not two position switches. Anyways, the OP has to chime in here... in the rare event he's using a two-position switch, well your code may still need to change. Time to hear from the OP.

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    rvendrame
                    Hero Member
                    wrote on last edited by
                    #12

                    @BulldogLowell said:

                    also, are you aware that you are not changing the variable "state" with a button press?

                    Just to mention, state is being changed:

                     if (value != oldValue && value==0)
                      {
                          state = !state;   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                          digitalWrite(RELAY_PIN, state);
                          gw.saveState(CHILD_ID, state);// forgot to save the new state to EEPROM
                          gw.send(msg.set(state, true)); // Send new state and request ack back
                      }
                    

                    Home Assistant / Vera Plus UI7
                    ESP8266 GW + mySensors 2.3.2
                    Alexa / Google Home

                    B 1 Reply Last reply
                    0
                    • R rvendrame

                      @BulldogLowell said:

                      also, are you aware that you are not changing the variable "state" with a button press?

                      Just to mention, state is being changed:

                       if (value != oldValue && value==0)
                        {
                            state = !state;   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                            digitalWrite(RELAY_PIN, state);
                            gw.saveState(CHILD_ID, state);// forgot to save the new state to EEPROM
                            gw.send(msg.set(state, true)); // Send new state and request ack back
                        }
                      
                      B Offline
                      B Offline
                      BulldogLowell
                      Contest Winner
                      wrote on last edited by
                      #13

                      @rvendrame

                      @rvendrame said:

                      Just to mention, state is being changed:

                      yes, I added that... it was missing from OP's code

                      1 Reply Last reply
                      0
                      • H Offline
                        H Offline
                        hek
                        Admin
                        wrote on last edited by
                        #14

                        Yes, as you are aware not the WithButton example was created to be used with a monostable button.

                        Haven't really thought about bi-stable but should be doable.

                        The state is actually not stored in EEPROM until ack message comes back from gateway. It is also only then the actual realy is flipped.

                        As you already mention above removing value==0 would send of a change when flipping button. Not sure if one should pick up oldValue from eeprom at startup as well.

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          roffy
                          wrote on last edited by
                          #15

                          wow thanks for all the suggestions guys! sorry for the slow reply, this is the first time iv actually been able to sit at my computer since i posted, if only we didnt have to work to pay bills id be here all day...ahhh if only..... anyway!

                          Yes i am using a pushbutton not a rocker switch so i would need it to only do something on the down press and ignore the upward contact on the release of the button.

                          Ill try changing the script as explained above and see what that does, (as you have probably guessed by now my arduino tinkering only covered the first 10 or so lessons in the basic tutorial, then i went off and only looked up specific coding for the small projects i was working on....and most of which i have forgotten as i haven't used them for a long time :( need to brush up on what i should already know i guess)

                          anyway i shall post results as soon as i try out the suggestions. i think you all get what im trying to achieve here! thanks again,

                          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


                          15

                          Online

                          12.1k

                          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