relay as a switch not as a button (domoticz)

  • Hi,
    I need help for the 'relay Switch' code.
    you can help with this code, I would like to use relay as a switch not as a button .
    (I'm Italian, I'm sorry for the bad English).
    thanks to you.

  • @ago1980 Help us understand your question a little better. I am not sure what you mean by use the relay as a switch. What kind of switch?

  • @dbemowsk hello I mean switches from classics from home. Thanks

  • Hero Member

    Hi @ago1980 Is it the RelayWithButtonActuator sketch you are looking to modify so it can be used with a toggle switch instead of a push button?

  • @Boots33 I would like to use with switch, I find nothing on.

  • Hero Member

    @ago1980 You can find the relay with button sketch Here

    That sketch uses a push button, to use that sketch with a toggle switch you should just need to make a couple of small changes in the loop part of the sketch.

    void loop() 
      // Get the update value
      int value =;
      if (value != oldValue && value==0) {
          send(msg.set(state?false:true), true); // Send new state and request ack back
      oldValue = value;
    if (value != oldValue) {
          send(msg.set(state?false:true), true); // Send new state and request ack back
          oldValue = value;

  • @Boots33 ok, but then the status is updated Domoticz?
    thank you

  • Hero Member

    @ago1980 Yes it should still update the status in domoticz when you use the switch.

  • @Boots33 ok, tomorrow I try and let me know. Thanks so much

  • @dbemowsk ok thanks clear code explanation, but I did not understand well what to do to hardware part.

  • @ago1980 So in the diagram below I show a wall switch and the push button switch that is normally used in this example. Simply remove the push button and attach the wall switch.

    Does this clear up the confusion?

  • @dbemowsk So you just have to remove the button and put the switch on? or put the switch in parallel with the button?Thank you

  • @ago1980 Yes. If you use the switch, you will need to use the code changes that I pointed out in the other thread.

  • @ago1980 With the switch in place you don't want to run it in parallel with the button. When the switch is in the on position, the connection will be shorted in which case the push button would not work. This is the reason for the code change. It is to detect when the switch changed from off to on, or on to off.

  • I have a problem when the radio nrf24l01 loses contact with the gateway does not work the node, how can I do? thank you

  • @ago1980 DO you have a capacitor on the nRF radio? How far away from the gateway is the node? Is there anything in between the gateway and the node such as windows or walls? If it is really far from the gateway, you will probably need a repeater node in between.

  • @dbemowsk I mean if GATEWAY does not work for some problem the node does not work does not turn on light

  • @ago1980 Are you sure the problem is with the gateway? Can you post pictures of your node and your gateway? Having those will allow us to help you better.

  • Hero Member

    @ago1980 said in relay as a switch not as a button (domoticz):

    I mean if GATEWAY does not work for some problem the node does not work does not turn on light

    The original sketch relies on an ack from the gateway to trigger the relay, so if the gateway is not available the relay state cannot be changed by the local switch or even the controller.

    Under most circumstances it would be desirable to have the local switch function no matter what the state of the network is. To do this you will need to modify the sketch so it no longer relies on the ack to make the change.

    In MySensors 2.1.1 by default a node will not boot through to the loop section of your sketch if it cannot find the gateway, so the first thing you need to do is force it to move on.

    The line shown below will do just that. The number at the end is how long you want the node to wait for an uplink to be established before it will move on to the rest of your sketch. It is in milliseconds, so in the example below it will wait 5 seconds. This line needs to be inserted near the top of your sketch before the #include <MySensors.h> line.


    After adding that you will then need to change the sketch so it no longer relies on the ack to change the relay state. that is pretty straight forward and once done your relay will then be able to be switched by the local switch no mater what the uplink status is.

    The next problem you will encounter is trying to keep your controller in sync with the local node. As you can now change the state of the relay without a connection to the controller it may loose its sync to the node. So you may find your controller thinks the relay is on when it is actually off. This is where it gets a little trickier so if it is important to you that the controller stays in sync you may need to experiment a bit to find what will give you the best results.

    I have some examples of relay nodes that explore ways to switch locally and try to maintain sync with the controller.
    The last sketch I posted there uses requestTime() to check to see if the controller is available and though there is a trade off in how quickly you can flick the switch back and forward it at least confirms that the controller is available. Other methods may only check for the gateways presence.

    Have a look you may find something of use.

    Synchronising Light switch

    Its a bit of an old post but some of these issues (using a toggle switch and making the switch always work) were also discussed in this post