Node Red loop for MQTT messages

Hi !

I'm trying to build a lighting dimmer for Zwave in Node red. However, i can't for the life of me figure out how not to loop it.
The endgame is; lighting that is dimmed according to the time of day. During the day at 60%, during evening 40%, during the night 20% for instance.

This is somewhat working. However; i do want to be able to adjust the dimming levels with the wall switch afterwards. Something i've been able to do with a blockly in Domoticz.

I now want to have the same functionality in Node Red, with OpenZwave being depricated. However, it's looping.
What i have in test currently:

Writing the new value into the MQTT folder, triggers the new test and thus a loop. Not unexpected if you think about it. But how to prevent this?

FYI; i'm using ZwaveJS currently.

Not sure why you are combining both inputs into your function node.

If you separate them, you won't have an issue.

Also, do you get the same response from the MQTT input when the light responds to a time command and a wall switch command? If so, use that in the function node to forward a different message for each.

The inject node you mean? That was just for testing purposes.
And not the cause of the loop.

Did manage to fix it though, by putting the return inside the if function:

[{"id":"496286b068576e81","type":"function","z":"02452070554fbb83","name":"function 21","func":"//Dimmer values\nlet value = msg.payload.value;\nlet newvalue = {}\nlet oldvalue = global.get('Store.Oldvalue', \"file\");\nvar dimLevelNight = 9;\nvar dimLevelDay = 59;\nvar dimLevelDusk = 29;\nvar dimPeriod = 30;\nvar dimLevel = {};\n\n//Time frames\nvar Sunrise = new Date(global.get('Sunrise', \"file\"));\nvar Sunset = new Date(global.get('Sunset', \"file\"));\nvar now = new Date();\n\nvar beforeSunrise = new Date(Sunrise.getTime() - (dimPeriod * 60 * 1000));\nvar afterSunset = new Date(Sunset.getTime() + (dimPeriod * 60 * 1000));\n\nvar daytimeStart = new Date(beforeSunrise.getTime() + (dimPeriod * 60 * 1000));\nvar daytimeEnd = new Date(afterSunset.getTime() - (dimPeriod * 60 * 1000));\n\n//Set dim level:\nif ((now >= afterSunset || now < beforeSunrise)) {\n dimLevel = dimLevelNight;\n// msg.payload.timeframe = 'Night';\n} \n\nelse if ((now >= daytimeStart && now <= daytimeEnd)) {\n dimLevel = dimLevelDay;\n// msg.payload.timeframe = 'Day';\n} \n\nelse \n{\n dimLevel = dimLevelDusk;\n// msg.payload.timeframe = 'Dusk';\n}\n\n\n//Function\nif (oldvalue !== value) {\n global.set('Store.Oldvalue', value, \"file\");\n if (oldvalue == 0)\n {\n newvalue = dimLevel\n msg.payload = newvalue;\n// msg.payload = { \"old value\": oldvalue, \"new value\": newvalue }; // Test payload\n\n \n return msg;\n \n }\n else if (oldvalue !== 0)\n {\n newvalue = msg.payload.value;\n msg.payload = newvalue;\n// msg.payload = { \"old value\": oldvalue, \"new value\": newvalue }; // Test payload\n\n\n return msg;\n }\n }\n\n\n else if (oldvalue == newvalue)\n {\n return null; \n }\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":140,"wires":[["fbe6da029b6e2a8a","c456afd3f4c2be09"]]},{"id":"fbe6da029b6e2a8a","type":"debug","z":"02452070554fbb83","name":"debug 174","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":910,"y":220,"wires":[]},{"id":"0d3b3e9237b6776d","type":"mqtt in","z":"02452070554fbb83","name":"Badkamerschakelaar ZwaveJS","topic":"zwave/18/38/1/currentValue","qos":"0","datatype":"auto-detect","broker":"92e5b2c.00a8b5","nl":false,"rap":true,"rh":0,"inputs":0,"x":250,"y":140,"wires":[["496286b068576e81","598fbf626744c129"]]},{"id":"598fbf626744c129","type":"debug","z":"02452070554fbb83","name":"debug 175","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":220,"wires":[]},{"id":"c456afd3f4c2be09","type":"mqtt out","z":"02452070554fbb83","name":"","topic":"zwave/18/38/1/targetValue/set","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"92e5b2c.00a8b5","x":970,"y":140,"wires":[]},{"id":"92e5b2c.00a8b5","type":"mqtt-broker","name":"MQTT (NAS)","broker":"10.0.0.4","port":"1883","clientid":"Node_Red_Client","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willRetain":"false","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]

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