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;