# Joining messages with different intervals

**URL:** <https://discourse.nodered.org/t/joining-messages-with-different-intervals/41359>\
**Category:** General\
**Created:** [20 February 2021 18:43 UTC](https://discourse.nodered.org/t/joining-messages-with-different-intervals/41359 "2021-02-20T18:43:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaxSchaaper](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maxschaaper/32/37608_2.png) [@MaxSchaaper](https://discourse.nodered.org/u/MaxSchaaper)\
**Post date:** [20 February 2021 18:43 UTC](https://discourse.nodered.org/t/joining-messages-with-different-intervals/41359/1 "2021-02-20T18:43:34Z")

</div>

Hello,  
Using Node-Red to readout our smart- energy meter. Messages coming in through Mqtt.  
Electricity info comes in once every second. Gas usage info comes once every 5 minutes.  
I want to join only **the last** messages only once at the end of every day and insert them in a Sqlite database.  
My question is which nodes to use for joining the messages and pass only the last messages at a specific time in a combined message (Array) .  
Can someone put me on the right track?

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [20 February 2021 19:07 UTC](https://discourse.nodered.org/t/joining-messages-with-different-intervals/41359/2 "2021-02-20T19:07:10Z")

</div>

Hello Max,

I do something similar writing to a database but my values come from Modbus energy meters (The source in not so importand but the steps)

1. Use a Function node after each Mqtt node to save the latest value in Global or Flow Context.  
You can read more about the use of Context [here](https://nodered.org/docs/user-guide/writing-functions#storing-data)

2. Use a [Cron-plus](https://flows.nodered.org/node/node-red-contrib-cron-plus) schedule node to trigger another Function node that reads the latest values from Context and prepares the sql Insert query which in turn will be wired to the Sqlite node to write the data in the db.

If you share your flow maybe we can recommend any additional steps.

---

<div class="post-metadata">

**Author:** ![MaxSchaaper](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maxschaaper/32/37608_2.png) [@MaxSchaaper](https://discourse.nodered.org/u/MaxSchaaper)\
**Post date:** [20 February 2021 21:16 UTC](https://discourse.nodered.org/t/joining-messages-with-different-intervals/41359/3 "2021-02-20T21:16:38Z")

</div>

Thanks hou Andy. The Context function is new to me. I will study on it en let you know of the results in the coming week.

---

<div class="post-metadata">

**Author:** ![aderici](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/aderici/32/37279_2.png) [@aderici](https://discourse.nodered.org/u/aderici)\
**Post date:** [20 February 2021 21:39 UTC](https://discourse.nodered.org/t/joining-messages-with-different-intervals/41359/4 "2021-02-20T21:39:02Z")

</div>

I guess the values from the meters are incremental. You can also subtract the new value from the last and get a more detailed view of the consumption.

---

<div class="post-metadata">

**Author:** ![MaxSchaaper](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maxschaaper/32/37608_2.png) [@MaxSchaaper](https://discourse.nodered.org/u/MaxSchaaper)\
**Post date:** [23 February 2021 16:44 UTC](https://discourse.nodered.org/t/joining-messages-with-different-intervals/41359/5 "2021-02-23T16:44:49Z")

</div>

Yes. It's working fine now. Exactly how I wanted it. Thank you very much for guiding me.

---

<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:** [9 March 2021 16:44 UTC](https://discourse.nodered.org/t/joining-messages-with-different-intervals/41359/6 "2021-03-09T16:44:54Z")

</div>

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