Node-Red OpenWeatherMap

735 views
Skip to first unread message

Cristiano Nunes

unread,
Dec 26, 2017, 9:49:31 AM12/26/17
to Node-RED

Hello, Since I apologize, I'm new to JS. I'm using openweather and posting the data to my "ui". I'm having some trouble trying to convert the time from sunrise and sunset to string.~

This is my code to extract the data i need, a function with 13 outputs.

//Variaveis 
var msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8,msg9,msg10,msg11,msg12,msg13 = {};

//Msg
msg1 = {payload: msg.payload.weather};
msg2 = {payload: msg.payload.detail};
msg3 = {payload: msg.payload.icon};
msg4 = {payload: msg.payload.tempc};
msg5 = {payload: msg.payload.humidity};
msg6 = {payload: msg.payload.windspeed};
msg7 = {payload: msg.payload.winddirection};
msg8 = {payload: msg.payload.location};
msg9 = {payload: msg.payload.sunrise};
msg10 = {payload: msg.payload.sunset};
msg11 = {payload: msg.payload.clouds};
msg12 = {payload: msg.data.visibility};
msg13 = {payload: msg.data.main.pressure};

// Update the status with current timestamp
var now = new Date();
var yyyy = now.getFullYear();
var mm = now.getMonth() < 9 ? "0" + (now.getMonth() + 1) : (now.getMonth() + 1); // getMonth() is zero-based
var dd  = now.getDate() < 10 ? "0" + now.getDate() : now.getDate();
var hh = now.getHours() < 10 ? "0" + now.getHours() : now.getHours();
var mmm  = now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes();
var ss  = now.getSeconds() < 10 ? "0" + now.getSeconds() : now.getSeconds();
node.status({fill:"blue",shape:"ring",text:"Last update: "+dd + "." + mm + "." + yyyy + " " + hh + ":" + mmm + ":" + ss}); 

return [msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8,msg9,msg10,msg11,msg12,msg13];

UI





Thanks.

Julian Knight

unread,
Dec 26, 2017, 11:46:16 AM12/26/17
to Node-RED
You haven't shared what is happening and what the problem is. I suspect this may be related to a question that cropped up recently about data in UNIX timestamp format? But I may not be remembering right.

In regard to your status string, you might consider using the OWM last update date rather than now since that will tell you how old the data is that you are currently seeing.

Cristiano Nunes

unread,
Dec 26, 2017, 12:06:12 PM12/26/17
to Node-RED
Omg...Sorry, I completely forgot.

Yes it is the problem of time conversion. when I try to convert "msg.payload.sunrise", for string is generated a conversion error for string.

But if I use a new function, with the code below it works correctly
var teste = new Date(msg.payload.sunrise);
msg
.payload = teste.toUTCString();

return msg;



Cristiano Nunes

unread,
Dec 27, 2017, 4:14:57 PM12/27/17
to Node-RED
anyone ? please

thanks 
 

Colin Law

unread,
Dec 27, 2017, 4:39:42 PM12/27/17
to node...@googlegroups.com
What is in msg.payload.sunrise (exactly) and how are you trying to convert it?

Colin

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/4aa38bc6-b39d-46d9-a14f-3beb4cab9534%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

steve rickus

unread,
Dec 27, 2017, 6:21:00 PM12/27/17
to Node-RED
Christiano,

It appears that the sunrise/sunset values are timestamps in unix epoch format (seconds since 1970-01-01)

Javascript dates use an internal format of milliseconds since the epoch -- so you will need to multiply by 1000,
and then you can get the local time from that js date object:

var dtm = new Date(msg.payload.sunrise * 1000); // sunrise ms value is 1514308422000
var str = dtm.getLocaleTimeString(); // sunrise time is '12:13:42 PM' in my timezone (ymmv)

Generally, it's best to pass the internal js timestamp (or an actual date object) in the msg.payload. This way,
downstream nodes will not have to parse any dates or convert timezones from the date string. The UI can then
convert that timestamp into whatever format needed for display. Generally, the dashboard nodes will do that
automatically, using the current browser's timezone setting. But when I need to do some other date/time formatting,
I usually wire in a "node-red-contrib-moment" node, and just tell it what output format I want, including the "humanized"
forms like "about 2 days ago" or "3 and a half hours from now".
--
Steve

Cristiano Nunes

unread,
Dec 28, 2017, 6:48:13 AM12/28/17
to Node-RED
hello

Thanks for help me.

The message I'm trying to convert:


and i try use this code to convert to string:

//Variaveis
var msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8,msg9,msg10,msg11,msg12,msg13 = {};

//Msg
msg1 = {payload: msg.payload.weather};
msg2 = {payload: msg.payload.detail};
msg3 = {payload: msg.payload.icon};
msg4 = {payload: msg.payload.tempc};
msg5 = {payload: msg.payload.humidity};
msg6 = {payload: msg.payload.windspeed};
msg7 = {payload: msg.payload.winddirection};
msg8 = {payload: msg.payload.location};
var teste ={payload: msg.payload.sunrise};
msg9 = new Date(teste).toString();
//msg9 = {payload: msg.payload.sunrise};
msg10 = {payload: msg.payload.sunset};
msg11 = {payload: msg.payload.clouds};
msg12 = {payload: msg.data.visibility};
msg13 = {payload: msg.data.main.pressure};

return [msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8,msg9,msg10,msg11,msg12,msg13];


Colin Law

unread,
Dec 28, 2017, 6:55:02 AM12/28/17
to node...@googlegroups.com
You have

var teste ={payload: msg.payload.sunrise};
which sets teste to an object with a property payload set to sunrise. Then you pass this to new Date() and not surprisingly it does not know what to do with it.  I think what you want is
var teste = msg.payload.sunrise;
which sets teste to the sunrise value.

Colin


--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

GeertVc

unread,
Dec 28, 2017, 10:27:46 AM12/28/17
to Node-RED

Can you try the following:

var teste = new Date(0);
msg.payload = teste.setUTCSeconds(msg.payload.sunrise);
return msg;


On Wednesday, December 27, 2017 at 10:14:57 PM UTC+1, Cristiano Nunes wrote:
anyone ? please

thanks 
 
Message has been deleted

GeertVc

unread,
Dec 28, 2017, 10:37:02 AM12/28/17
to Node-RED
Sorry, should be:

var teste = new Date(0);
teste.setUTCSeconds(msg.payload.sunrise);
msg.payload = teste;
return msg;

Cristiano Nunes

unread,
Jan 7, 2018, 9:33:43 PM1/7/18
to Node-RED
its solve :) i use moment node.


thanks for the help :)


Walter

unread,
Jan 9, 2018, 4:51:51 PM1/9/18
to Node-RED
For most text fields you could just feed them the whole weather info and let the text field pick the right data by himself - see "value format"

Dave C-J

unread,
Jan 9, 2018, 5:44:07 PM1/9/18
to node...@googlegroups.com
Just to note - that works for text fields but not charts or gauges.
Reply all
Reply to author
Forward
0 new messages