How to simulate (inject) full message object

#1

Two parts to this question, one is a theoretical question, the other a practical question.

I have the Dark Sky node plugged into a function, which uses a for-loop to build an array, which I can then output to a dashboard graph. It shows percent chance of rain per hour, for next 24 hours.

Function node:

var m={};
m.series = ['% rain'];
m.labels = [];
m.data = [[]];
for (i=0;i<=24;i++) {
    m.labels.push(i);
    m.data[0].push(msg.data.hourly.data[i].precipProbability);
}

Question 1 (practical)
As you can see, the X axis will display a count from 0 to 24. Instead I'd like it to display 4x labels, showing the hour of the day, e.g. 01:00, 07:00, 13:00, 19:00. These are provided in the incoming object:

msg.data.hourly.data[0].time
msg.data.hourly.data[6].time
msg.data.hourly.data[12].time
msg.data.hourly.data[18].time

I guess this question is in two parts: how to format the time, and how to display only 4 labels when there are 24 data points.

Re the first point, I have tried to push this into the m.labels array using .toTimeString(), i.e. m.labels.push(msg.data.hourly.data[i].time.toTimeString()); but the graph still shows the long unix timestamp in the X axis label. (When I hover over the graph it says "invalid date format".) I also tried changing the X axis label in the graph node, but that didn't work. How can I achieve that?

Question 2 (theoretical)
Let's say I don't want to keep using up the API limit of Dark Sky, every time I test my code, so instead I copy the entire message object output of the Dark Sky node into the clipboard (from the debug sidebar), then enter it into an inject node as JSON. After I clear the values in the graph, and unwire it from the Dark Sky node and re-wire it into my inject node... when I inject it, the graph doesn't display.

I think this is because I've copied the whole message object into the inject node (JSON), and this includes e.g. even the topic. So I see why that wouldn't work.

But theoretically, how would I "simulate" the entire output of this Dark Sky node - or any other node for that matter?

(I say it's theoretical, because the API limit for Dark Sky is massive - I'm just curious)

Thanks

0 Likes

#2

The issue is that the inject node will give you msg.payload = object

You are more likely to suceeed with this strategy if you paste the result of your copy in a function node, like this:

msg = {

paste your object
}

return msg;
1 Like

#3

Thanks @Andrei

For reference I was able to resolve my coding problem. I didn't understand that the unix timestamp stored in the array actually wasn't a date object, so had to create a new date object. Here's what I did for anyone wanting to do the same. In honesty, not sure why I had to multiply the value by 1000 but I saw this done elsewhere online and when I compared the dates, it worked.

var time = new Date(msg.data.hourly.data[i].time * 1000);
var hour = time.getHours();
var displayhour = hour + ":00h";

m.labels.push(displayhour);
m.data[0].push((msg.data.hourly.data[i].precipProbability)*100);
0 Likes