How do I incorporate the hysteresis effect in a control flow?

I have a problem in the variable I want a math loop in node-red but I didn't know how to do it
Variable x= 24
Variable y= 30
If 30> temperature> 24
Temperature - x > 0
X = temperature;
Else if temperature> 30
Temperature - y < 0
Y = temperature

I think you need to explain a bit more about your problem.
You have two FIXED variables x and y, and then on the third line you refer to 'temperature' -where does that come from?
Just write down in words what you are trying to achieve.

Simply I want two variables to take the temperature that we get from an mqtt broker and compare it if it
X= 24
Y= 30
If 30>temperature>24
X -temperature > 0
X= temperature. Then we gonna send in another topic "off "
Else if temperature>30
X- temperature<0
Then we gonna trigger "on"
The main problem that I'm facing right now is when I put switch if temperature>= 30 on
Temperature<= 24 off
The program can't understand what he should do if it between 30 and 24 so we need to compare it if it the heat down he should be "on" till the 24 and if it up he should be "off" till 30
I hope that I explained will

I still don't really understand what you are trying to do.

Is the variable Y the upper limit for your temperature comparison and X is the lower limit??

So the sort of checks you want to do are... IF temperature > Y
and another check is IF temperature < X

This is a very similar question to what you posted in...

Are you trying to incorpoarte a hysteresis effect in your control system?

It gonna be constant variable if the heat is going down by 31 30 29 28 27 ( in negative way ) I want it to keep the on till it equal to 24 so it gonna turn off
And in the inverse way I want it to goin up from 24 till 30 with off but when he is equal to 30 he gonna turn on
The previous problem u give it me the best solution.
But now let just say the temperature in the house is 25 what should the topic be on or off
So if I compare it with a const variable like
X = 24
If the 30 > temperature > 24
We gonna do X - temperature (if it superior than 0 ) it gonna turn off
Else if
Temperature > 30
Temperature - y (y = 30) if it inferior than 0 it gonna turn on

OK - that is a classic hysteresis effect.
It is based on the current value and the previous history (was the thing 'on' or 'off').
It should be possible to write/create a function node using JavaScript to achieve what you need.

However, you may get a response from (other) people on the forum that have used a hysteresis loop/node.

Meanwhile, you might want to take a look at this node (as it might do what you want).

Have a play with this piece of code (it uses the above hysteresis node).

[{"id":"f880ca06f55532b1","type":"hysteresis","z":"c3190d8308dad5b3","name":"","rising_threshold":"30","falling_threshold":"24","initial_edge":"any","x":450,"y":160,"wires":[["4d624ee5b741e4b0"]]},{"id":"b7d9f6bfc6fdf529","type":"inject","z":"c3190d8308dad5b3","name":"30.1","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"30.1","payloadType":"num","x":270,"y":100,"wires":[["f880ca06f55532b1"]]},{"id":"239cc5af875fa75a","type":"debug","z":"c3190d8308dad5b3","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":890,"y":160,"wires":[]},{"id":"8ab1e609b9e4a9ad","type":"inject","z":"c3190d8308dad5b3","name":"26","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"26","payloadType":"num","x":270,"y":160,"wires":[["f880ca06f55532b1"]]},{"id":"d6c3785723d26ecb","type":"inject","z":"c3190d8308dad5b3","name":"23.9","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"23.9","payloadType":"num","x":270,"y":220,"wires":[["f880ca06f55532b1"]]},{"id":"b3811a65b9d30b3e","type":"inject","z":"c3190d8308dad5b3","name":"18","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"18","payloadType":"num","x":270,"y":260,"wires":[["f880ca06f55532b1"]]},{"id":"4d624ee5b741e4b0","type":"function","z":"c3190d8308dad5b3","name":"Check temp band","func":"if (msg.payload >= 30.1) {\n    msg.payload = \"Device is ON\";\n    node.status({fill:\"red\",shape:\"dot\",text:\"Device is ON\"});\n}\nelse if (msg.payload <=23.9) {\n    msg.payload =\"Device is OFF\";\n    node.status({fill:\"green\",shape:\"dot\",text:\"Device is OFF\"});\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":650,"y":160,"wires":[["239cc5af875fa75a"]]},{"id":"ba5d0545e4b041c7","type":"inject","z":"c3190d8308dad5b3","name":"36","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"36","payloadType":"num","x":270,"y":60,"wires":[["f880ca06f55532b1"]]},{"id":"0188da4635ee3be5","type":"comment","z":"c3190d8308dad5b3","name":"Temp band >=30.1 or <=23.9","info":"","x":680,"y":120,"wires":[]}]

You should be able to remove my (test) Inject nodes and connect the parsed output from your MQTT node.

I would suggest you consider changing the title of this post to something like...

"How do I incorporate the hysteresis effect in a control flow?" it would help other people who might read your thread.

Am I missing something?

Surely a single switch node:
Temp < 24 -> output 1 -> off
Temp > 30 -> output 2 -> on
Otherwise do nothing.

1 Like

Yes you can use a Switch node and a lot simpler than my version.
Screen Shot 03-29-22 at 11.44 AM

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