# Help with creating a dynamic list of tripped sensors

**URL:** <https://discourse.nodered.org/t/help-with-creating-a-dynamic-list-of-tripped-sensors/43805>\
**Category:** General\
**Created:** [7 April 2021 09:58 UTC](https://discourse.nodered.org/t/help-with-creating-a-dynamic-list-of-tripped-sensors/43805 "2021-04-07T09:58:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gadget](https://avatars.discourse-cdn.com/v4/letter/g/dec6dc/32.png) [@gadget](https://discourse.nodered.org/u/gadget)\
**Post date:** [7 April 2021 09:58 UTC](https://discourse.nodered.org/t/help-with-creating-a-dynamic-list-of-tripped-sensors/43805/1 "2021-04-07T09:58:58Z")

</div>

I'm attempting to create a list of contact sensors which are in the open state.

For example I have a node which returns the state of sensor XYZ. So if the payload is "open", the text "XYZ" is added to the string/list which I can do (each addition is delineated with a new line).

Conversely, if the state of XYZ changes to closed I want to be able to search for and delete "XYZ" from the string, and this part I'm not sure how to to achieve.

Any help/pointers gratefully received! String manipulation unfortunately is not one of my coding strengths.

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [7 April 2021 10:56 UTC](https://discourse.nodered.org/t/help-with-creating-a-dynamic-list-of-tripped-sensors/43805/2 "2021-04-07T10:56:21Z")

</div>

It would be easier to give some advice if you tell us how the data structure looks like. I assume the list is an array but if you decided to store data in an object then the code to achieve what you want would be different.

---

<div class="post-metadata">

**Author:** ![gadget](https://avatars.discourse-cdn.com/v4/letter/g/dec6dc/32.png) [@gadget](https://discourse.nodered.org/u/gadget)\
**Post date:** [7 April 2021 11:50 UTC](https://discourse.nodered.org/t/help-with-creating-a-dynamic-list-of-tripped-sensors/43805/3 "2021-04-07T11:50:08Z")

</div>

I see, thanks. To be honest I haven't given thought to the structure of the list. The intention is to be able to export a list of what sensors are "open" at any one time in the simplest possible way. The number of devices that would likely be populating the array/list at any time would be small (10 at most).

I'll give it some more thought to flesh it out further.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [7 April 2021 12:16 UTC](https://discourse.nodered.org/t/help-with-creating-a-dynamic-list-of-tripped-sensors/43805/4 "2021-04-07T12:16:15Z")

</div>

> [@gadget](#):
>
> Any help/pointers gratefully received! String manipulation unfortunately is not one of my coding strengths.

That is not the ideal way to achieve what you want.

Ideally you would store the state of the item and then build a string based on the state.

e.g. you could store the sensors values an array like this...

```auto
[
  { "name": "sensor 1", "state": false },
  { "name": "sensor 2", "state": true },
  { "name": "sensor 3", "state": true }, 
]

```

or an object like this...

```auto
{
 "Sensor 1": false,
 "Sensor 2": true,
 "Sensor 3": true
}

```

With an array, you could build a status string like this...

```auto
var offSensors = sensors.filter(e => e.state == false).map(e=>e.name).join(",");
//returns 'sensor 1'

var onSensors = sensors.filter(e => e.state == true).map(e=>e.name).join(",");
//returns 'sensor 2,sensor 3'

```

**However** , to update a value in the array is a bit harder (much easier with an object) so that is what i will demo...

  

### Store sensors states in an object...

#### Premise...

Store a single value for each sensor named by `msg.topic` in an object named `sensors` in a flow context variable.

- When updating a state in flow context, `msg.topic` must hold the sensor name and `msg.payload` must have the sensor value
- Demo assumes `msg.payload` will be boolean (true or false)

Example data...

```auto
{
 "Sensor 1": false,
 "Sensor 2": true,
 "Sensor 3": true
}

```

//adding / updating a sensors value

```auto
var sensors = flow.get("sensors") || {}; //get the sensors state from flow context
var sensorName = msg.topic; //get sensor name from incoming msg
sensors[sensorName] = msg.payload; //store value under a key in the sensors object 
flow.set("sensors", sensors); //update the flow context

```

getting a string of on or off sensors...

```auto
var sensors = flow.get("sensors") || {}; //get the sensors state from flow context

msg.payload = {}; //prepare the payload as an object, ready for the off and on strings

msg.payload.offSensors = Object.entries(sensors).filter(e => e[1] == false).map(e=>e[0]).join(",");
//return example 'sensor 1'

msg.payload.onSensors = Object.entries(sensors).filter(e => e[1] == true).map(e=>e[0]).join(",");
//return example 'sensor 2,sensor 3'

return msg;

```

---

<div class="post-metadata">

**Author:** ![gadget](https://avatars.discourse-cdn.com/v4/letter/g/dec6dc/32.png) [@gadget](https://discourse.nodered.org/u/gadget)\
**Post date:** [7 April 2021 12:23 UTC](https://discourse.nodered.org/t/help-with-creating-a-dynamic-list-of-tripped-sensors/43805/5 "2021-04-07T12:23:27Z")

</div>

Wow - thanks for that, it will keep me occupied for a fair few days working through it!

---

<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:** [7 April 2021 13:56 UTC](https://discourse.nodered.org/t/help-with-creating-a-dynamic-list-of-tripped-sensors/43805/6 "2021-04-07T13:56:25Z")

</div>

> [@gadget](#):
>
> The intention is to be able to export a list of what sensors are "open" at any one time in the simplest possible way.

What is it being exported to and what use will then be made of it? The answer to that will influence which way is best.

---

<div class="post-metadata">

**Author:** ![gadget](https://avatars.discourse-cdn.com/v4/letter/g/dec6dc/32.png) [@gadget](https://discourse.nodered.org/u/gadget)\
**Post date:** [8 April 2021 14:15 UTC](https://discourse.nodered.org/t/help-with-creating-a-dynamic-list-of-tripped-sensors/43805/7 "2021-04-08T14:15:08Z")

</div>

Thanks Steve - that code works great!

@Colin, @Andrei FYI the output is being sent to my hubitat hub as a list of open contact sensors for display on a tablet dashboard. As display space is at a premium I wanted to just show a list of open sensors rather than create a whole bunch of device tiles to show status (most of them will be closed at any one time anyway)

---

<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:** [22 April 2021 14:15 UTC](https://discourse.nodered.org/t/help-with-creating-a-dynamic-list-of-tripped-sensors/43805/8 "2021-04-22T14:15:31Z")

</div>

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