If with two variables

Hello,

I have the follwing flow.....so I want to have that the function with "Heizungssteuerung" prove, if the temperaturedifference is 2or lower AND if the servo ist still on the degree that it should be.....and if so, don't turn the servo again.....I thought I can do it with
if ((c>2) && (d!=120)) {return {payload:120];}
But my servo spinning around, if I do that......
Here's my flow:

[{"id":"9ba45455.216818","type":"grove-temperature-humidity-sensor-sht3x","z":"26dadbef.3ae754","name":"","x":380,"y":220,"wires":[["f7b34d4b.127e68","e48d9b1d.5eeba"]]},{"id":"3c9b95a2.b8c25a","type":"grove-servo","z":"26dadbef.3ae754","name":"","port_number":"18","x":1330,"y":160,"wires":[]},{"id":"f7b34d4b.127e68","type":"function","z":"26dadbef.3ae754","name":"Temperatur","func":"msg.payload = msg.payload.temperature;\nmsg.topic = \"temperatur_akt\"\nreturn msg;\n","outputs":1,"noerr":0,"x":710,"y":120,"wires":[["928752c8.ddabc8","288216e8.63f292","1d76f4d8.5300fb"]]},{"id":"e48d9b1d.5eeba","type":"function","z":"26dadbef.3ae754","name":"Luftfeuchtigkeit","func":"msg.payload = msg.payload.humidity;\nreturn msg;","outputs":1,"noerr":0,"x":720,"y":280,"wires":[["6137e324.198d8c","35536c1c.3cc81c"]]},{"id":"928752c8.ddabc8","type":"debug","z":"26dadbef.3ae754","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1130,"y":120,"wires":[]},{"id":"6137e324.198d8c","type":"debug","z":"26dadbef.3ae754","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1130,"y":240,"wires":[]},{"id":"288216e8.63f292","type":"ui_gauge","z":"26dadbef.3ae754","name":"Temperatur","group":"c7fb338a.be9a98","order":2,"width":0,"height":0,"gtype":"gage","title":"Temperatur","label":"°C","format":"{{value}}","min":0,"max":"40","colors":["#0000ff","#ff8000","#ca3838"],"seg1":"15","seg2":"25","x":1150,"y":200,"wires":[]},{"id":"35536c1c.3cc81c","type":"ui_gauge","z":"26dadbef.3ae754","name":"Luftfeuchtigkeit","group":"c7fb338a.be9a98","order":3,"width":0,"height":0,"gtype":"gage","title":"Luftfeuchtigkeit","label":"%","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":1140,"y":280,"wires":[]},{"id":"c8d4a57a.06538","type":"inject","z":"26dadbef.3ae754","name":"","topic":"","payload":"","payloadType":"date","repeat":"5","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":220,"wires":[["9ba45455.216818"]]},{"id":"81c4f09a.612f48","type":"function","z":"26dadbef.3ae754","name":"Heizungseinstellung","func":"a = msg.payload.temperatur_akt;\nb = msg.payload.temperatur_wunsch;\nc = b-a;\nd = msg.payload.einstellung;\nif ((c>2) && (d!=120))\n{return {payload: 120};}\nelse if ((c>0) && (d!=60)){return {payload:60};}\nelse if ((c<=0) && (d!==0)){return {payload:0};}\nreturn msg;\n","outputs":1,"noerr":0,"x":1120,"y":160,"wires":[["3c9b95a2.b8c25a","38b77c7.ecba484","5f5847ca.ce7d7"]]},{"id":"38b77c7.ecba484","type":"debug","z":"26dadbef.3ae754","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1330,"y":120,"wires":[]},{"id":"3c3c1abf.1e56fe","type":"trigger","z":"26dadbef.3ae754","op1":"","op2":"0","op1type":"pay","op2type":"str","duration":"-5","extend":false,"units":"s","reset":"","bytopic":"all","name":"","x":1120,"y":340,"wires":[["1d76f4d8.5300fb"]]},{"id":"1d76f4d8.5300fb","type":"join","z":"26dadbef.3ae754","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":890,"y":100,"wires":[["81c4f09a.612f48","f66cf801.8da6c"]]},{"id":"7f757389.f306dc","type":"ui_text","z":"26dadbef.3ae754","group":"c7fb338a.be9a98","order":3,"width":"0","height":"0","name":"Wunschtemperatur","label":"Wunschtemperatur","format":"{{msg.payload}}","layout":"col-center","x":770,"y":220,"wires":[]},{"id":"f66cf801.8da6c","type":"debug","z":"26dadbef.3ae754","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1130,"y":80,"wires":[]},{"id":"c4543955.e894e8","type":"ui_slider","z":"26dadbef.3ae754","name":"","label":"","tooltip":"","group":"c7fb338a.be9a98","order":4,"width":0,"height":0,"passthru":true,"outs":"all","topic":"temperatur_wunsch","min":"15","max":"30","step":1,"x":730,"y":160,"wires":[["7f757389.f306dc","3c3c1abf.1e56fe"]]},{"id":"5f5847ca.ce7d7","type":"function","z":"26dadbef.3ae754","name":"","func":"msg.payload = msg.payload;\nmsg.topic = \"einstellung\";\nreturn msg;","outputs":1,"noerr":0,"x":900,"y":440,"wires":[["1d76f4d8.5300fb"]]},{"id":"c7fb338a.be9a98","type":"ui_group","z":"","name":"Heizungssteuerung","tab":"becc9a80.77594","disp":true,"width":"6","collapse":false},{"id":"becc9a80.77594","type":"ui_tab","z":"","name":"Heizungssteuerung","icon":"dashboard","disabled":false,"hidden":false}]

Thanks for your help !!!

Hello,
There were many problems with your function as you were writing it.
You logic was right but you were formatting the code wrong and using wrong return statements.
It should look something like this:

let a = parseFloat(msg.payload.temperatur_akt);
let b = parseFloat(msg.payload.temperatur_wunsch);
let c = b-a;
let d = parseFloat(msg.payload.einstellung);
if (c>2 && d!=120) {
    msg.payload = 120;
} else if (c>0 && d!=60) {
    msg.payload = 60;
} else if (c<=0 && d!==0) {
    msg.payload = 0;
}
if(typeof msg.payload != "number") return null;
return msg;

I also added a check for typeof msg.payload as their are a whole lot of possibilities where neither of the three conditions you put in are true and the original payload object would be returned (for example you already set your servo to 0 and the last condition is true, you dont want to return anything).
Hope this is what you were looking for.
I tested this function and it should work.
for example when you put in a msg.payload object like this:

{
    "temperatur_akt": "18",
    "temperatur_wunsch": "17",
    "einstellung": "120"
}

it returns 0.
and for something like:

{
    "temperatur_akt": "20",
    "temperatur_wunsch": "21",
    "einstellung": "0"
}

it returns 60.
Best regards Johannes

2 Likes

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