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

a HTTP (POST) call in any browser to a external service returns the data (unixdate, Kelvin-temperatures and the moisture for a specific area i am interested in.

I want to make the call once or twice a day and put the response in the appropiate variables (better logic i.e.).

I expect that one of the HTTP calls in the HTTP app could do the job, but with so many cards and options i need some help/hint in a HowTo solution, anyone can Help ???

Example:
Actual Call (i.e.):
http://api.agromonitoring.com/agro/1.0/soil?polyid=5cc5c0f3d86170003e090a62&appid=afb5821958544dc9fedba14e4e46f849

Actual Response (i.e.):
{“dt”:1556452800,“t10”:284.6,“moisture”:0.146,“t0”:290.74}

1 Like

Maybe with the HTTP app from @Erik ?

Thx, but what to put where in the suggested Get card?
Shout it be:

  • url without parameters inurl (i.e. http://api.agromonitoring.com/agro/1.0/soil )
  • ?polyid=5cc…?appid=afb5… in JSONpath ( but then in JSON format… {“polyid”:"5cc… , “appid”:“afb5…”}
  • variable: a Better Logig Variable (type string?) to hold the response?
    And in a separate flow breakdown of Response in different variables ?

This are some of the choices i consider te make, thats why i try to find some specialists for help…

Replied via PM with working example :slight_smile:

Would u mind to put the working example also here for future reference?
@Erik ^^

3 Likes

Thx, i understand your solution but on my Homey i now have a problem to use any TAG,
whenever i select one, Homey app aborts. Have to solve this first before i can implement your example.

Have you tried it with a browser via flow.homey.app?

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.