# Join messages from individual ''sensors'' into one json object

**URL:** <https://discourse.nodered.org/t/join-messages-from-individual-sensors-into-one-json-object/32184>\
**Category:** Developing Nodes\
**Created:** [29 August 2020 12:56 UTC](https://discourse.nodered.org/t/join-messages-from-individual-sensors-into-one-json-object/32184 "2020-08-29T12:56:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![RoSi](https://avatars.discourse-cdn.com/v4/letter/r/ba9def/32.png) [@RoSi](https://discourse.nodered.org/u/RoSi)\
**Post date:** [29 August 2020 12:56 UTC](https://discourse.nodered.org/t/join-messages-from-individual-sensors-into-one-json-object/32184/1 "2020-08-29T12:56:38Z")

</div>

Let's imagine that I am having two inject nodes to simulate two temperature sensors. I'd like to join them into one json object as follows:

```auto
{
   "sensor1":22,
   "sensor2":21
}

```

and to update the object if any one value changes. That means, i might get 5 messages from sensor2, but none for sensor1.

---

<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:** [29 August 2020 13:07 UTC](https://discourse.nodered.org/t/join-messages-from-individual-sensors-into-one-json-object/32184/2 "2020-08-29T13:07:19Z")

</div>

Set a topic for each inject, connect them to a join node set to manual mode, count of 2 & key/value output --\> debug node.

Play with the join node settings - you will soon understand what it does.

---

<div class="post-metadata">

**Author:** ![RoSi](https://avatars.discourse-cdn.com/v4/letter/r/ba9def/32.png) [@RoSi](https://discourse.nodered.org/u/RoSi)\
**Post date:** [29 August 2020 13:14 UTC](https://discourse.nodered.org/t/join-messages-from-individual-sensors-into-one-json-object/32184/3 "2020-08-29T13:14:55Z")

</div>

Great. I didn't played with join mode enough to figure it by myself. Setting the topic for each sensor was the key.

Got this working! **Thank you.**

Problem now is, that when I restart Node-Red, I need to get both messages to generate the object. Is there some workaround please?

---

<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:** [29 August 2020 13:33 UTC](https://discourse.nodered.org/t/join-messages-from-individual-sensors-into-one-json-object/32184/4 "2020-08-29T13:33:30Z")

</div>

where do they come from - MQTT? Set the topics to retained & then when you restart & re-connect, they will be resent.

Alternatively, you will have to look into persistent storage (storing value to file and recover on startup)

---

<div class="post-metadata">

**Author:** ![RoSi](https://avatars.discourse-cdn.com/v4/letter/r/ba9def/32.png) [@RoSi](https://discourse.nodered.org/u/RoSi)\
**Post date:** [29 August 2020 13:38 UTC](https://discourse.nodered.org/t/join-messages-from-individual-sensors-into-one-json-object/32184/5 "2020-08-29T13:38:11Z")

</div>

All makes sense now. I might use MQTT to transfer the values, to simplifying it.  
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:** [29 August 2020 14:03 UTC](https://discourse.nodered.org/t/join-messages-from-individual-sensors-into-one-json-object/32184/6 "2020-08-29T14:03:37Z")

</div>

> [@RoSi](#):
>
> I need to get both messages to generate the object

If you set the join node to require just 1 value instead of 2 then it will give you a message after the first one, containing only that value obviously.  
I presume you have already selected And After Every Subsequent so that you get a new message even if only one of them is sent after you get going.

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [29 August 2020 19:56 UTC](https://discourse.nodered.org/t/join-messages-from-individual-sensors-into-one-json-object/32184/7 "2020-08-29T19:56:36Z")

</div>

I went the hardcore route... all my remote sensors send back MQTT messages on schedule or demand, these are parsed/mapped into a DB table via insert SQL, and then... I use DB query to pull consolidated data. If you get fancy you can even use a stored procedure to do the data merge as you want, so NR just pulls the resulting data set and displays it. Since I already had the MQTT messaging setup in python, and database as the consolidation point for the raw data, I did not need NR to do the bulk of the work. I still find, that as newer sensors are released, it is easier to incorporate them into python than using i2c nodes directly (if applicable). Even the Pi dtoverly methods for supporting sensors have no 'clean' bridge to NR at this time (at least that I have found), sure using exec nodes or such works, but when you can have almost any remote device send MQTT back to NR, not using that is sometimes like ice-skating uphill (sorry for the Blade reference, just seemed applicable). 🙂

---

<div class="post-metadata">

**Author:** ![RoSi](https://avatars.discourse-cdn.com/v4/letter/r/ba9def/32.png) [@RoSi](https://discourse.nodered.org/u/RoSi)\
**Post date:** [31 August 2020 05:17 UTC](https://discourse.nodered.org/t/join-messages-from-individual-sensors-into-one-json-object/32184/8 "2020-08-31T05:17:13Z")

</div>

I agree, this is hardcore! 😅

I moved forward littlebit with my "problem" and got now joining messages as follow:

```auto
[{"id":"c14ed17f.70b7a","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"a2ff22c.40424e","type":"inject","z":"c14ed17f.70b7a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"main_time","payload":"","payloadType":"date","x":220,"y":120,"wires":[["21aeb3df.901b6c"]]},{"id":"3b443659.2f5a5a","type":"inject","z":"c14ed17f.70b7a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"bedroom_state","payload":"0","payloadType":"num","x":200,"y":280,"wires":[["96e0dcfd.3a082"]]},{"id":"7d05e6e.6f34e18","type":"inject","z":"c14ed17f.70b7a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"main_state","payload":"1","payloadType":"num","x":190,"y":160,"wires":[["21aeb3df.901b6c"]]},{"id":"c9266feb.9b0cb","type":"inject","z":"c14ed17f.70b7a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"bedroom_time","payload":"","payloadType":"date","x":230,"y":240,"wires":[["96e0dcfd.3a082"]]},{"id":"21aeb3df.901b6c","type":"join","z":"c14ed17f.70b7a","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":470,"y":160,"wires":[["faf7569b.77cd18"]]},{"id":"96e0dcfd.3a082","type":"join","z":"c14ed17f.70b7a","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":470,"y":240,"wires":[["faf7569b.77cd18"]]},{"id":"faf7569b.77cd18","type":"join","z":"c14ed17f.70b7a","name":"","mode":"custom","build":"merged","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":630,"y":200,"wires":[["2bbfd490.2511dc"]]},{"id":"2bbfd490.2511dc","type":"debug","z":"c14ed17f.70b7a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":820,"y":200,"wires":[]}]

```

it produces JSON object:

```auto
{
   "main_state":{
      "main_time":1598850426209,
      "main_state":1
   },
   "bedroom_state":{
      "bedroom_time":1598850427518,
      "bedroom_state":0
   }
}

```

but what I would like to do is:

```auto
{
   "main":{
      "time":1598850426209,
      "state":1
   },
   "bedroom":{
      "time":1598850427518,
      "state":0
   }
}

```

Is it possible please?

---

<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 August 2020 07:54 UTC](https://discourse.nodered.org/t/join-messages-from-individual-sensors-into-one-json-object/32184/9 "2020-08-31T07:54:13Z")

</div>

Can you change the topics and attribute names in the data coming in?  
Otherwise you can use a change node to do it. Set it to Move msg.payload.main\_state To msg.payload.main, Move msg.payload.main.main\_time To msg.payload.main.time and so on. Alternatively you could write some javascript to iterate the keys in msg.payload and change all the xxx\_state to xxx and all the sub attributes xxx\_time to time and xxx\_state to state. Not particularly difficult but would need a certain level of js experience.

---

<div class="post-metadata">

**Author:** ![RoSi](https://avatars.discourse-cdn.com/v4/letter/r/ba9def/32.png) [@RoSi](https://discourse.nodered.org/u/RoSi)\
**Post date:** [31 August 2020 13:17 UTC](https://discourse.nodered.org/t/join-messages-from-individual-sensors-into-one-json-object/32184/10 "2020-08-31T13:17:51Z")

</div>

OMG. Thanks to all so far for help.

Somehow, and don't ask me how, I got this.

```auto
[{"id":"1414279f.2ef558","type":"key-value-read","z":"c14ed17f.70b7a","store":"92d5edb.a2aa81","key":"main_timestamp","name":"main_timestamp","x":380,"y":400,"wires":[["b1aae43c.24e418"]]},{"id":"92d5edb.a2aa81","type":"key-value-store","z":"","filepath":"doors.json","namespace":"","name":""}]

```

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

The goal is to produce JSON object for 2 doors. Main and bedroom (for example). For every state change it needs to store a timestamp and be persietent to node-red restarts. I am using _node-red-contrib-persist_.

Final JSON looks as follows:

```auto
{
   "main":[
      1598873690,
      1
   ],
   "bedroom":[
      1598873688,
      1
   ]
}

```

which I am more than happy with. The only problem is, that it takes two refreshes of /endpoint to read changed values.

Any idea someone how to make it to just one refresh only please? I know, it's a mess.

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [31 August 2020 14:29 UTC](https://discourse.nodered.org/t/join-messages-from-individual-sensors-into-one-json-object/32184/11 "2020-08-31T14:29:55Z")

</div>

I have been playing around with json-db as well for persistent state stuff.

---

<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 August 2020 14:33 UTC](https://discourse.nodered.org/t/join-messages-from-individual-sensors-into-one-json-object/32184/12 "2020-08-31T14:33:52Z")

</div>

> [@RoSi](#):
>
> _node-red-contrib-persist_ .

Does that have any advantages over the persistent context provided by node red core?  
[https://nodered.org/docs/user-guide/context](https://nodered.org/docs/user-guide/context)

---

<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 August 2020 14:49 UTC](https://discourse.nodered.org/t/join-messages-from-individual-sensors-into-one-json-object/32184/13 "2020-08-31T14:49:51Z")

</div>

> [@Nodi.Rubrum](#):
>
> I have been playing around with json-db

The same question there, does that have advantages over the core persistent context?  
I notice it is nearly three years since it was last updated. Also I note that the readme instructions to install it will actually install json-db-node-red which is a different node. There doesn't appear to be an Issues tab for the node so one cannot submit an issue.

**[Edit]** Although it isn't clear which one you are actually playing with, json-db-node-red or node-red-contrib-json-db.

---

<div class="post-metadata">

**Author:** ![RoSi](https://avatars.discourse-cdn.com/v4/letter/r/ba9def/32.png) [@RoSi](https://discourse.nodered.org/u/RoSi)\
**Post date:** [31 August 2020 14:58 UTC](https://discourse.nodered.org/t/join-messages-from-individual-sensors-into-one-json-object/32184/14 "2020-08-31T14:58:02Z")

</div>

I am not aware of any advantages, but it's easier for me to use. It's this one actually: [https://flows.nodered.org/node/node-red-contrib-persist](https://flows.nodered.org/node/node-red-contrib-persist)

But I guess, to use context will be more efficient to use without external nodes.

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [31 August 2020 17:58 UTC](https://discourse.nodered.org/t/join-messages-from-individual-sensors-into-one-json-object/32184/15 "2020-08-31T17:58:50Z")

</div>

Json-db has a nice tree structure for organization of variable context. Thus, the original use was to keep context variables persistent, as variables, not a data structure. This fit the current flow I had in place. But I decided to improve the flow, so the state of flow is now an object structure, i.e. a single variable (which is a complex data structure). This new design provides more flexibility when it comes to saving the entire datastructure as persistent.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [31 August 2020 18:18 UTC](https://discourse.nodered.org/t/join-messages-from-individual-sensors-into-one-json-object/32184/16 "2020-08-31T18:18:54Z")

</div>

Can you share an example of the flexibility it gives over what is available with the built-in context?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [31 August 2020 19:17 UTC](https://discourse.nodered.org/t/join-messages-from-individual-sensors-into-one-json-object/32184/17 "2020-08-31T19:17:23Z")

</div>

You can save objects in context storage. Here is an example I use:

 ![Screen Shot 2020-08-31 at 3.15.05 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/9/d9865df3545e223db6d08c7edf31b6fcdb04d4ce.png)

Each of these objects represents a different water station - a WeMos D1 Pro connected to some sensors and a battery with a solar panel to recharge it.

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [31 August 2020 19:34 UTC](https://discourse.nodered.org/t/join-messages-from-individual-sensors-into-one-json-object/32184/18 "2020-08-31T19:34:59Z")

</div>

I am not sure what you mean by flexibility, the current context model is flexible. I am using json-db to provide persistence. Just so happens json-db can let you structure data anyway you want just like context does.

---

<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 August 2020 20:02 UTC](https://discourse.nodered.org/t/join-messages-from-individual-sensors-into-one-json-object/32184/19 "2020-08-31T20:02:18Z")

</div>

> [@Nodi.Rubrum](#):
>
> I am using json-db to provide persistence.

The built in context also provides persistence when required.

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [31 August 2020 20:05 UTC](https://discourse.nodered.org/t/join-messages-from-individual-sensors-into-one-json-object/32184/20 "2020-08-31T20:05:33Z")

</div>

Like I said, I was playing around with it. The one draw back with it... the actual file created does not seem to be human readable. The test file I looked at, was not actually a json formatted file. Which seemed odd. Maybe somehow I broke it? LOL.

[Next page](https://discourse.nodered.org/t/join-messages-from-individual-sensors-into-one-json-object/32184.md?page=2)
