Needing help with Node red work flow for creating a on off switch with MQTT

Good day I am in need of help so I have made a work flow in Node Red that works well bringing in the data from a Milesight UC300 but I am struggling to get the commands out to work with the digital outputs(gpio_output_1;gpio_output_2).
I apologize in advance with all the code but I am in need of help

Commands
on = {“confirmed”:true,“fport”:85,“data”:“BwEA/w==”}
off = {“confirmed”:true,“fport”:85,“data”:“BwAA/w==”}

these commands need to be sent to a mqtt out node

This is the function node for Process Payload

let payload = msg.payload;

let device_id = payload.devEUI;
let device_name = payload.deviceName;
let gpio_input_1 = payload.gpio_input_1;
let gpio_input_2 = payload.gpio_input_2;
let gpio_input_3 = payload.gpio_input_3;
let gpio_input_4 = payload.gpio_input_4;
let gpio_output_1 = payload.gpio_output_1;
let gpio_output_2 = payload.gpio_output_2;
let modbus_chn_1 = payload.modbus_chn_1;
let modbus_chn_2 = payload.modbus_chn_2;
let modbus_chn_3 = payload.modbus_chn_3;
let modbus_chn_4 = payload.modbus_chn_4;
let modbus_chn_5 = payload.modbus_chn_5;
let modbus_chn_6 = payload.modbus_chn_6;
let modbus_chn_7 = payload.modbus_chn_7;
let modbus_chn_8 = payload.modbus_chn_8;
let modbus_chn_9 = payload.modbus_chn_9;
let modbus_chn_10 = payload.modbus_chn_10;
let modbus_chn_11 = payload.modbus_chn_11;
let modbus_chn_12 = payload.modbus_chn_12;
let modbus_chn_13 = payload.modbus_chn_13;
let modbus_chn_14 = payload.modbus_chn_14;
let modbus_chn_15 = payload.modbus_chn_15;
let modbus_chn_16 = payload.modbus_chn_16;
let modbus_chn_17 = payload.modbus_chn_17;
let modbus_chn_18 = payload.modbus_chn_18;
let modbus_chn_19 = payload.modbus_chn_19;
let modbus_chn_20 = payload.modbus_chn_20;
let modbus_chn_21 = payload.modbus_chn_21;
let modbus_chn_22 = payload.modbus_chn_22;
let modbus_chn_23 = payload.modbus_chn_23;
let modbus_chn_24 = payload.modbus_chn_24;
let modbus_chn_25 = payload.modbus_chn_25;
let modbus_chn_26 = payload.modbus_chn_26;
let modbus_chn_27 = payload.modbus_chn_27;
let modbus_chn_28 = payload.modbus_chn_28;
let modbus_chn_29 = payload.modbus_chn_29;
let modbus_chn_30 = payload.modbus_chn_30;
let modbus_chn_31 = payload.modbus_chn_31;
let modbus_chn_32 = payload.modbus_chn_32;
let adc_1 = payload.adc_1;
let adc_2 = payload.adc_2;


msg.payload = {
    device_id: device_id,
    device_name: device_name,
    gpio_input_1: payload.gpio_input_1,
    gpio_input_2: payload.gpio_input_2,
    gpio_input_3: payload.gpio_input_3,
    gpio_input_4: payload.gpio_input_4,
    gpio_output_1: payload.gpio_output_1,
    gpio_output_2: payload.gpio_output_2,
    modbus_chn_1: payload.modbus_chn_1,
    modbus_chn_2: payload.modbus_chn_2,
    modbus_chn_3: payload.modbus_chn_3,
    modbus_chn_4: payload.modbus_chn_4,
    modbus_chn_5: payload.modbus_chn_5,
    modbus_chn_6: payload.modbus_chn_6,
    modbus_chn_7: payload.modbus_chn_7,
    modbus_chn_8: payload.modbus_chn_8,
    modbus_chn_9: payload.modbus_chn_9,
    modbus_chn_10: payload.modbus_chn_10,
    modbus_chn_11: payload.modbus_chn_11,
    modbus_chn_12: payload.modbus_chn_12,
    modbus_chn_13: payload.modbus_chn_13,
    modbus_chn_14: payload.modbus_chn_14,
    modbus_chn_15: payload.modbus_chn_15,
    modbus_chn_16: payload.modbus_chn_16,
    modbus_chn_17: payload.modbus_chn_17,
    modbus_chn_18: payload.modbus_chn_18,
    modbus_chn_19: payload.modbus_chn_19,
    modbus_chn_20: payload.modbus_chn_20,
    modbus_chn_21: payload.modbus_chn_21,
    modbus_chn_22: payload.modbus_chn_22,
    modbus_chn_23: payload.modbus_chn_23,
    modbus_chn_24: payload.modbus_chn_24,
    modbus_chn_25: payload.modbus_chn_25,
    modbus_chn_26: payload.modbus_chn_26,
    modbus_chn_27: payload.modbus_chn_27,
    modbus_chn_28: payload.modbus_chn_28,
    modbus_chn_29: payload.modbus_chn_29,
    modbus_chn_30: payload.modbus_chn_30,
    modbus_chn_31: payload.modbus_chn_31,
    modbus_chn_32: payload.modbus_chn_32,
    adc_1: payload.adc_1,
    adc_2: payload.adc_2,

};

return msg;

This is the function node for Discovery Payload

let device_id = msg.payload.device_id;
let device_name = msg.payload.device_name;
let gpio_output_1 = msg.payload.gpio_output_1
let sensor_type = "gpio_output_1";

// Define the discovery topic
let discovery_topic = `homeassistant/switch/UC300/${device_id}_${sensor_type}/config`;

// Define the discovery payload
let discovery_payload = {
    name: `${device_name} GPIO Output 1`,
    command_topic: `homeassistant/milesight/downlink/24e124445d016017`,
    state_topic: `homeassistant/switch/UC300/${device_id}/state`,
    unique_id: `${device_id}_${sensor_type}`,
    device: {
        identifiers: [device_id],
        name: device_name,
        model: "Milesight Sensor",
        manufacturer: "Milesight"
    },
    // Define the payloads as JSON strings
    payload_on: `1`,
    payload_off: `0`,
    value_template: "{{ value_json.gpio_output_1 }}",
};

msg.topic = discovery_topic;
msg.payload = JSON.stringify(discovery_payload);
msg.retain = true;

return msg;

This is the function node for Command payload

let device_id = msg.payload.device_id;
let gpio_output_1 = msg.payload.gpio_output_1;  // "1" for on, "0" for off

let payload;

// Check if gpio_output_2 is defined and process the command
if (gpio_output_1 !== undefined) {
    if (gpio_output_1 === 1) {
        // Command to turn on GPIO output 2
        payload = Buffer.from([0x07, 0x01, 0x00, 0xff]);
    } else if (gpio_output_1 === 0) {
        // Command to turn off GPIO output 2
        payload = Buffer.from([0x07, 0x00, 0x00, 0xff]);
    } else {
        // If the command is not recognized, return without doing anything
        node.warn("Unknown command received: " + JSON.stringify(gpio_output_1));
        return null;
    }
    
    // Set the MQTT topic and payload
    msg.topic = `homeassistant/milesight/downlink/24e124445d016017`;
    msg.payload = payload;

    return msg;
} else {
    node.warn("GPIO Output command is undefined");
    return null;
}

This is the function node for publish payload

let device_id = msg.payload.device_id;
let device_name = msg.payload.device_name;
let gpio_output_1 = msg.payload.gpio_output_1;
let gpio_output_2 = msg.payload.gpio_output_2;

msg.topic = `homeassistant/switch/UC300/${device_id}/state`;
msg.payload = JSON.stringify({
    device_name: device_name,
    gpio_output_1: gpio_output_1,
    gpio_output_2: gpio_output_2,
});

return msg;

Could you expand on what the problem is? You send the command, it doesn’t work? Is there any error?

Hi @Mikefila ,
So the first problem I am having when I click the switch in home assistant it does not go into the opposite state which is one of the issues.
And with that happening I think that is preventing the command to be pushed to the mqtt topic.
is there a different Node Red node that should be used for on off switches in home assistant to work?
I will try create just the node red work flow of just the nodes I have for the switch command if that will help figure out where I have gone wrong.

Here is the exported Node Red work flow

[{"id":"eb86f5a92d01a413","type":"tab","label":"UC300 switch flow","disabled":false,"info":"","env":[]},{"id":"9e0b292cbd92e1ee","type":"mqtt in","z":"eb86f5a92d01a413","name":"","topic":"homeassistant/milesight/uplink","qos":"0","datatype":"json","broker":"024346bb511cf5e0","nl":false,"rap":true,"rh":0,"inputs":0,"x":200,"y":40,"wires":[["e4b8decee4e16ef9","22338d4d2d8a0e8f"]]},{"id":"e4b8decee4e16ef9","type":"debug","z":"eb86f5a92d01a413","name":"debug 125","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":40,"wires":[]},{"id":"e898bba3c696720b","type":"debug","z":"eb86f5a92d01a413","name":"debug 126","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1210,"y":480,"wires":[]},{"id":"5a4734ddbca3f263","type":"debug","z":"eb86f5a92d01a413","name":"debug 128","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":120,"wires":[]},{"id":"22338d4d2d8a0e8f","type":"function","z":"eb86f5a92d01a413","name":"Process Payload","func":"let payload = msg.payload;\n\nlet device_id = payload.devEUI;\nlet device_name = payload.deviceName;\nlet gpio_output_1 = payload.gpio_output_1;\nlet gpio_output_2 = payload.gpio_output_2;\n\nmsg.payload = {\n    device_id: device_id,\n    device_name: device_name,\n    gpio_output_1: payload.gpio_output_1,\n    gpio_output_2: payload.gpio_output_2,\n\n};\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":230,"y":120,"wires":[["5a4734ddbca3f263","718b4288ffe587f1"]]},{"id":"bb4d8e46dae2a80b","type":"function","z":"eb86f5a92d01a413","name":"Create Discovery Payload gpio_output_1","func":"let device_id = msg.payload.device_id;\nlet device_name = msg.payload.device_name;\nlet gpio_output_1 = msg.payload.gpio_output_1\nlet sensor_type = \"gpio_output_1\";\n\n// Define the discovery topic\nlet discovery_topic = `homeassistant/switch/UC300/${device_id}_${sensor_type}/config`;\n\n// Define the discovery payload\nlet discovery_payload = {\n    name: `${device_name} GPIO Output 1`,\n    command_topic: `homeassistant/milesight/downlink/24e124445d016017`,\n    state_topic: `homeassistant/switch/UC300/${device_id}/state`,\n    unique_id: `${device_id}_${sensor_type}`,\n    device: {\n        identifiers: [device_id],\n        name: device_name,\n        model: \"Milesight Sensor\",\n        manufacturer: \"Milesight\"\n    },\n    // Define the payloads as JSON strings\n    payload_on: `1`,\n    payload_off: `0`,\n    value_template: \"{{ value_json.gpio_output_1 }}\",\n};\n\nmsg.topic = discovery_topic;\nmsg.payload = JSON.stringify(discovery_payload);\nmsg.retain = true;\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":440,"wires":[["e898bba3c696720b","05396752e27c1eaf"]]},{"id":"71f3e3101ca33c8c","type":"function","z":"eb86f5a92d01a413","name":"Create Discovery Payload gpio_output_2","func":"let device_id = msg.payload.device_id;\nlet device_name = msg.payload.device_name;\nlet gpio_output_2 = msg.payload.gpio_output_2;\nlet sensor_type = \"gpio_output_2\";\n\n// Define the discovery topic\nlet discovery_topic = `homeassistant/switch/UC300/${device_id}_${sensor_type}/config`;\n\n// Define the discovery payload\nlet discovery_payload = {\n    name: `${device_name} GPIO Output 2`,\n    command_topic: `homeassistant/milesight/downlink/24e124445d016017`,\n    state_topic: `homeassistant/switch/UC300/${device_id}/state`,\n    unique_id: `${device_id}_${sensor_type}`,\n    device: {\n        identifiers: [device_id],\n        name: device_name,\n        model: \"Milesight Sensor\",\n        manufacturer: \"Milesight\"\n    },\n    // Define the payloads as JSON strings\n    payload_on: `1`,\n    payload_off: `0`,\n    value_template: \"{{ value_json.gpio_output_2 }}\",\n    optimistic: false,\n    qos: 0\n};\n\nmsg.topic = discovery_topic;\nmsg.payload = JSON.stringify(discovery_payload);\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":480,"wires":[["e898bba3c696720b","05396752e27c1eaf"]]},{"id":"718b4288ffe587f1","type":"switch","z":"eb86f5a92d01a413","name":"","property":"payload.device_id","propertyType":"msg","rules":[{"t":"eq","v":"24e124445d016017","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":230,"y":320,"wires":[["bb4d8e46dae2a80b","71f3e3101ca33c8c","36a6fd223f578b28","f27043b937d644f1","8f6ae93a7892059d","51a22ab4f24c93b8"]]},{"id":"36a6fd223f578b28","type":"debug","z":"eb86f5a92d01a413","name":"debug 129","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":210,"y":440,"wires":[]},{"id":"f27043b937d644f1","type":"function","z":"eb86f5a92d01a413","name":"Command Digital Output 1","func":"let device_id = msg.payload.device_id;\nlet gpio_output_1 = msg.payload.gpio_output_1;  // \"1\" for on, \"0\" for off\n\nlet payload;\n\n// Check if gpio_output_2 is defined and process the command\nif (gpio_output_1 !== undefined) {\n    if (gpio_output_1 === 1) {\n        // Command to turn on GPIO output 2\n        payload = Buffer.from([0x07, 0x01, 0x00, 0xff]);\n    } else if (gpio_output_1 === 0) {\n        // Command to turn off GPIO output 2\n        payload = Buffer.from([0x07, 0x00, 0x00, 0xff]);\n    } else {\n        // If the command is not recognized, return without doing anything\n        node.warn(\"Unknown command received: \" + JSON.stringify(gpio_output_1));\n        return null;\n    }\n    \n    // Set the MQTT topic and payload\n    msg.topic = `homeassistant/milesight/downlink/24e124445d016017`;\n    msg.payload = payload;\n\n    return msg;\n} else {\n    node.warn(\"GPIO Output command is undefined\");\n    return null;\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":320,"wires":[["7246de110284dc61","7aecdd2da72429c4"]]},{"id":"8f6ae93a7892059d","type":"function","z":"eb86f5a92d01a413","name":"Command Digital Output 2","func":"let device_id = msg.payload.device_id;\nlet gpio_output_2 = msg.payload.gpio_output_2;  // \"1\" for on, \"0\" for off\n\nlet payload;\n\n// Check if gpio_output_2 is defined and process the command\nif (gpio_output_2 !== undefined) {\n    if (gpio_output_2 === 1) {\n        // Command to turn on GPIO output 2\n        payload = Buffer.from([0x07, 0x01, 0x00, 0xff]);\n    } else if (gpio_output_2 === 0) {\n        // Command to turn off GPIO output 2\n        payload = Buffer.from([0x07, 0x00, 0x00, 0xff]);\n    } else {\n        // If the command is not recognized, return without doing anything\n        node.warn(\"Unknown command received: \" + JSON.stringify(gpio_output_2));\n        return null;\n    }\n\n    // Set the MQTT topic and payload\n    msg.topic = `homeassistant/milesight/downlink/24e124445d016017`;\n    msg.payload = payload;\n\n    return msg;\n} else {\n    node.warn(\"GPIO Output command is undefined\");\n    return null;\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":360,"wires":[["7246de110284dc61","7aecdd2da72429c4"]]},{"id":"7246de110284dc61","type":"mqtt out","z":"eb86f5a92d01a413","name":"","topic":"homeassistant/milesight/downlink/$deveui","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"024346bb511cf5e0","x":1300,"y":360,"wires":[]},{"id":"7aecdd2da72429c4","type":"debug","z":"eb86f5a92d01a413","name":"debug 130","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1210,"y":320,"wires":[]},{"id":"51a22ab4f24c93b8","type":"function","z":"eb86f5a92d01a413","name":"Publish Switch Data","func":"let device_id = msg.payload.device_id;\nlet device_name = msg.payload.device_name;\nlet gpio_output_1 = msg.payload.gpio_output_1;\nlet gpio_output_2 = msg.payload.gpio_output_2;\n\nmsg.topic = `homeassistant/switch/UC300/${device_id}/state`;\nmsg.payload = JSON.stringify({\n    device_name: device_name,\n    gpio_output_1: gpio_output_1,\n    gpio_output_2: gpio_output_2,\n});\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":240,"wires":[["aa1a322e3924726f","3dc2462b4bdabe78"]]},{"id":"aa1a322e3924726f","type":"mqtt out","z":"eb86f5a92d01a413","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"024346bb511cf5e0","x":1190,"y":260,"wires":[]},{"id":"3dc2462b4bdabe78","type":"debug","z":"eb86f5a92d01a413","name":"debug 131","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1210,"y":220,"wires":[]},{"id":"05396752e27c1eaf","type":"mqtt out","z":"eb86f5a92d01a413","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"024346bb511cf5e0","x":1190,"y":440,"wires":[]},{"id":"024346bb511cf5e0","type":"mqtt-broker","name":"","broker":"192.168.100.246","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"autoUnsubscribe":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willRetain":"false","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]

here is a screan shot of how the work flow looks like.

If you use an inject with the payload for the gpio commands directly to an mqtt node, does it work? In other words do not send it through the func, hard code the message format inside the inject.

Once you get a working message then you can tune the functions to reproduce it. There is a standalone program called mqtt explorer. This will let you see the messages as they are sent over mqtt.

so i am trying to get it correct with the injector first but for some reason it does not want to replace the on and off with
on = {“confirmed”:true,“fport”:85,“data”:“BwEA/w==”}
off = {“confirmed”:true,“fport”:85,“data”:“BwAA/w==”} I have tried so many things to try get this switch working but no luck.

Could you post the link to what you are trying to connect to?

appologies if i have miss read this do you want to know what device i am trying to make the work flow for?
https://github.com/Milesight-IoT/SensorDecoders/tree/main/UC_Series/UC300

Here is the github page for the device I am working with.

so the thing I am struggling with is. The work flow i have created, has made toggle button in home assistant but when i click on them nothing happens it moves when you click it and then just goes back to where it was. and when i change the state on the gate way home assistant pick up that it has changed.

here is the work flow again with

[{"id":"eb86f5a92d01a413","type":"tab","label":"UC300 switch flow","disabled":false,"info":"","env":[]},{"id":"9e0b292cbd92e1ee","type":"mqtt in","z":"eb86f5a92d01a413","name":"","topic":"homeassistant/milesight/uplink","qos":"0","datatype":"json","broker":"024346bb511cf5e0","nl":false,"rap":true,"rh":0,"inputs":0,"x":200,"y":40,"wires":[["e4b8decee4e16ef9","22338d4d2d8a0e8f"]]},{"id":"e4b8decee4e16ef9","type":"debug","z":"eb86f5a92d01a413","name":"debug 125","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":40,"wires":[]},{"id":"e898bba3c696720b","type":"debug","z":"eb86f5a92d01a413","name":"debug 126","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1210,"y":440,"wires":[]},{"id":"5a4734ddbca3f263","type":"debug","z":"eb86f5a92d01a413","name":"debug 128","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":120,"wires":[]},{"id":"22338d4d2d8a0e8f","type":"function","z":"eb86f5a92d01a413","name":"Process Payload","func":"let payload = msg.payload;\n\nlet device_id = payload.devEUI;\nlet device_name = payload.deviceName;\nlet gpio_output_1 = payload.gpio_output_1;\nlet gpio_output_2 = payload.gpio_output_2;\n\nmsg.payload = {\n    device_id: device_id,\n    device_name: device_name,\n    gpio_output_1: payload.gpio_output_1,\n    gpio_output_2: payload.gpio_output_2,\n\n};\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":230,"y":120,"wires":[["5a4734ddbca3f263","718b4288ffe587f1"]]},{"id":"bb4d8e46dae2a80b","type":"function","z":"eb86f5a92d01a413","name":"Create Discovery Payload gpio_output_1","func":"let device_id = msg.payload.device_id;\nlet device_name = msg.payload.device_name;\nlet gpio_output_1 = msg.payload.gpio_output_1;\nlet sensor_type = \"gpio_output_1\";\n\n// Define the discovery topic\nlet discovery_topic = `homeassistant/switch/UC300/${device_id}_${sensor_type}/config`;\n\n// Define the discovery payload\nlet discovery_payload = {\n    name: `${device_name} GPIO Output 1`,\n    command_topic: `homeassistant/milesight/downlink/24e124445d016017`,\n    state_topic: `homeassistant/switch/UC300/${device_id}/state`,\n    unique_id: `${device_id}_${sensor_type}`,\n    device: {\n        identifiers: [device_id],\n        name: device_name,\n        model: \"Milesight Sensor\",\n        manufacturer: \"Milesight\"\n    },\n    // Define the payloads as JSON strings\n    payload_on: `1`,\n    payload_off: `0`,\n    value_template: \"{{ value_json.gpio_output_1 }}\",\n    optimistic: false,\n    qos: 0\n};\n\nmsg.topic = discovery_topic;\nmsg.payload = JSON.stringify(discovery_payload);\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":440,"wires":[["e898bba3c696720b","05396752e27c1eaf"]]},{"id":"71f3e3101ca33c8c","type":"function","z":"eb86f5a92d01a413","name":"Create Discovery Payload gpio_output_2","func":"let device_id = msg.payload.device_id;\nlet device_name = msg.payload.device_name;\nlet gpio_output_2 = msg.payload.gpio_output_2;\nlet sensor_type = \"gpio_output_2\";\n\n// Define the discovery topic\nlet discovery_topic = `homeassistant/switch/UC300/${device_id}_${sensor_type}/config`;\n\n// Define the discovery payload\nlet discovery_payload = {\n    name: `${device_name} GPIO Output 2`,\n    command_topic: `homeassistant/milesight/downlink/24e124445d016017`,\n    state_topic: `homeassistant/switch/UC300/${device_id}/state`,\n    unique_id: `${device_id}_${sensor_type}`,\n    device: {\n        identifiers: [device_id],\n        name: device_name,\n        model: \"Milesight Sensor\",\n        manufacturer: \"Milesight\"\n    },\n    // Define the payloads as JSON strings\n    payload_on: `1`,\n    payload_off: `0`,\n    value_template: \"{{ value_json.gpio_output_2 }}\",\n    optimistic: false,\n    qos: 0\n};\n\nmsg.topic = discovery_topic;\nmsg.payload = JSON.stringify(discovery_payload);\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":480,"wires":[["e898bba3c696720b","05396752e27c1eaf"]]},{"id":"718b4288ffe587f1","type":"switch","z":"eb86f5a92d01a413","name":"","property":"payload.device_id","propertyType":"msg","rules":[{"t":"eq","v":"24e124445d016017","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":230,"y":320,"wires":[["bb4d8e46dae2a80b","71f3e3101ca33c8c","36a6fd223f578b28","f27043b937d644f1","8f6ae93a7892059d","51a22ab4f24c93b8"]]},{"id":"36a6fd223f578b28","type":"debug","z":"eb86f5a92d01a413","name":"debug 129","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":210,"y":440,"wires":[]},{"id":"f27043b937d644f1","type":"function","z":"eb86f5a92d01a413","name":"Command Digital Output 1","func":"let device_id = msg.payload.device_id;\nlet gpio_output_1 = msg.payload.gpio_output_1;  // \"1\" for on, \"0\" for off\n\nlet payload;\n\n// Check if gpio_output_2 is defined and process the command\nif (gpio_output_1 !== undefined) {\n    if (gpio_output_1 === 1) {\n        // Command to turn on GPIO output 2\n        payload = \"on\";\n    } else if (gpio_output_1 === 0) {\n        // Command to turn off GPIO output 2\n        payload = \"off\";\n    } else {\n        // If the command is not recognized, return without doing anything\n        node.warn(\"Unknown command received: \" + JSON.stringify(gpio_output_1));\n        return null;\n    }\n\n    // Set the MQTT topic and payload\n    msg.topic = `homeassistant/milesight/downlink/24e124445d016017`;\n    msg.payload = payload;\n\n    return msg;\n} else {\n    node.warn(\"GPIO Output command is undefined\");\n    return null;\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":320,"wires":[["7aecdd2da72429c4","7246de110284dc61"]]},{"id":"8f6ae93a7892059d","type":"function","z":"eb86f5a92d01a413","name":"Command Digital Output 2","func":"let device_id = msg.payload.device_id;\nlet gpio_output_2 = msg.payload.gpio_output_2;  // \"1\" for on, \"0\" for off\n\nlet payload;\n\n// Check if gpio_output_2 is defined and process the command\nif (gpio_output_2 !== undefined) {\n    if (gpio_output_2 === 1) {\n        // Command to turn on GPIO output 2\n        payload = Buffer.from([0x07, 0x01, 0x00, 0xff]);\n    } else if (gpio_output_2 === 0) {\n        // Command to turn off GPIO output 2\n        payload = Buffer.from([0x07, 0x00, 0x00, 0xff]);\n    } else {\n        // If the command is not recognized, return without doing anything\n        node.warn(\"Unknown command received: \" + JSON.stringify(gpio_output_2));\n        return null;\n    }\n\n    // Set the MQTT topic and payload\n    msg.topic = `homeassistant/milesight/downlink/24e124445d016017`;\n    msg.payload = payload;\n\n    return msg;\n} else {\n    node.warn(\"GPIO Output command is undefined\");\n    return null;\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":360,"wires":[["7246de110284dc61","7aecdd2da72429c4"]]},{"id":"7246de110284dc61","type":"mqtt out","z":"eb86f5a92d01a413","name":"","topic":"homeassistant/milesight/downlink/$deveui","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"024346bb511cf5e0","x":1300,"y":360,"wires":[]},{"id":"7aecdd2da72429c4","type":"debug","z":"eb86f5a92d01a413","name":"debug 130","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1210,"y":320,"wires":[]},{"id":"51a22ab4f24c93b8","type":"function","z":"eb86f5a92d01a413","name":"Publish Switch Data","func":"let device_id = msg.payload.device_id;\nlet device_name = msg.payload.device_name;\nlet gpio_output_1 = msg.payload.gpio_output_1;\nlet gpio_output_2 = msg.payload.gpio_output_2;\n\nmsg.topic = `homeassistant/switch/UC300/${device_id}/state`;\nmsg.payload = JSON.stringify({\n    device_name: device_name,\n    gpio_output_1: gpio_output_1,\n    gpio_output_2: gpio_output_2,\n});\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":240,"wires":[["aa1a322e3924726f","3dc2462b4bdabe78"]]},{"id":"aa1a322e3924726f","type":"mqtt out","z":"eb86f5a92d01a413","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"024346bb511cf5e0","x":1190,"y":260,"wires":[]},{"id":"3dc2462b4bdabe78","type":"debug","z":"eb86f5a92d01a413","name":"debug 131","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1210,"y":220,"wires":[]},{"id":"05396752e27c1eaf","type":"mqtt out","z":"eb86f5a92d01a413","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"024346bb511cf5e0","x":1190,"y":480,"wires":[]},{"id":"024346bb511cf5e0","type":"mqtt-broker","name":"","broker":"192.168.100.246","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"autoUnsubscribe":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willRetain":"false","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]

here is a picture of the switches in my device on home assistant
Screenshot 2024-08-26 155526

The switches are created through mqtt discovery, yes? When it switches it sends a command. It doesn’t do anything because what is being sent is invalid code.

This is the cellular or the lora model? Is the unit connected and sending messages to to the mqtt broker?

yes switches are created through the mqtt discovery. I dont even think when I click the created button that it does any thing and that’s is one of the problems.

How is the best way to fix the invalid code ?

Its the LoRa model yes the unit is connected to a milesight gateway and the gateway is connected to mqtt mosquitto.

Looking further into this unit, I don’t think you can send commands in a json format. From their documentation it shows hexadecimal examples. There is no info on their site for connecting the lora version over mqtt only for the cellular model.

@mikefila here is a blog about the UC300 and they explain what to send to the gateway via MQTT to trigger the digital outputs. https://sparwan.com/en/blogs/wiki/comment-controler-a-distance-des-appareils-via-mqtt-sur-milesight-gateway?srsltid=AfmBOorhApi7nn2b_5hG8CyZsFZiew8S90qLnw6xidQ4bbuX70D4zuK1

I have tested the commands that need to push to the topic of the milesight gateway from mqtt explorer and the command gets delivered.

I have also nearly got the switches to a working state I have made two separate work flows for output 1 and 2

this is the debug message
Screenshot 2024-08-28 081737

I am trying to figure out why the switch in home assistant still doesn’t want work though any tips on what i need to do or add?

These are the switches that you originally created through discovery? I don’t think you can update them via discovery. I believe you need to delete them and then resend the discovery message to recreate them.