# Function node - Calculate and filter messages

**URL:** <https://discourse.nodered.org/t/function-node-calculate-and-filter-messages/75288>\
**Category:** General\
**Created:** [16 February 2023 10:43 UTC](https://discourse.nodered.org/t/function-node-calculate-and-filter-messages/75288 "2023-02-16T10:43:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![niehans](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/niehans/32/41104_2.png) [@niehans](https://discourse.nodered.org/u/niehans)\
**Post date:** [16 February 2023 10:43 UTC](https://discourse.nodered.org/t/function-node-calculate-and-filter-messages/75288/1 "2023-02-16T10:43:45Z")

</div>

I have this input

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/3/13eee44998dc40d49ea9fbba1d11541dab9a0de7.png)

`[{"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:

```auto
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];

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/2/920e06f6f181a4cc49341c829b9687752048100d.png)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [16 February 2023 10:59 UTC](https://discourse.nodered.org/t/function-node-calculate-and-filter-messages/75288/2 "2023-02-16T10:59:21Z")

</div>

> [@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"] )`

---

<div class="post-metadata">

**Author:** ![niehans](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/niehans/32/41104_2.png) [@niehans](https://discourse.nodered.org/u/niehans)\
**Post date:** [16 February 2023 11:32 UTC](https://discourse.nodered.org/t/function-node-calculate-and-filter-messages/75288/3 "2023-02-16T11:32:21Z")

</div>

Then how to get rid of the original message?

```auto
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];

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [16 February 2023 13:02 UTC](https://discourse.nodered.org/t/function-node-calculate-and-filter-messages/75288/4 "2023-02-16T13:02:28Z")

</div>

> [@niehans](#):
>
> Then how to get rid of the original message?

Instead of setting msg.payload.Lux etc, build up and return a new message.

```auto
let newmsg = {payload:{}}
newmsg.payload.Lux = ((msg.payload["AI1-lux"] * kl) + Rl)
.
.
.
return newmsg

```

---

<div class="post-metadata">

**Author:** ![niehans](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/niehans/32/41104_2.png) [@niehans](https://discourse.nodered.org/u/niehans)\
**Post date:** [16 February 2023 15:11 UTC](https://discourse.nodered.org/t/function-node-calculate-and-filter-messages/75288/5 "2023-02-16T15:11:54Z")

</div>

Thank jbudd

It was the:

```auto
{payload:{}}

```

that I couldn't figure out. Could you give me little explanation about the syntax

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [16 February 2023 18:49 UTC](https://discourse.nodered.org/t/function-node-calculate-and-filter-messages/75288/6 "2023-02-16T18:49:58Z")

</div>

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:{}}**

---

<div class="post-metadata">

**Author:** ![niehans](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/niehans/32/41104_2.png) [@niehans](https://discourse.nodered.org/u/niehans)\
**Post date:** [28 February 2023 10:26 UTC](https://discourse.nodered.org/t/function-node-calculate-and-filter-messages/75288/7 "2023-02-28T10:26:01Z")

</div>

Thanks for the explanation 😉

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [14 March 2023 10:26 UTC](https://discourse.nodered.org/t/function-node-calculate-and-filter-messages/75288/8 "2023-03-14T10:26:51Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
