Help work out correct MQTT message to activate relay

  • I'm trying to active a relay but not sure exactly what the full message should be.

    This is what I'm trying...


    In the serial monitor I can see "read" messages coming in but the relay is not doing anything.

    This is what I see in the serial monitor when I boot up the relay, maybe someone can help me make sense of it?

    send: 24-24-0-0 s=255,c=3,t=11,pt=0,l=5,st=ok:Relay
    send: 24-24-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
    send: 24-24-0-0 s=1,c=0,t=3,pt=0,l=5,st=ok:1.4.1

  • Can you please post your GW sketch ?

    Check this also.

    Another note regarding the above link. Some replies are talking about a bug which was fixed. But in all cases check the first replies concerning setting the message structure.

  • As far as I know I am just using the default MySensors relay sketch with 1 relay.

    // Example sketch showing how to control physical relays. 
    // This example will remember relay state even after power failure.
    #include <MySensor.h>
    #include <SPI.h>
    #define RELAY_1  6  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 1 // Total number of attached relays
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    MySensor gw;
    void setup()  
      // Initialize library and add callback for incoming messages
      gw.begin(incomingMessage, AUTO, true);
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Relay 1.0");
      // Fetch relay status
      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)
        gw.present(sensor, S_LIGHT);
        // Then set relay pins in output mode
        pinMode(pin, OUTPUT);   
        // Set relay to last known state (using eeprom storage) 
        digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
    void loop() 
      // Alway process incoming messages whenever possible
    void incomingMessage(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_LIGHT) {
         // Change relay state
         digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         gw.saveState(message.sensor, message.getBool());
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(", New status: ");

  • @ahmedadelhosni

    Ahhh by GW sketch I guess you mean gateway sketch? I set that up well over a year ago and I actually wouldn't even have a clue where the original sketch is... My temp/humidity sensors have been working perfectly for over a year now. So just diving back into the arduino stuff to see if I can get the relay going.

    I'm even using a different PC to compile and upload the sketches so I might not even have the original GW sketch....

  • @TommySharp Yeah I mean the gateway.

    You say your gateway is running for a year now, but now you want to use MQTT so you have to set this MQTT gateway. I am not familiar with MYsensor library 1.5 or previous versions. I am using the development branch ( )
    It has a sketch for setting MQTT gateway and you can test sending a payload data using mosquitto as explained in my first link in the previous reply. Also there is a new sketch for relayactuator example.

    It works okay and I have tested it.

  • @ahmedadelhosni
    Yes my gateway is the MQTT gateway and it has been running for well over a year. It happily posts temp/humidity which my OpenHAB server subscribes to and reads in the sensor readings.

    I just need to work out now how to activate a relay 🙂

  • Plugin Developer



    According to the mqtt gateway sketch in 1.5.3, topic structure is:


    Mysensors payload is mqtt payload.

  • So when my relay sensor announces itself to the gateway below.... Does it announce the child ID anywhere so I can be sure I'm using the right one?

    send: 24-24-0-0 s=255,c=3,t=11,pt=0,l=5,st=ok:Relay
    send: 24-24-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
    send: 24-24-0-0 s=1,c=0,t=3,pt=0,l=5,st=ok:1.4.1

  • Plugin Developer

    The last line shows s=1, so child id is 1.

    Read this post for details on topic structure:

    Also read the text at the top of the mqtt gateway sketch for more info.

    I think you should test with this topic:

    Replace prefix with your set topic prefix.

    You can set payload with the -m option if you're using mosquitto.

  • Plugin Developer

    Be aware though, that the mqtt broker gateway sketch will me removed in the next release, so if you want the best support I would think about changing to the mqtt client gateway. This is currently only available in the dev branch at github, but there is a sticky guide in the forum, on how to set it up.

  • Awesome! I'd much rather use a client gateway so might just hold off until the next release of MySensors and then use the MQTT Client Gateway.