Skip to content

Instantly share code, notes, and snippets.

@DIYtechie
Last active May 21, 2020 08:43
Show Gist options
  • Save DIYtechie/4e5614087340e87b87f4a6013298d0ac to your computer and use it in GitHub Desktop.
Save DIYtechie/4e5614087340e87b87f4a6013298d0ac to your computer and use it in GitHub Desktop.
Cycle actions (subflow)

This simple subflow can be used to cycle actions such as choosing a new scene or radio station for each new payload received. Suitable to be used in conjunction with a change node. Double click the subflow to set the numbr of scenes/actiona and set AutoReset to true if you want the counter to reset after 3 seconds (great if you want an easy way to default to your favourite scene).

Subflow

image

Example flow 1

image

Example flow 2

image


Copy the subflow below to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option

[
{
"id": "99fb54ac.f882f8",
"type": "subflow",
"name": "Cycle actions",
"info": "",
"category": "",
"in": [
{
"x": 60,
"y": 130,
"wires": [
{
"id": "ae3e5b31.5a9118"
},
{
"id": "48a3b254.5f1a9c"
}
]
}
],
"out": [
{
"x": 350,
"y": 150,
"wires": [
{
"id": "ae3e5b31.5a9118",
"port": 0
}
]
}
],
"env": [
{
"name": "Actions",
"type": "num",
"value": "3",
"ui": {
"type": "input",
"opts": {
"types": [
"num"
]
}
}
},
{
"name": "AutoReset",
"type": "bool",
"value": "false"
}
],
"color": "#DDAA99",
"outputLabels": [
"Action number"
],
"icon": "font-awesome/fa-refresh",
"status": {
"x": 350,
"y": 110,
"wires": [
{
"id": "ae3e5b31.5a9118",
"port": 0
}
]
}
},
{
"id": "ae3e5b31.5a9118",
"type": "function",
"z": "99fb54ac.f882f8",
"name": "Cycle actions",
"func": "var Actions = flow.get(\"toggleNext\") + 1||1;\n\nif (Actions >= env.get(\"Actions\")){\n flow.set(\"toggleNext\",0);\n msg.payload = env.get(\"Actions\");\n }\nelse{\n flow.set(\"toggleNext\", Actions);\n msg.payload = Actions\n }\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 200,
"y": 130,
"wires": [
[]
]
},
{
"id": "53bd1934.771a68",
"type": "trigger",
"z": "99fb54ac.f882f8",
"op1": "",
"op2": "true",
"op1type": "nul",
"op2type": "bool",
"duration": "3",
"extend": true,
"units": "s",
"reset": "",
"bytopic": "all",
"name": "",
"x": 260,
"y": 210,
"wires": [
[
"7b3ccef9.bbba9"
]
]
},
{
"id": "7b3ccef9.bbba9",
"type": "change",
"z": "99fb54ac.f882f8",
"name": "reset counter",
"rules": [
{
"t": "set",
"p": "toggleNext",
"pt": "flow",
"to": "0",
"tot": "num"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 290,
"y": 250,
"wires": [
[]
]
},
{
"id": "48a3b254.5f1a9c",
"type": "switch",
"z": "99fb54ac.f882f8",
"name": "AutoReset on/off",
"property": "AutoReset",
"propertyType": "env",
"rules": [
{
"t": "true"
}
],
"checkall": "true",
"repair": false,
"outputs": 1,
"x": 210,
"y": 170,
"wires": [
[
"53bd1934.771a68"
]
]
},
{
"id": "aa7fa079.8627",
"type": "comment",
"z": "99fb54ac.f882f8",
"name": "Latest version of this and other flows can be found at: https://flows.nodered.org/collection/YsSoz9IDM4Z_",
"info": "",
"x": 400,
"y": 50,
"wires": []
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment