problem with weerlive api to domoticz using node-red

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
wkossen
Posts: 62
Joined: Friday 06 November 2020 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

problem with weerlive api to domoticz using node-red

Post by wkossen »

Hi,

I'm trying to learn some node-red and domoticz, so i chose a practice project. Getting the forecast from the weerlive.nl api and pushing it into a domoticz virtual text sensor.

but i can't get it to work. let me explain what i have so far:

I have a device (dummy sensor of type weather forecast / text) in domoticz
i have mqtt running and working to deliver data to domoticz (this already works with another flow)
i have an api key for weerlive.nl as i need it for my home town. but for this post, we could use the demo api key that only works for Amsterdam. which makes the url be like: http://weerlive.nl/api/json-data-10min. ... =Amsterdam and that outputs something like:

{ "liveweer": [{"plaats": "Amsterdam", "temp": "12.4", "gtemp": "8.4", "samenv": "Geheel bewolkt", "lv": "90", "windr": "ZZW", "windms": "6", "winds": "4", "windk": "11.7", "windkmh": "21.6", "luchtd": "1012.2", "ldmmhg": "759", "dauwp": "10", "zicht": "20", "verw": "Bewolkt en af en toe (mot)regen, morgenmiddag vooral in het zuiden zon", "sup": "07:58", "sunder": "16:52", "image": "bewolkt", "d0weer": "bewolkt", "d0tmax": "12", "d0tmin": "5", "d0windk": "3", "d0windknp": "8", "d0windms": "4", "d0windkmh": "15", "d0windr": "Z", "d0neerslag": "4", "d0zon": "7", "d1weer": "bewolkt", "d1tmax": "14", "d1tmin": "8", "d1windk": "3", "d1windknp": "8", "d1windms": "4", "d1windkmh": "15", "d1windr": "Z", "d1neerslag": "30", "d1zon": "30", "d2weer": "regen", "d2tmax": "15", "d2tmin": "12", "d2windk": "3", "d2windknp": "10", "d2windms": "5", "d2windkmh": "19", "d2windr": "Z", "d2neerslag": "90", "d2zon": "10", "alarm": "0"}]}

debug node shows me the output of a http request node nicely. the forecast value is called "verw" which is the first 4 characters of the Dutch word verwachting, which means 'expectation' or in terms of weather, forecast. This entry holds a sentence describing the forecast, like: "Bewolkt en af en toe (mot)regen, morgenmiddag vooral in het zuiden zon" (which it sais right now.)

this i run through a split node into a function nodig where i want to make this output domoticz-readable, and there it breaks. i get nothing:

13/11/2020, 15:45:54node: 9d51eb61.26c4f8
msg.payload : string[0]
""

yeah, nothing. i expected more. really. just a little more if at all possible...

here's the function i put in the function node:

var msg1 = {};
msg.topic = msg.payload.name;
switch (msg.topic) {
case "verw":
msg.payload = {"command":"udevice","idx":111,"nvalue":0,"svalue":(msg.payload.verw)};
break;
}
return msg;

probably way overcomplicated and apparently wrong. I put it in switch/case to allow me to add other things to sensors later. i guess the problem is in the payload somehow.

but what is wrong, i just can't figure it out, probably because i understand very little about this whole system still. But i'm willing to learn, because i have a long list of things i want to try out. oh, and i'm not a programmer or software developer, so these things don't come naturally.

Of course i need to add the domoticz mqtt node at the end, but I'll just copy that from the other flow, so i dont expect any problems there...

so i hope someone can just point out my failings and set me straight on the node-red path of glory. Thanks for any help!

Cheers
Willem
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

Re: problem with weerlive api to domoticz using node-red

Post by EddyG »

Could you post your flow and leave sensitive data out (your apikey), or better replace it with the Amsterdam key?
Did you pass the data thru a json parser?
User avatar
FireWizard
Posts: 1968
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: problem with weerlive api to domoticz using node-red

Post by FireWizard »

Hello Willem @wkossen

It is a good idea, if you want to learn Node Red, that you take a practical case.

Let's analyze what you have made so far.

1. It's clear that you are able to insert the API Key and your Lat/Lon coordinates in your HTTP request.

2. You receive the correct JSON.

So so far everything is alright, but then....

After "http request" node you want to connect a split node.
Why do you want to do that? What do you want to split?
If you investigate the JSON, you will see that you receive an "Object" (liveweer), which consists of an array with 1 element.
This array element (element 0) contains an object again (starting with "Plaats etc and up to "Alarm"),

There is nothing you can split.

This is a very nice formatted object, with a lot of properties
And one of the properties is verw and that is the one your are interested in.

If you hoover with your mouse over that property (verw), you will see a number of additional icons.

The explanation of those icons you will find at: https://nodered.org/docs/user-guide/messages
Hoover with the mouse over "verw:" and click on the most left icon (>_). The path will be copied to the clipboard.

Next you have created a Function node.

First you declare a variable msg1, which is not used anymore. So useless, delete it.
Second: You want to do something with msg.payload.name. However I don't see anything with the name "name" under payload.
So msg.topic is more or less empty and there is no topic with the name "verw". Useless actions.
msg.payload is more or less okay, except of th svalue,

The break at the end is not necessary, as the msg will be returned anyway.

So I hope, you understand that I deleted a lot and that the Function node is much simpler.

msg.payload = {"command":"udevice","idx":111,"nvalue":0,"svalue":msg.payload.liveweer[0].verw};
return msg;

payload.liveweer[0].verw} is the result of hoover with the mouse, click >_ and paste.
Just that simple and I suggest you try several others.

Screenshot_weerlive.png
Screenshot_weerlive.png (12.4 KiB) Viewed 639 times
Regards
wkossen
Posts: 62
Joined: Friday 06 November 2020 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: problem with weerlive api to domoticz using node-red

Post by wkossen »

thanks for the elaborate answer. i removed the split and connected function to http request. I used split assuming i could split all the parameter:value pairs to enumerate them separately with switch/case.

I still can't get it to work. The hover part i don't understand. The only place i see verw is in the debug output and there is nothing happening when i hover over it. so i don't have a button/icon to click.

just copying and running your function code gets me

function : (error)
"TypeError: Cannot read property '0' of undefined"

which can't be good. I must be missing (not understanding) something.
User avatar
FireWizard
Posts: 1968
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: problem with weerlive api to domoticz using node-red

Post by FireWizard »

Hello Willem (@wkossen)

You wrote:
I still can't get it to work. The hover part i don't understand. The only place i see verw is in the debug output and there is nothing happening when i hover over it. so i don't have a button/icon to click.
I really have no idea, what you are doing, but your flow should look like:

Screenshot_KNMI.png
Screenshot_KNMI.png (18.86 KiB) Viewed 632 times

After the HTTP request node, you can connect a Debug node (the one just over the function node).

If I press the "Inject" node you will get the items, you mentioned in your previous post.
Among them "verw:"

Screenshot_KNMI2.png
Screenshot_KNMI2.png (6.29 KiB) Viewed 632 times

If you move your mouse over the light grey area, you will get 3 more icons.
If you click on the most left one (>_) the path is copied to the clipboard. (The middle one copies the value, while the right one pins the item).

The contents of the Function node (function) is as follows:

Code: Select all

msg.payload = {"command":"udevice","idx":111,"nvalue":0,"svalue":msg.payload.liveweer[0].verw};
return msg;
After that, if you press the Inject node , you will see at the other Debug node the result, as shown in my previous post.

Regards
wkossen
Posts: 62
Joined: Friday 06 November 2020 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: problem with weerlive api to domoticz using node-red

Post by wkossen »

ok, so i guess i had my request node wrong. it was sending text output rather than json object. that's why i couldn't see the array nor the copy path option on the verw parameter.

added the mqtt object and now i actually get the text in my sensor in domoticz. so it works! nice. Thanks for all the help.

The only thing that would be nice is if i could get the sensor to show up in the weather tab on domoticz rather than just at utility.

thanks for the help. this has been a good learning experience. on to the next project :)
User avatar
FireWizard
Posts: 1968
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: problem with weerlive api to domoticz using node-red

Post by FireWizard »

Hello Willem (@wkossen)
ok, so i guess i had my request node wrong. it was sending text output rather than json object.
Sorry for that. As I saw JSON code in your first post, I thought you were aware of the possibilities inside the HTTP request node.
But if you send text output as a string, you will also see the left and middle icon.
Regardless of what you select after "Return" in the HTTP request node, you have always those extra icons, if you move your mouse over the result in the Debug node.
i couldn't see the array nor the copy path option on the verw parameter.
You cannot see "verw", if it is a string.
The only thing that would be nice is if i could get the sensor to show up in the weather tab on domoticz rather than just at utility.
I think, many Domoticz users would welcome an options to move things between the tabs and/or create own tabs.
There are some possibilities, but not a simple and easy way to move it.

Regards
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest