I have this input
[{"id":"0ffff120ea2c2c27","type":"inject","z":"f6f2187d.f17ca8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"AI1-lux\":962,\"AI2-disp\":1,\"AI3-hal1\":227,\"AI4-hal2\":237,\"AI5-selv\":0,\"AI6-udv\":220,\"VM50-hal1-on\":30,\"VM52-hal1-off\":20,\"VM54-hal2-on\":30,\"VM56-hal2-off\":0,\"Q1-varme-hal1\":true,\"Q2-ventilator-hal1\":false,\"Q3-varme-hal2\":false,\"Q4-ventilator-hal2\":false,\"Q5-varmekabel1-1\":true,\"Q6-varmekabel1-2\":true,\"Q7-varmekabel2-1\":true,\"Q8-varmekabel2-2\":true,\"Q9-el-trace\":false,\"Q10-ind-ud-plade\":false,\"Q11-lys-ch1\":false,\"Q12-lys-ch2\":false}","payloadType":"json","x":270,"y":300,"wires":[["d7c720af1e60ea56"]]}]
And I want to calculate and filter the message. I have tried with this code but it return the original message and my calculated message:
const Rl = 30
const kl = 2.97
const Rt = -500
const kt = 2.5
msg.payload.lux = ((msg.payload["AI1-lux"] * kl) + Rl)
msg.payload.Halltemp1 = ((msg.payload["AI3-hal1"] * kt) + Rt) / 10
msg.payload.Halltemp2 = ((msg.payload["AI4-hal2"] * kt) + Rt) / 10
msg.payload.Outdoortemp = ((msg.payload["AI6-udv"] * kt) + Rt) / 10
if (msg.payload["Q1-varme-hal1"] === "true" || msg.payload["Q1-varme.hal1"] === true) {
msg.payload.Heater1 = 1;
} else {
msg.payload.Heater1 = 0;
}
if (msg.payload["Q3-varme-hal2"] === "true" || msg.payload["Q3-varme-hal2"] === true) {
msg.payload.Heater2 = 1;
} else {
msg.payload.Heater2 = 0;
}
return [msg];
niehans:
=== "true"
You are comparing the type (===
) and the string "true"
while they are booleans, they will evaluate to false.
instead you can use if(msg.payload["Q1-varme-hal1"] || msg.payload["Q1-varme.hal1"] )
Then how to get rid of the original message?
const Rl = 30
const kl = 2.97
const Rt = -500
const kt = 2.5
msg.payload.Lux = ((msg.payload["AI1-lux"] * kl) + Rl)
msg.payload.Halltemp1 = ((msg.payload["AI3-hal1"] * kt) + Rt) / 10
msg.payload.Halltemp2 = ((msg.payload["AI4-hal2"] * kt) + Rt) / 10
msg.payload.Outdoortemp = ((msg.payload["AI6-udv"] * kt) + Rt) / 10
if (msg.payload["Q1-varme-hal1"] || msg.payload["Q1-varme.hal1"] ) {
msg.payload.Heater1 = 1;
} else {
msg.payload.Heater1 = 0;
}
if (msg.payload["Q3-varme-hal2"] || msg.payload["Q3-varme-hal2"] ) {
msg.payload.Heater2 = 1;
} else {
msg.payload.Heater2 = 0;
}
return [msg];
jbudd
16 February 2023 13:02
4
Instead of setting msg.payload.Lux etc, build up and return a new message.
let newmsg = {payload:{}}
newmsg.payload.Lux = ((msg.payload["AI1-lux"] * kl) + Rl)
.
.
.
return newmsg
Thank jbudd
It was the:
{payload:{}}
that I couldn't figure out. Could you give me little explanation about the syntax
jbudd
16 February 2023 18:49
6
Well you can't assign a value to somevar.something unless somevar is already an object, so you declare it with let somevar={}
And (I didn't realise this either till I tested my reply) you can't assign a value to mewmsg.payload.something unless newmsg.payload is already an object.
So I declared newmsg as an object which contains payload, also an object
let newmsg = {payload:{}}
Thanks for the explanation
system
Closed
14 March 2023 10:26
8
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.