Summing all msg.payload in a month

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

I am building UI for She-bear - 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:

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!

I would start with persistence: a database.
Something relatively simple like sqlite.

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

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.

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 :frowning:

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

Everything I know about InfluxDB I learned from Andreas Spiess:

1 Like

I use the so called TIG stack; (telegraf, influxdb and grafana),
Comprehensive tutorial for installation.

1 Like

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.

/*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?

Is the join node not helpful ?

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...

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

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:

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

I need an output message payload as object:
{Power_0: 123,
Power_1: 345,
Power_2: 567}

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

Great! A little more guidance, please :flushed:

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...

1 Like

image
Thank you. It works! :slight_smile:

1 Like