Node-RED/CheatSheet: Difference between revisions

From Wiki
(Created page with "== Global variables == <pre> var labelmode = global.get("labelmode") </pre> == Node Status == <pre> node.status({fill:"gray",shape:"dot",text:msg.serialnumber + " = " + msg.pclass + "W"}); </pre> == Stop messages == <pre> if ( ...)) { return null; } else { return msg; } </pre> == InfluxDB == * write data with custom timestamp <pre> var timestamp = new Date(date + 'Z').getTime() * 1e6 msg = { payload : { value: value, time: timestamp },...")
 
mNo edit summary
 
(4 intermediate revisions by the same user not shown)
Line 2: Line 2:
<pre>
<pre>
var labelmode = global.get("labelmode")
var labelmode = global.get("labelmode")
global.set("labelmode", 123)
</pre>
</pre>
== Convert string to number ==
* Change-node
* JSONata: $number(payload)


== Node Status ==
== Node Status ==
Line 8: Line 14:
node.status({fill:"gray",shape:"dot",text:msg.serialnumber + " = " + msg.pclass + "W"});
node.status({fill:"gray",shape:"dot",text:msg.serialnumber + " = " + msg.pclass + "W"});
</pre>
</pre>
== Stop messages ==
== Stop messages ==
<pre>
<pre>
Line 28: Line 32:
         measurement: topic }
         measurement: topic }
</pre>
</pre>
== Retrieve retained MQTT message ==
<pre>
npm install mqtt --save
</pre>
* settings.js
<pre>
functionGlobalContext: {
mqtt:require('mqtt')
},
</pre>
* Function node
<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>
[[Category:Programming]]

Latest revision as of 15:14, 28 September 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

npm install mqtt --save
  • settings.js
functionGlobalContext: {
mqtt:require('mqtt')
},
  • Function node
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;