Node red - write a function and call it from a function

Hello,

I am learning node red and I need help to optimize a flow that I have created.

This is the flow : Node red project - Pastebin.com

To make this flow work, you need additionally to import : node-red-contrib-zigbee2mqtt-devices/scene-selector.md at master · Dirnei/node-red-contrib-zigbee2mqtt-devices (github.com)

I have smart switches installed at home with tasmota firmware. All the light are tuya based smart bulbs.

After pressing buttons on the smart switches, they send mqtt commands for which I wrote functions that routes them to appropriate action (turn on and off , dimmer up and down, cycle created scenes,etc).

I do not know if it has been made in the best way, but it works.

It is not finished yet, but it becomes more and more complex every time I add something new.

So I want to reduce and organize better the nodes and the written code, so if errors will occur it will be more easier to find a solution.

I have some questions for which I hope to receive some hints.

  1. After the node “check status light”, we have “Action”, “choose single” (a button of a switch is pressed once) and “choose double” (the button is pressed two times) nodes which I would like to merge them into one function called “Switch” that I started already to write. I have also another function node called “dimmer” which I would like to keep it separate, because I do not want to make these functions too long and not readable. So the idea is to call the already made function dimmer from the switch node. Honestly speaking, I do not know how to do it. Could you help me?
  2. The other thing are the scene selection nodes. If possible I do not want to use them, I will prefer to use code written by my selves. I have created 6 scenes in home assistant, two for each room. Every room has multiple smart bulbs connected to a button of a smart switch. In every room we have a default scene and a relax scene. The default one, all the bulbs have been set to white mode, while in the relax scene, the bulbs have different colours. I would like to add more scenes in the future.

It works in this way, pressing the first button switch, it turns on or off the group of lights. Pressing the first button two times, it dimmers up the light if it is on and it has 4 brightness steps afterwards it is set to the minimum brightness again. Pressing the second button switch, it cycles between the two created scenes default and relax. I want with every next button press to cycle all the created scenes.Here I want to use something written by my selves, but I do not know how to get the previous selected scenes so to set the next one. Any ideas?