Node-RED/CheatSheet: Difference between revisions

From Wiki
mNo edit summary
mNo edit summary
Line 31: Line 31:
                     time: timestamp },
                     time: timestamp },
         measurement: topic }
         measurement: topic }
</pre>
== Retrieve retained MQTT message ==
<pre>
const mqtt = global.get('mqtt')
const client = mqtt.connect('mqtt://192.168.111.1');
const topic = "company/line1/pos1/MES/currentSN"
client.on("connect", () => {
  client.subscribe(topic)
})
client.on("message", (topic, message) => {
  msg.currentSN = message.toString()
  node.send(msg)
  client.end();
})
return;
</pre><pre>
functionGlobalContext: {
mqtt:require('mqtt')
},
</pre><pre>
npm install mqtt --save
</pre>
</pre>

Revision as of 12:23, 19 May 2024

Global variables

var labelmode = global.get("labelmode")
global.set("labelmode", 123)

Convert string to number

  • Change-node
  • JSONata: $number(payload)

Node Status

node.status({fill:"gray",shape:"dot",text:msg.serialnumber + " = " + msg.pclass + "W"});

Stop messages

if ( ...)) { 
return null; 
} else {
return msg;
}

InfluxDB

  • write data with custom timestamp
var timestamp = new Date(date + 'Z').getTime() * 1e6

msg = { payload : { value: value,
                    time: timestamp },
        measurement: topic }

Retrieve retained MQTT message

const mqtt = global.get('mqtt')
const client = mqtt.connect('mqtt://192.168.111.1');
const topic = "company/line1/pos1/MES/currentSN"

client.on("connect", () => {
  client.subscribe(topic)
})

client.on("message", (topic, message) => {
  msg.currentSN = message.toString()
  node.send(msg)
  client.end();
})

return;

functionGlobalContext: { mqtt:require('mqtt') },

npm install mqtt --save