# Please if you can help with my flow

**URL:** <https://discourse.nodered.org/t/please-if-you-can-help-with-my-flow/65844>\
**Category:** General\
**Created:** [31 July 2022 12:53 UTC](https://discourse.nodered.org/t/please-if-you-can-help-with-my-flow/65844 "2022-07-31T12:53:22Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![murraa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/murraa/32/59354_2.png) [@murraa](https://discourse.nodered.org/u/murraa)\
**Post date:** [31 July 2022 12:53 UTC](https://discourse.nodered.org/t/please-if-you-can-help-with-my-flow/65844/1 "2022-07-31T12:53:22Z")

</div>

![screenshot](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/3/e3bcd8684f26b6889e9f91511b963efd00590d27.jpeg)

Hello everyone, please if you can help with my flow, I would like to put two conditions, if any devices are online state send mqtt message "off", if all devices have offline state send mqtt message "on"  
Thanks for your help and have a nice day

---

<div class="post-metadata">

**Author:** ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)\
**Post date:** [31 July 2022 13:01 UTC](https://discourse.nodered.org/t/please-if-you-can-help-with-my-flow/65844/2 "2022-07-31T13:01:52Z")

</div>

On this kind of community forum, the "custom" is to present what you have done and doesn't work as intended and not just saying "please, can you do that for me"...  
So, what have you tried so far?

---

<div class="post-metadata">

**Author:** ![murraa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/murraa/32/59354_2.png) [@murraa](https://discourse.nodered.org/u/murraa)\
**Post date:** [31 July 2022 13:08 UTC](https://discourse.nodered.org/t/please-if-you-can-help-with-my-flow/65844/3 "2022-07-31T13:08:53Z")

</div>

Here I got stuck, I don't have the necessary knowledge to write the code for function node, I tried with change node and switch node without success.

---

<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:** [31 July 2022 13:10 UTC](https://discourse.nodered.org/t/please-if-you-can-help-with-my-flow/65844/4 "2022-07-31T13:10:14Z")

</div>

you could use JSONata in change node  
set msg.payload  
to J:

```auto
"online" in $$.payload.*.state ? "off" : "on" 
// may need slight editing as i have not tested against your payload

```

should return 'off' if any state is online, and 'on' if all are offline

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 July 2022 13:11 UTC](https://discourse.nodered.org/t/please-if-you-can-help-with-my-flow/65844/5 "2022-07-31T13:11:22Z")

</div>

> [@murraa](#):
>
> if one or all devices have online state send mqtt message "off"

What if two devices are online?

---

<div class="post-metadata">

**Author:** ![murraa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/murraa/32/59354_2.png) [@murraa](https://discourse.nodered.org/u/murraa)\
**Post date:** [31 July 2022 13:12 UTC](https://discourse.nodered.org/t/please-if-you-can-help-with-my-flow/65844/6 "2022-07-31T13:12:34Z")

</div>

I asked for help and guidance, I did not expect the final result

---

<div class="post-metadata">

**Author:** ![murraa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/murraa/32/59354_2.png) [@murraa](https://discourse.nodered.org/u/murraa)\
**Post date:** [31 July 2022 13:14 UTC](https://discourse.nodered.org/t/please-if-you-can-help-with-my-flow/65844/7 "2022-07-31T13:14:50Z")

</div>

the message is "off",only if all devices is offline the message is "on".

Thank you.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 July 2022 13:16 UTC](https://discourse.nodered.org/t/please-if-you-can-help-with-my-flow/65844/8 "2022-07-31T13:16:30Z")

</div>

OK, so you meant 'if any devices are online' not 'if one or all devices are online`. I suggest you edit your initial post to make it clear.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 July 2022 13:25 UTC](https://discourse.nodered.org/t/please-if-you-can-help-with-my-flow/65844/9 "2022-07-31T13:25:07Z")

</div>

If you want to use a Function node that you can use Object.entries() to give you an array of key/value pairs, then you can iterate through that to test the values. Something like

```auto
for (const [key, value] of Object.entries(msg.payload)) {
  // key contains the key name (the IP address) and value contains the array
  // do the test here
}

```

---

<div class="post-metadata">

**Author:** ![murraa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/murraa/32/59354_2.png) [@murraa](https://discourse.nodered.org/u/murraa)\
**Post date:** [31 July 2022 13:25 UTC](https://discourse.nodered.org/t/please-if-you-can-help-with-my-flow/65844/10 "2022-07-31T13:25:38Z")

</div>

Thank you.

Have a nice day!

---

<div class="post-metadata">

**Author:** ![murraa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/murraa/32/59354_2.png) [@murraa](https://discourse.nodered.org/u/murraa)\
**Post date:** [31 July 2022 16:04 UTC](https://discourse.nodered.org/t/please-if-you-can-help-with-my-flow/65844/11 "2022-07-31T16:04:10Z")

</div>

![screenshot(1)](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/4/04aa0284aea518feae2bb6c21b459f4d1c8756b8.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [14 August 2022 16:05 UTC](https://discourse.nodered.org/t/please-if-you-can-help-with-my-flow/65844/12 "2022-08-14T16:05:08Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
