Here is a function that fades out a light at bedtime, where you can set three values (in red):
I'm sure this is possible using only core nodes, but I am using Node-RED to learn a bit more about coding. For example, I wasn't sure how to add a delay into a loop, so I googled for possible ways to do this and came up with this way using a function that seems to call itself:
var startval = msg.payload;
var dimtime = msg.dimtime * 1000;
var initialdelay = msg.initialdelay * 1000;
var steptime = dimtime / startval;
node.send(msg);
setTimeout(function(){
var i = startval;
function f() {
if (i>0) {
i--;
msg.payload = i;
node.send(msg);
node.status({fill:"green", shape:"ring", text:"fading"});
} else {
node.status();
return;
}
setTimeout( f, steptime );
}
f();
},initialdelay)
node.status({fill:"blue", shape:"ring", text:"Initial delay"});
Here it is with some sample values:
[{"id":"164e4745.06ee19","type":"function","z":"705d403b.f2b8","name":"Fade Out","func":"var startval = msg.payload;\nvar dimtime = msg.dimtime * 1000;\nvar initialdelay = msg.initialdelay * 1000;\nvar steptime = dimtime / startval;\n\n\n\n\nnode.send(msg);\n\n// Initial Delay\nsetTimeout(function(){\n var i = startval;\n function f() {\n if (i>0) {\n i--;\n msg.payload = i;\n node.send(msg);\n node.status({fill:\"green\", shape:\"ring\", text:\"fading\"});\n } else {\n node.status();\n return;\n }\n setTimeout( f, steptime );\n }\n f();\n},initialdelay)\n\n\nnode.status({fill:\"blue\", shape:\"ring\", text:\"Initial delay\"});\n","outputs":1,"noerr":0,"x":2960,"y":360,"wires":[["784f9ddd.3bd7d4"]]},{"id":"784f9ddd.3bd7d4","type":"debug","z":"705d403b.f2b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":3110,"y":360,"wires":[]},{"id":"d28c7544.d904a8","type":"change","z":"705d403b.f2b8","name":"Dim settings","rules":[{"t":"set","p":"payload","pt":"msg","to":"10","tot":"num"},{"t":"set","p":"initialdelay","pt":"msg","to":"10","tot":"num"},{"t":"set","p":"dimtime","pt":"msg","to":"30","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":2810,"y":360,"wires":[["164e4745.06ee19"]]},{"id":"f5495c9d.55dd5","type":"inject","z":"705d403b.f2b8","name":"","topic":"home/bedroom/light/circuit2level","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":2660,"y":360,"wires":[["d28c7544.d904a8"]]}]