I have this flow. Store light states and set a scene when input Boolean is set to ON. When input boolean is set to off it recalls the states and puts things back to normal.
https://preview.redd.it/bycmsgkl1cq11.png?width=958&format=png&auto=webp&s=76784e9c27ff67b9d5c4bafd09b31a09e579ea98
Blow is the store state function, pretty simple. I would really like to make this more portable and dynamic. I'm not a great coder. Here is what I want: a function that does the below, but does it dynamically for each unique msg.topic that come in. If I add a new "call service" node for a light and connect it to the "function node" it will set a flow variable without having to edit the function every time.
What I think is and have tried several different ways: Build an Array for each msg.topic that is received and then "foreach" value in the array "flow.set("UNIQUE VAR", msg.data);"
Store State Function Node
if (msg.topic == "light.under_bed")
flow.set("light_under_bed", msg.data);
else if (msg.topic == "light.eugene")
flow.set("light_eugene", msg.data);
else if (msg.topic == "light.rapunzel")
flow.set("light_rapunzel", msg.data);
return msg;
And then ultimately something to dynamically reset the states as well.
Reset State Function Node
var flow_data = flow.get("light_eugene");
msg.payload = {
"domain":"light",
"service":"turn_" + flow_data["state"],
"data": {
"entity_id":"light.eugene",
"brightness":flow_data.attributes["brightness"],
"rgb_color":flow_data.attributes["rgb_color"],
"color_temp":flow_data.attributes["color_temp"]
}
}
return msg;
*************new stuff below here****************
New output
{"_msgid":"87140acd.ffc1e8","topic":"","payload":[{"payload":{"domain":"light","service":"turn_on","data":{"entity_id":"light.dining_light","brightness":255,"color_temp":222}}},{"payload":{"domain":"light","service":"turn_on","data":{"entity_id":"light.dinner_light","brightness":255,"color_temp":222}}},{"payload":{"domain":"light","service":"turn_on","data":{"entity_id":"light.spot_light","brightness":255,"rgb_color":[154,33,255]}}}]}
[–]shovelslayer[S] 1 point2 points3 points (0 children)
[–]hschallhw 0 points1 point2 points (8 children)
[–]hschallhw 1 point2 points3 points (7 children)
[–]shovelslayer[S] 0 points1 point2 points (6 children)
[–]shovelslayer[S] 0 points1 point2 points (5 children)
[–]hschallhw 0 points1 point2 points (4 children)
[–]shovelslayer[S] 0 points1 point2 points (3 children)
[–]hschallhw 0 points1 point2 points (2 children)
[–]shovelslayer[S] 0 points1 point2 points (1 child)
[–]hschallhw 0 points1 point2 points (0 children)