# Node Red loop for MQTT messages

**URL:** <https://discourse.nodered.org/t/node-red-loop-for-mqtt-messages/77524>\
**Category:** General\
**Created:** [13 April 2023 10:35 UTC](https://discourse.nodered.org/t/node-red-loop-for-mqtt-messages/77524 "2023-04-13T10:35:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MikeyMan83](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikeyman83/32/66792_2.png) [@MikeyMan83](https://discourse.nodered.org/u/MikeyMan83)\
**Post date:** [13 April 2023 10:35 UTC](https://discourse.nodered.org/t/node-red-loop-for-mqtt-messages/77524/1 "2023-04-13T10:35:33Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/4/e42398e4d1ddb6b1bc62aba58d03d208be942c91.png)

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:

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

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.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [13 April 2023 12:28 UTC](https://discourse.nodered.org/t/node-red-loop-for-mqtt-messages/77524/2 "2023-04-13T12:28:19Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [13 April 2023 21:42 UTC](https://discourse.nodered.org/t/node-red-loop-for-mqtt-messages/77524/3 "2023-04-13T21:42:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MikeyMan83](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikeyman83/32/66792_2.png) [@MikeyMan83](https://discourse.nodered.org/u/MikeyMan83)\
**Post date:** [14 April 2023 08:03 UTC](https://discourse.nodered.org/t/node-red-loop-for-mqtt-messages/77524/4 "2023-04-14T08:03:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MikeyMan83](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikeyman83/32/66792_2.png) [@MikeyMan83](https://discourse.nodered.org/u/MikeyMan83)\
**Post date:** [14 April 2023 08:05 UTC](https://discourse.nodered.org/t/node-red-loop-for-mqtt-messages/77524/5 "2023-04-14T08:05:38Z")

</div>

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":""}]`

---

<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:** [13 June 2023 08:05 UTC](https://discourse.nodered.org/t/node-red-loop-for-mqtt-messages/77524/6 "2023-06-13T08:05:56Z")

</div>

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