# Function for Hysteresi on analog signal

**URL:** <https://discourse.nodered.org/t/function-for-hysteresi-on-analog-signal/24195>\
**Category:** General\
**Created:** [3 April 2020 23:36 UTC](https://discourse.nodered.org/t/function-for-hysteresi-on-analog-signal/24195 "2020-04-03T23:36:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rromele](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rromele/32/18431_2.png) [@rromele](https://discourse.nodered.org/u/rromele)\
**Post date:** [3 April 2020 23:36 UTC](https://discourse.nodered.org/t/function-for-hysteresi-on-analog-signal/24195/1 "2020-04-03T23:36:07Z")

</div>

Hi,  
I would like to implement a function for the control of an analog signal, with the operation indicated below:

![hyst01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/d/1d34d7d15791a529c2b50ae4cd838a967062bbef.jpeg)

I don't know how to memorize the fact that the input signal has exceeded the setpoint inside the function attached, any help is welcome.

```auto
[{"id":"483a7328.79364c","type":"function","z":"7aab5d55.3ddbd4","name":"Input","func":"msg.payload=Number(msg.payload);\nmsg.topic = \"Input\";\nreturn msg;","outputs":1,"noerr":0,"x":270,"y":180,"wires":[["a3c54186.81ec"]]},{"id":"e8490a51.f32b38","type":"function","z":"7aab5d55.3ddbd4","name":"SetPoint","func":"msg.payload=Number(msg.payload);\nmsg.topic = \"SetPoint\";\nreturn msg;","outputs":1,"noerr":0,"x":260,"y":280,"wires":[["a3c54186.81ec"]]},{"id":"a3c54186.81ec","type":"function","z":"7aab5d55.3ddbd4","name":"Comparatore","func":"context.set(msg.topic,msg.payload);\n\nvar SetPoint = context.get(\"SetPoint\") ||100;\nvar In = context.get(\"Input\") || 0;\nvar Hyst = context.get(\"Hyst\") || 10;\n\nif (In > (SetPoint)) \n{\n msg.payload = 1; // HL >100\n} \nelse if (In <= (SetPoint-Hyst))\n{\n msg.payload = 0; // LL <=90\n \n} else \n{\n msg.payload = 2; // \n}\n\nreturn msg;","outputs":1,"noerr":0,"x":430,"y":240,"wires":[["b66c62b0.04fd1"]]},{"id":"26479dfa.2ab9f2","type":"inject","z":"7aab5d55.3ddbd4","name":"","topic":"","payload":"91","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":200,"wires":[["483a7328.79364c"]]},{"id":"8809f3e3.e344f","type":"inject","z":"7aab5d55.3ddbd4","name":"","topic":"","payload":"100","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":280,"wires":[["e8490a51.f32b38"]]},{"id":"25e1a718.395a68","type":"inject","z":"7aab5d55.3ddbd4","name":"","topic":"","payload":"105","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":160,"wires":[["483a7328.79364c"]]},{"id":"8aadddc7.7259f","type":"inject","z":"7aab5d55.3ddbd4","name":"","topic":"","payload":"89","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":240,"wires":[["483a7328.79364c"]]},{"id":"b66c62b0.04fd1","type":"switch","z":"7aab5d55.3ddbd4","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"}],"checkall":"true","repair":false,"outputs":3,"x":590,"y":240,"wires":[[],[],[]],"outputLabels":["Reset","SendMsg","Targhet"]}]

```

---

<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:** [4 April 2020 05:37 UTC](https://discourse.nodered.org/t/function-for-hysteresi-on-analog-signal/24195/2 "2020-04-04T05:37:10Z")

</div>

There is a [hysteresis node available](https://flows.nodered.org/node/node-red-contrib-hysteresis), would that help you out ?

---

<div class="post-metadata">

**Author:** ![rromele](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rromele/32/18431_2.png) [@rromele](https://discourse.nodered.org/u/rromele)\
**Post date:** [4 April 2020 21:12 UTC](https://discourse.nodered.org/t/function-for-hysteresi-on-analog-signal/24195/3 "2020-04-04T21:12:39Z")

</div>

Hi bakman2,

thanks for your help, the Hysteresi node works as I wanted, how can I parameterize the input thresholds through an input function?

Can you give me a little idea to work on?

Thanks

---

<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:** [3 June 2020 21:12 UTC](https://discourse.nodered.org/t/function-for-hysteresi-on-analog-signal/24195/4 "2020-06-03T21:12:42Z")

</div>

Closed on 3 June 2020 21:12 UTC

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