# How to compare sensor values with a number

**URL:** <https://discourse.nodered.org/t/how-to-compare-sensor-values-with-a-number/69337>\
**Category:** General\
**Tags:** function-node\
**Created:** [20 October 2022 14:40 UTC](https://discourse.nodered.org/t/how-to-compare-sensor-values-with-a-number/69337 "2022-10-20T14:40:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Randomguys](https://avatars.discourse-cdn.com/v4/letter/r/c57346/32.png) [@Randomguys](https://discourse.nodered.org/u/Randomguys)\
**Post date:** [20 October 2022 14:40 UTC](https://discourse.nodered.org/t/how-to-compare-sensor-values-with-a-number/69337/1 "2022-10-20T14:40:10Z")

</div>

Hello everyone, I'm new here. I'm struggling to write a function that compare a sensor value with a number then return 1 if the sensor value equal or grater than the number and return 0 if it smaller. I'm using Modbus RTU (RS485) to receive the sensor value and control the Raspberry pin. Sorry for my bad English, i'm not a english speaker.

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 October 2022 15:44 UTC](https://discourse.nodered.org/t/how-to-compare-sensor-values-with-a-number/69337/2 "2022-10-20T15:44:37Z")

</div>

Welcome to the forum @Randomguys

If you make it just do what you have said, then when the value is very close to the number you are comparing it with, then the noise on the measurement may make it hop up and down a bit around that value, so the gpio pin will flick up and down, which is probably not what you want. You will probably need to apply a bit of hysteresis, so you get a small dead band around the number. Have a look at [node-red-contrib-hysteresis](https://flows.nodered.org/node/node-red-contrib-hysteresis) which may do what you want. Set one threshold just above your comparison value and one just below it.

---

<div class="post-metadata">

**Author:** ![Randomguys](https://avatars.discourse-cdn.com/v4/letter/r/c57346/32.png) [@Randomguys](https://discourse.nodered.org/u/Randomguys)\
**Post date:** [20 October 2022 15:59 UTC](https://discourse.nodered.org/t/how-to-compare-sensor-values-with-a-number/69337/3 "2022-10-20T15:59:59Z")

</div>

Thanks for reply my question but i haven't write the function yet. Can you help me with that ?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 October 2022 16:08 UTC](https://discourse.nodered.org/t/how-to-compare-sensor-values-with-a-number/69337/4 "2022-10-20T16:08:08Z")

</div>

You don't need a function. Use the node I linked to instead.

---

<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:** [19 December 2022 16:09 UTC](https://discourse.nodered.org/t/how-to-compare-sensor-values-with-a-number/69337/5 "2022-12-19T16:09:01Z")

</div>

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