OpenWeatherMap - formatting datas

224 views
Skip to first unread message

Werner Girlich

unread,
Mar 30, 2018, 1:01:20 PM3/30/18
to Node-RED
I am able to extract any datas from OWM to make them visible in my UI - that's not the issue. I have problems in formatting that datas:

Code msg.payload : array[40]array[40]
[0 9]
0: object
dt: 1522422000
main: object
temp: 12.8
temp_min: 11.63
temp_max: 12.8
pressure: 1002.28
sea_level: 1012.31
grnd_level: 1002.28
humidity: 86
temp_kf: 1.17
weather: array[1]
clouds: object
wind: object
rain: object
sys: object
dt_txt: "2018-03-30 15:00:00"

I my template node I just want to use the icon, the temperature value and the date - but temperature only rounded in an absolute value and the timestamp only in 'hh:mm'.

<div style="display: flex;height: 100%;justify-content: center;align-items: center;">
<i class="fa-2x wi wi-owm-{{msg.payload[5].weather[0].icon}}"></i>
</div>
<div ng-bind-html="msg.payload[5].main.temp"></div>
<div ng-bind-html="msg.payload[5].dt_txt"></div>

 Any help is welcome!

Mark Setrem

unread,
Mar 30, 2018, 4:23:42 PM3/30/18
to Node-RED

Werner Girlich

unread,
Mar 30, 2018, 8:05:07 PM3/30/18
to Node-RED
Didn't know that this can lead to an endless discussion there at stackoverflow...  For my little application the
'.toFixed(0)'-appendix is fully OK and works fine. Thanks for your advice.

Is anybody here to help in deleting the 'date'-information out of the timestamp?

Mark Setrem

unread,
Mar 30, 2018, 9:53:26 PM3/30/18
to Node-RED
i’m sure if you google formatting date javascript you’ll find a stack page that will help you

Werner Girlich

unread,
Mar 31, 2018, 6:44:15 PM3/31/18
to Node-RED
I tried, but I fail in extracting hh:mm even from a text-formatted or ISO-formatted input to place the javascript into a function node. Haven't found any help yet.

Colin Law

unread,
Mar 31, 2018, 8:40:53 PM3/31/18
to node...@googlegroups.com
Are you trying to extract the time out of dt_text as shown in your first post?  If so you can use substring [1]

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/5aaa7042-efa3-4e26-95eb-46065fbc8414%40googlegroups.com.

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

Matha Goram

unread,
Apr 1, 2018, 3:58:52 AM4/1/18
to Node-RED
Hello Werner,

Your note (using OpenWeatherMap) caught my eye while I was searching for some guidance on the Notification widget of the Dashboard!

Here is my function to reformat the datetime stamp returned by OpenWeatherMap:

// convert Unix timestamp to local time in readable format
function riseset(timestamp)
{
    var myLocale = "en-US"; // bn-IN, hi-IN, ur-IN
    var myOptions = {weekday: "long", year: "numeric", month: "numeric",
        day: "numeric"};
    var d = new Date(timestamp * 1.e3);
    return d.toLocaleTimeString(myLocale, myOptions);
}


It is working under Chromium on RPi Stretch (current).

Regards.

Werner Girlich

unread,
Apr 1, 2018, 4:51:00 PM4/1/18
to Node-RED
Hi Colin, Martha, thanks for your inputs. For sure you both have the right approach - but I fail in converting the scripts in a function node. I am a beginner in NodeRED. Nevertheless, string manipulation gave me the idea to search for a particular node, and I found 'node-red-contrib-string'. Great, with this node I was able to solve my little problem. Again thanks and have a nice Easter!
Reply all
Reply to author
Forward
0 new messages