Calling an external service and receiving the Response in a variable?

Thx that is the solution, Safari IPhone 5 was the problem…
Everything works now.

A Nice and relative simple solution for a quite common problem when calling external services that can be called with HTTP. Please ASK Erik to put iT as reply on this forum.
By the was, one has to use the browser as Erik suggested.

@Erik, @Arnfinn_Gilberg,

Hi Erik,

As suggested by Jef and previously asked by Roco. Can you please share the solution you sent to Jef? Both Arnfinn and myself are also looking for this solution.

And scrolling by this topic again I noticed it was Jefs first post and i failed to welcome him to the Homey community and hope he’ll share and experience the fun and ease of Homey! Welcome Jef!

Talked to Erik and when he finds some time he will post the solution here. :wink:

Hi all,

Sorry for this delayed response, but I had to find some time for it.
There are several ways to build the flows @Jef_van_Leeuwen requested.
Because he wants to map multiple values from the same HTTP response it’s best to make 2 flows with combine cards GET JSON trigger object (A24), JSON object (T81) and JSONpath Better Logic (A80).
(Native Homey logic support is in development, but for now we rely on BetterLogic for storing variables).

When in doubt validate your JSON values for flow cards with jsonlint.com. Check your JSONpath expressions with jsonpath.com. If you’re using JSONpath expressions on XML responses, be aware of the XML to JSON conversion. This conversion can be simulated on RunKit.com with the xml2js module.

The first flow will do a get request. The returned response will be available in the JSON token on the trigger of the second flow.
In the second flow this JSON token can be used in the action cards, where the desired value is queried by JSONpath and put in Better Logic variables.

Preparation - Define Better Logic variables:
image

Flow 1 - Request (poll every hour):
2019-04-30%2020_05_06-Homey%20Flow

Flow 2 - Extract values and map to Better Logic:
Flow 1 triggers this flow because of the matching trigger-name ‘MonitorUpdate’

1 Like

Hi @Erik,

Very much appreciated to share the example for all to learn and benefit!
I will look into it and see if I can convert it to read an XML string from a local web server and send that to a variable! If anyone else has valid examples of how to work with the magnificent but in my opinion not simple :wink: HTTP request flow cards app please post these on this forum!

Hi @Erik,
The flows you published are very nice. I have about the same question, but…

I have a jsonfile where I get an hourly weather forecast. How can I get the data for example for 4 O’Clock instead off 3 O’Clock?

{“plaatsnaam”:[{“plaats”:“Utrecht”}],“data”:[{“tijd”:“1564405200”,“tijd_nl”:“29-07-2019 15:00”,“offset”:“07”,“loc”:“none”,“temp”:“26”,“winds”:“2”,“windb”:“2”,“windknp”:“4”,“windkmh”:“7.2”,“windr”:“190”,“windrltr”:“Z”,“neersl”:“0”,“luchtd”:“1011”,“luchtdmmhg”:“758”,“luchtdinhg”:“29.84”,“hw”:“0”,“mw”:“0”,“lw”:“29”,“tw”:“29”,“rv”:“41”,“gr”:“155”,“cape”:“281.10”,“cond”:“3”,“ico”:“3”,“samenv”:“Halfbewolkt”,“icoon”:“halfbewolkt”},{“tijd”:“1564408800”,“tijd_nl”:“29-07-2019 16:00”,“offset”:“08”,“loc”:“none”,“temp”:“27”,“winds”:“2”,“windb”:“2”,“windknp”:“4”,“windkmh”:“7.2”,“windr”:“191”,“windrltr”:“Z”,“neersl”:“0”,“luchtd”:“1011”,“luchtdmmhg”:“758.2”,“luchtdinhg”:“29.85”,“hw”:“0”,“mw”:“0”,“lw”:“35”,“tw”:“35”,“rv”:“39”,“gr”:“176”,“cape”:“281.29”,“cond”:“3”,“ico”:“3”,“samenv”:“Halfbewolkt”,“icoon”:“halfbewolkt”},

Hi @Marcel_Ubels
If the data is allways on the same place, lets say allways in the second element then $.data[1].temp will work fine.
If the day and time is fixed then $.data[?(@.tijd_nl=="29-07-2019 15:00")].temp is a good query.
Probably you want to get the data for a specific time, but ignore the day and month. That will work with this query: $.data[?(@.tijd_nl.indexOf('15:00',0)>0)].temp

2 Likes

@Erik thx for your time. This way i will come quite a bit further. The first part I had, the second part with filtering I will try soon.

Data was always in the same place. So that worked fine

Hi @Erik or someone else willing to help

Can you use the same method for getting information from “normal” web page. I’m trying to get the gasoline price data from webpage table. www.polttoaine.net

The simplified webpage xml code is following:

(“

1.570” is the value I want to get read to the variable)
			<table id="Halvin_Kallein" style="width: 586px;">

				<tbody>
					<tr></tr>

					<tr class="bg2">

						<td></td>
						<td></td>
						<td></td>
						<td class="Hinnat">1.570
						</td>
					</tr>
				</tbody>
			</table>
		</div>
	</div>
</div>

So the JSONpath should be $.html.body.div[1].div[4].div[6].table.tbody.tr[1].td[2]

Is this right?

I used the following flow cards:
For trigger:

For working

When I hit the test button, the trigger flow card shows that it is updated. The JSON data is saved in the strTest variable but the strTest2 variable isn’t updated.

I noticed that the strTest variable is containing the webpage xml code correctly. So the question is that can you use the GET requests in “ordinary” webpage to get the data? If you can, what whould I change in my flows to get it work?

Hi!
Unfortunantly HTML is in a lot cases not compliant with the XML convertor. For example when tags are not closed. Parsing this webpage results in this error:

2019-09-21 15:30:52 [log] [HttpApp] 15:30:52 --> data is invalid XML (node:10017) UnhandledPromiseRejectionWarning: Error: Unexpected close tag Line: 48 Column: 7 Char: > at error (/node_modules/xml2js/node_modules/sax/lib/sax.js:667:10) at strictFail (/node_modules/xml2js/node_modules/sax/lib/sax.js:693:7) at closeTag (/node_modules/xml2js/node_modules/sax/lib/sax.js:887:9) at SAXParser.write (/node_modules/xml2js/node_modules/sax/lib/sax.js:1449:13) at Parser.exports.Parser.Parser.parseString (/node_modules/xml2js/lib/xml2js.js:503:31) at Parser.parseString (/node_modules/xml2js/lib/xml2js.js:7:59) at exports.parseString (/node_modules/xml2js/lib/xml2js.js:535:19) at Object.exports.convertToJson (/lib/util.js:12:7) at util.genericRequestHelper.then.result (/lib/flow/actions.js:110:24) at <anonymous>

I don’t think you can workaround this issue with my app.

Erik

Hi.

Thanks for the fast answer and great app.

Might have to dive into Homey Script to have the solution for this problem.

Hi Erik,

I am trying to follow your receipt in order to get the hourly forecast from this site:
https://api.met.no/weatherapi/locationforecast/2.0?lat=58.945370&lon=5.573560

I would like to store all the parameters (i.e air_temperature, wind_speed, ) for the next 12 hours in variables for use in other flows. However, I am struggling to get it to work (I am not very experienced with programming unfortunately). Do you have any ideas how I can make it work?
How would you store the data in variables for instance? Would you make one air_temperature variable in better logic for each hour, or is it possible to store all 12 hours in an array?
Also, when I try to run I get a flow card error in the second step saying value not found. Can you see what I am doing wrong?

Any help would be greatly appreciated :slight_smile:

All the best,
Eirik

Hi @thudelicious

If you like to have the current hour temperature, the JSONpath expression for that is

$.properties.timeseries[0].data.instant.details.air_temperature

The next hour is

$.properties.timeseries[1].data.instant.details.air_temperature

and so on…

There isn’t any array possibility in better logic or Homey variables, so you have to use separate variables. I think in Homey script there are possibility for arrays if you want to dive into that.

In http://jsonpath.com/ you can evaluate the JSONpath syntax by copying your source to the page etc.

I see! Thank you so much.
The link was also very useful to check out the syntax :slight_smile:
I still get an error message when trying to test the flow however, but I am guessing that the card needs some sort of input which should be given from the other card. So when I try to test it I have to give it the input ({“a”:1,“b”:“some text”})? What should I put in here in order to test the card?

Think I will have to look into Homey Script eventually, but as I just got my homey I think I will start with trying to get a better understanding of the flows first :slight_smile:

Thanks again!

1 Like

It seems to be running fine when I checked the variables, so I guess the best way is to verify the JSONpath first.

1 Like

Hello @Erik,

I would like to add a condition to my sprinkler that if the predicted amount of rain from the national weather service is greater than 0, the sprinkler will not start.

I have the url for the query to the weather service for the next 5 days:

http://daten.buergernetz.bz.it/services/weather/district/6/bulletin?format=json&lang=de

This is the result, where I need the value marked in yellow:

I created 2 flows, the first calls the weatherdata, the second should write the betterlogic variable:

I also created a betterlogic variable:

If i try to test the second flow i always get this error:

Thanks a lot

Tip: test your expression with jsonpath.com
With $.forecasts[0].rainTo it should take the rainTo value of the first forecast in the dataset.

i’m getting this error?

image

thanks