# Zwave JS loop help

**URL:** https://discourse.nodered.org/t/zwave-js-loop-help/101794
**Category:** General
**Created:** [16 September 2026 17:06 UTC](https://discourse.nodered.org/t/zwave-js-loop-help/101794 "2026-09-16T17:06:36Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![fogmajor](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@fogmajor](https://discourse.nodered.org/u/fogmajor)
#### Post date: [16 September 2026 17:06 UTC](https://discourse.nodered.org/t/zwave-js-loop-help/101794/1 "2026-09-16T17:06:36Z")

</div>

I currently have 3 zwave devices that I switch on with delay between them of a few seconds

 ![Nodered](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/e/0e40e411561ed7d181ef7f0e6cc4329575228d1f.png)

The function node is as follows

```auto
if (msg.payload.directive == "TurnOn") { // === means equals, both value and datatype ie 1 but not "1"
 var onoff = true
}
else if (msg.payload.directive == "TurnOff") {
var onoff = false
}
//var on = msg.payload /\*trigger\*/
const \_nodeId = 23; /\* Target Node \*/
const \_value = onoff; /\* Value \*/
const \_valueId = {
"commandClassname": "Binary Switch", 
"commandClass": 37, /\* Binary Class \*/
"endpoint": 0, /\* Target End point - The device might have 2 binary switches \*/
 "property": "targetValue", /\* Property \*/
}
const Message = {
payload: {
 cmd: {
api: 'VALUE',
method: 'setValue'
 },
 cmdProperties: {
 nodeId: \_nodeId,
valueId: \_valueId,
value: \_value
 }
 }
}
return Message;

```

I the move to the second function one changing the Node ID and then the third.

Is there a way of just having one function and having the node id changing

Many thanks

Andrew

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [16 September 2026 17:12 UTC](https://discourse.nodered.org/t/zwave-js-loop-help/101794/2 "2026-09-16T17:12:35Z")

</div>

You should be able to send an array of ID's e.g `[23,24,25]`As @marcus-j-davies has allowed mutli casting in his nodes.

Or if you wamt a delay set up a loop ( for, forech, etc) and use node.send() rather than return, and delay each output of the function node with a delay set to rate limit. [Writing Functions : Node-RED](https://nodered.org/docs/user-guide/writing-functions#sending-messages-asynchronously)

Or use a split node and split the array of id,s and use the split payload in the same function node, you can rate limit each split message.

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [16 September 2026 19:08 UTC](https://discourse.nodered.org/t/zwave-js-loop-help/101794/3 "2026-09-16T19:08:32Z")

</div>

As mentioned above...

send 1 message, but set `nodeId` to an array of Device ID's

- Providing all devices in the array support the same class / security (I think - but there might be magic in the driver to address that anyway)

You shouldn't need to loop / spread.  
If multicasting isn't an option - refer to `node.send()`

---

<div class="post-metadata">

### Author: ![fogmajor](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@fogmajor](https://discourse.nodered.org/u/fogmajor)
#### Post date: [17 September 2026 09:26 UTC](https://discourse.nodered.org/t/zwave-js-loop-help/101794/4 "2026-09-17T09:26:56Z")

</div>

Hi and thank you so much for your prompt reply.  
Would you be able to expand on your responses with a brief outline of the requirements, i am fairly new to nodered.

Thank you in advance

Andrew

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [17 September 2026 19:29 UTC](https://discourse.nodered.org/t/zwave-js-loop-help/101794/5 "2026-09-17T19:29:24Z")

</div>

Hi @fogmajor

I have butchered your image.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/3/530b110ddacb43774948ba93e864990daaa98f37.png)

And the `function` node that is left, use the following

```auto
/* Short hand for 'If its TurnOn = true else false' */
let Value = (msg.payload.directive === "TurnOn" ? true : false)

const Nodes = [3, 5, 7] /* Your Target Nodes */

const ValueID = {
    "commandClass": 37, /* Binary Class */
    "endpoint": 0, /* Target End point - */
    "property": "targetValue" /* Property */
}

const Message = {
    payload: {
        cmd: {
            api: 'VALUE',
            method: 'setValue'
        },
        cmdProperties: {
            nodeId: Nodes,
            valueId: ValueID,
            value: Value
        }
    }
}
return Message

```

Sending an array (collection of Node ID's) : [1,2,3,4,5]  
The Device Node will attempt to send a single message to all target nodes, instead of 3 messages to the network.

This is not a Node RED thing its a Marcus is great thing 😉

- It uses multicast to achieve it.
  - The stipulation is, the Target nodes, must support the same class (and I think Security class), so its trial and error - although I think the underlining driver does a lot here to address that)

Also..

Where you see `commandClassName`, `propertyName` its just metadata, not a required property

An array in javascript, is just that... an array of "things"  
`[Something, Something, Something, Something]`

In the Device Node, it supports an array of Node IDs

---

<div class="post-metadata">

### Author: ![fogmajor](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@fogmajor](https://discourse.nodered.org/u/fogmajor)
#### Post date: [17 September 2026 22:24 UTC](https://discourse.nodered.org/t/zwave-js-loop-help/101794/6 "2026-09-17T22:24:48Z")

</div>

Hi Marcus  
May I take this opportunity to thank you for the basic and informative way you explained the solution. It works a treat there was no trial and error but it also taught me about arrays. Thank you for your input of Zwavejs.

As late father used to say "Every day is a school day"

Best wishes

Andrew

---

<div class="post-metadata">

### Author: ![fogmajor](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@fogmajor](https://discourse.nodered.org/u/fogmajor)
#### Post date: [17 September 2026 22:26 UTC](https://discourse.nodered.org/t/zwave-js-loop-help/101794/7 "2026-09-17T22:26:18Z")

</div>

Hi there  
Many thanks for taking the time trouble to answer my questions.

Best wishes

Andrew

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [18 September 2026 09:20 UTC](https://discourse.nodered.org/t/zwave-js-loop-help/101794/8 "2026-09-18T09:20:32Z")

</div>

Just a nice to know, if you use the property name as the object name, in javascript it will create the property names for you, eg:

```auto

const api = "VALUE"
const method = "setValue"

let value = (msg.payload.directive === "TurnOn" ? true : false)

const nodeId = [3, 5, 7]
const valueId = {
    "commandClass": 37,
    "endpoint": 0, 
    "property": "targetValue" 
}

const message = {
    payload: {
        cmd: { api, method },
        cmdProperties: { nodeId, valueId, value }
    }
}

return message

```

I am allergic to pascal case in javascript :')

---

<div class="post-metadata">

### Author: ![fogmajor](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@fogmajor](https://discourse.nodered.org/u/fogmajor)
#### Post date: [18 September 2026 10:23 UTC](https://discourse.nodered.org/t/zwave-js-loop-help/101794/9 "2026-09-18T10:23:28Z")

</div>

Thank you very much, its good to know.

Best Wishes  
Andrew
