# Compare numbers in switch node doesn't work

**URL:** <https://discourse.nodered.org/t/compare-numbers-in-switch-node-doesnt-work/57876>\
**Category:** General\
**Created:** [6 February 2022 15:43 UTC](https://discourse.nodered.org/t/compare-numbers-in-switch-node-doesnt-work/57876 "2022-02-06T15:43:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Moosbueffel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moosbueffel/32/36732_2.png) [@Moosbueffel](https://discourse.nodered.org/u/Moosbueffel)\
**Post date:** [6 February 2022 15:43 UTC](https://discourse.nodered.org/t/compare-numbers-in-switch-node-doesnt-work/57876/1 "2022-02-06T15:43:22Z")

</div>

What's wrong comparing numbers with switch node? I don't get an error, so I think my code is correct:  
 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/c/bc2b34d13ede874b1a2aa92b186c4efb2c11fffb.png)

The _true_ comes from this function node, where it works:

```auto
let val = Date.now() / 1000 | 0; // timestamp in sec

msg.payload = msg.payload === val ? true : false;

return msg;

```

My MQTT node uses JSON interpretation, to get a number. The flow:

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/3/23d39210a51bfc3eb8ae717f7a797ca038bd53b8.png)

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [6 February 2022 15:58 UTC](https://discourse.nodered.org/t/compare-numbers-in-switch-node-doesnt-work/57876/2 "2022-02-06T15:58:45Z")

</div>

You will see the red box around your "number" this is because it is not a number, it needs to be evaluated first.

You can use jsonata to do calculations, although current time will change during the operation so you may not get a match to the payload anyway ?

What are you trying to do with the flow ?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [6 February 2022 16:03 UTC](https://discourse.nodered.org/t/compare-numbers-in-switch-node-doesnt-work/57876/3 "2022-02-06T16:03:46Z")

</div>

If you want to check if msg.payload is true or false, then click the chevron next to "==" and select "is true" or "is false"  
[edit] p.s. In your function code, when you divide your timestamp by 1000 you will end up with a float, you will need to round it. You also may want to compare if difference between timestamps(inseconds) is \> or \< than a certain period.

---

<div class="post-metadata">

**Author:** ![Moosbueffel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moosbueffel/32/36732_2.png) [@Moosbueffel](https://discourse.nodered.org/u/Moosbueffel)\
**Post date:** [6 February 2022 16:36 UTC](https://discourse.nodered.org/t/compare-numbers-in-switch-node-doesnt-work/57876/4 "2022-02-06T16:36:04Z")

</div>

I don't really understand your answer. Why is it not a number? My code? In my function node, it will generate a number!?

Choosing JSONata will also result in a red box. Perhaps I have to change the code??

Problem could be the correct time, so I will need a function node anyway to unsharp the time.

I get a timestamp from another program on my RASPI via MQTT and this is my way to test, if the program sending the MQTT is alive.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [6 February 2022 16:43 UTC](https://discourse.nodered.org/t/compare-numbers-in-switch-node-doesnt-work/57876/5 "2022-02-06T16:43:45Z")

</div>

> [@Moosbueffel](#):
>
> I don't really understand your answer. Why is it not a number? My code? In my function node, it will generate a number!?

numbers are for numbers only. It does NOT evaluate a forumla.

> [@Moosbueffel](#):
>
> Choosing JSONata will also result in a red box. Perhaps I have to change the code??

Your equation is not proper JSONata

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [6 February 2022 16:43 UTC](https://discourse.nodered.org/t/compare-numbers-in-switch-node-doesnt-work/57876/6 "2022-02-06T16:43:59Z")

</div>

The == is just comparing payload with what you type in - it doesn't do the calculation for you.

But if you do something like this, then it will. So injecting payload of 5 will pass to output 1

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

But as I said you would need to look at reducing the time stamp anyway to allow processing time 😉

---

<div class="post-metadata">

**Author:** ![Moosbueffel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moosbueffel/32/36732_2.png) [@Moosbueffel](https://discourse.nodered.org/u/Moosbueffel)\
**Post date:** [6 February 2022 16:57 UTC](https://discourse.nodered.org/t/compare-numbers-in-switch-node-doesnt-work/57876/7 "2022-02-06T16:57:52Z")

</div>

Ok, thanks I learned:  
== is only for distinct numbers  
JSONata is only for basic calculation (like adjust measurement)

---

<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:** [20 February 2022 16:58 UTC](https://discourse.nodered.org/t/compare-numbers-in-switch-node-doesnt-work/57876/8 "2022-02-20T16:58:26Z")

</div>

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