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