# Summing all msg.payload in a month

**URL:** <https://discourse.nodered.org/t/summing-all-msg-payload-in-a-month/13723>\
**Category:** General\
**Created:** [26 July 2019 16:00 UTC](https://discourse.nodered.org/t/summing-all-msg-payload-in-a-month/13723 "2019-07-26T16:00:25Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![igrowing](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/igrowing/32/663_2.png) [@igrowing](https://discourse.nodered.org/u/igrowing)\
**Post date:** [26 July 2019 16:00 UTC](https://discourse.nodered.org/t/summing-all-msg-payload-in-a-month/13723/1 "2019-07-26T16:00:25Z")

</div>

Hi guys, not sure "General" is correct forum branch, maybe "Dashboard" fits better for the question.

I am building UI for [She-bear](https://github.com/igrowing/esp8266_smart_home_ready/tree/master/modules/schieber) - a monitor and sentinel of house water consumption. Of course, I'll post stable version in "Share your project" eventually.

Meanwhile, I need to sum consumption (in liters), coming with MQTT, with dedicated topic 'liters'. The sum is intended to be presented in monthly chart. So far, I reviewed:

- Forum threads: [Adding 2 Msg.payloads numbers](https://discourse.nodered.org/t/adding-2-msg-payloads-numbers/5251) and [2 msg.topic in calculation function node](https://discourse.nodered.org/t/2-msg-topic-in-calculation-function-node/4526) - not much relevant to long-playing case.
- Found 2 very relevant nodes: `Bar-chart-data` and `dashboard-sum-bars`. They are almost perfect to the purpose, except in my case the MQTT messages don't come periodically. Water consumed in random times.

I am looking for a node or flow example which can:

- Set a time frame (month begin and end);
- Receive messages in the time frame and sum their values;
- Reset the sum on next time frame;
- Persist the data.

I could write such flow in NR. Just checking maybe i can save the effort: nothing is new under the sun...

Thank you!

---

<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:** [26 July 2019 23:14 UTC](https://discourse.nodered.org/t/summing-all-msg-payload-in-a-month/13723/2 "2019-07-26T23:14:11Z")

</div>

I would start with persistence: a database.  
Something relatively simple like [sqlite](https://flows.nodered.org/node/node-red-node-sqlite).

Store the MQTT data in there. Once that is working, dashboard is next.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [27 July 2019 16:40 UTC](https://discourse.nodered.org/t/summing-all-msg-payload-in-a-month/13723/3 "2019-07-27T16:40:58Z")

</div>

> [@bakman2](#):
>
> I would start with persistence: a database.

Definitely, but you should also consider InfluxDB. Either will give you lots of ways to "slice and dice" the data before displaying it on a dashboard. You won't be restricted to a fixed time frame such as the calendar month. You can get a total for the past 30 days, or the monthly average for the past year, or a comparison of today with a week ago today, and so on and on. Fun.

---

<div class="post-metadata">

**Author:** ![igrowing](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/igrowing/32/663_2.png) [@igrowing](https://discourse.nodered.org/u/igrowing)\
**Post date:** [27 July 2019 16:50 UTC](https://discourse.nodered.org/t/summing-all-msg-payload-in-a-month/13723/4 "2019-07-27T16:50:49Z")

</div>

> [@drmibell](#):
>
> you should also consider InfluxDB

Got it. Struggling since yesterday to couple InluxDB -\> Grafana -\> template UI.  
No problems with grafana. Tons of problems with influx. Solved influx not opening port 8086 (many people complain on the Internet). Next barrier is create user. Then create database, then create table or so...  
It goes hard ☹

A link to reasonable step-by-step guide/tutorial to Influx is welcome. DDG and Google brought tons of junk...

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [27 July 2019 17:07 UTC](https://discourse.nodered.org/t/summing-all-msg-payload-in-a-month/13723/6 "2019-07-27T17:07:06Z")

</div>

> [@igrowing](#):
>
> reasonable step-by-step guide/tutorial

Everything I know about InfluxDB I learned from Andreas Spiess:

[![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/c/0c143ce2ddb01dcba7e073a8b86772bf6b6e35dd.jpeg "#255 Node-Red, InfluxDB, and Grafana Tutorial on a Raspberry Pi") ](https://www.youtube.com/watch?v=JdV4x925au0)

---

<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:** [27 July 2019 18:32 UTC](https://discourse.nodered.org/t/summing-all-msg-payload-in-a-month/13723/7 "2019-07-27T18:32:04Z")

</div>

I use the so called TIG stack; (telegraf, influxdb and grafana),  
[Comprehensive tutorial](https://angristan.xyz/monitoring-telegraf-influxdb-grafana/#thetigstack) for installation.

---

<div class="post-metadata">

**Author:** ![igrowing](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/igrowing/32/663_2.png) [@igrowing](https://discourse.nodered.org/u/igrowing)\
**Post date:** [3 August 2019 15:12 UTC](https://discourse.nodered.org/t/summing-all-msg-payload-in-a-month/13723/8 "2019-08-03T15:12:58Z")

</div>

Please, help with programming.

InfluxDB requires all fields and values as msg.payload object. So I need to gather few messages with different topics and values into one object. I try to do that using context.

```auto
/*Input: msg.topic="Phase 0", msg.payload="123"*/
var num_params = 3;

context.set(msg.topic.replace(" ", "_"), parseInt(msg.payload)); // here is the problem: Key is the number, not a topic.
if (context.keys().length >= num_params) {
    msg.payload = {};
    for (var key in context.keys()) { // here is the problem: Key is the number, not a topic.
        msg.payload[key] = context[key]; // invoke the key
        delete context[key]; // clear the key for next round
    }
    node.status({fill:"green",shape:"ring",text: 'Sent:'+msg.payload});
    return msg;
}
// For debug:
var obj = {};
for (var key in context.keys()) obj[key] = context[key];
node.status({fill:"blue",shape:"dot",text: context.keys().length+ JSON.stringify(obj)}+msg.topic.replace(" ", "_"));

```

How do I keep in context an attribute named as msg.topic?  
How to invoke it later and how to clear it?

---

<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:** [3 August 2019 16:54 UTC](https://discourse.nodered.org/t/summing-all-msg-payload-in-a-month/13723/9 "2019-08-03T16:54:49Z")

</div>

Is the join node not helpful ?

---

<div class="post-metadata">

**Author:** ![igrowing](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/igrowing/32/663_2.png) [@igrowing](https://discourse.nodered.org/u/igrowing)\
**Post date:** [3 August 2019 17:15 UTC](https://discourse.nodered.org/t/summing-all-msg-payload-in-a-month/13723/10 "2019-08-03T17:15:36Z")

</div>

Started with Join, of course. It doesn't do the job of connecting several messages with topics and payload into single object {topic1: payload1,topic2: payload3,topic3: payload3... }  
Or I didn't understand hot to set the node...

---

<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:** [3 August 2019 17:41 UTC](https://discourse.nodered.org/t/summing-all-msg-payload-in-a-month/13723/11 "2019-08-03T17:41:10Z")

</div>

Can you give some example input (some more than in the function above).

---

<div class="post-metadata">

**Author:** ![igrowing](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/igrowing/32/663_2.png) [@igrowing](https://discourse.nodered.org/u/igrowing)\
**Post date:** [3 August 2019 17:54 UTC](https://discourse.nodered.org/t/summing-all-msg-payload-in-a-month/13723/12 "2019-08-03T17:54:00Z")

</div>

messages arrive with topics and payloads: (topic: payload)  
Power 0: 123  
Power 1: 345  
Power 2: 567  
etc.  
Copy from debug window of one such message:

```auto
object
  topic: "Phase 1"
  payload: "7058"

```

I need an output message payload as object:  
{Power\_0: 123,  
Power\_1: 345,  
Power\_2: 567}

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [3 August 2019 17:58 UTC](https://discourse.nodered.org/t/summing-all-msg-payload-in-a-month/13723/13 "2019-08-03T17:58:22Z")

</div>

Yes the join node in manual mode should be able to do that

---

<div class="post-metadata">

**Author:** ![igrowing](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/igrowing/32/663_2.png) [@igrowing](https://discourse.nodered.org/u/igrowing)\
**Post date:** [3 August 2019 18:01 UTC](https://discourse.nodered.org/t/summing-all-msg-payload-in-a-month/13723/14 "2019-08-03T18:01:04Z")

</div>

Great! A little more guidance, please 😳

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [3 August 2019 18:52 UTC](https://discourse.nodered.org/t/summing-all-msg-payload-in-a-month/13723/15 "2019-08-03T18:52:26Z")

</div>

well you want an object using msg.topic as the key for each property of that object so set it to manually join to create a key/value object using msg.topic as the key...

---

<div class="post-metadata">

**Author:** ![igrowing](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/igrowing/32/663_2.png) [@igrowing](https://discourse.nodered.org/u/igrowing)\
**Post date:** [4 August 2019 05:53 UTC](https://discourse.nodered.org/t/summing-all-msg-payload-in-a-month/13723/16 "2019-08-04T05:53:46Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/12c03ebb798e76e5673be797acbda6eeb90e24ab.png)  
Thank you. It works! 🙂
