HTTP request flow cards

Who can and will help me? maybe @robertklep ?

I want to operate my Air conditioner with flows, The device has a built-in webserver,

I start with a POST-request to log-on to http:/192.168.x.x/api.cgi:
{“command”:“login”,“data”:{“username”:“user”,“password”:“welkom”}}

Now that works charming and the response (promise) on this POST-request is:
{“success”:true,“data”:{“id”:{“sessionID”:“v8uS55b50457Ep7Yl5O94liu2sXY0WE”}}}

Now I need that SessionID in a consecutive command to change some values (eg. On/off)
{“command”:“setdatapointvalue”,“data”:{“sessionID”:" v8uS55b50457Ep7Yl5O94liu2sXY0WE ,“uid”:1,“value”:1}}

I’ve tried my best but I can’t get that response in a variable.

Something similar works fine wit a GET-request in the flow card A22 “GET JSONpath Better Logic”, That’s how I read-out some values of my ancient Homewizard…($.data.id.SessionID)

But there’s no such a card for a POST-request, So I thought maybe the card T30 “Incoming-post’ could do the trick… but I can’t get it to work

I also tried advanced options on the A22 card {“method”:”post” etc… etc…} but I cant get the body-part {“command”: etc.etc.} in there

Help?

I don’t think it’s possible with the HTTP Request Flow Cards app. Instead, you could use a Homeyscript:

const response = await fetch('http://192.168.x.x/api.cgi', {
    method : 'POST',
    headers: { 'content-type' : 'application/json' },
    body: JSON.stringify({"command":"login","data":{"username":"user","password":"welkom"}})
});
if (! response.ok) throw Error('Invalid response');
const data = await response.json();
if (! data.success) throw Error('No success');
const sessionID = data.data.id.sessionID;
await setTagValue('sessionID', { type: 'string', title : 'Session ID' }, sessionID);
return true;

In other cards you should be able to use a tag called sessionID for subsequent requests.

@robertklep

thanks for the reply (so fast!!)
I was kinda afraid you were gonna say something like that.
Now I need to get myself familiar with Homeyscript…

I know a thing or two about javascripting (I’ll manage with examples and trial n error.) But I never did any scripting on homey. I Have the app installed but that’s about it

I am starting from absolute zero…
where do I put (store) the code, how do I call it… that sort of beginners-questions

is there a crash-course Homeyscripting for Dummies somewhere? 'cus I can’t find it on the community

You can install the script here: https://homeyscript.homey.app/

When you have installed the app, you can use it in a flow like this:

I called the script ‘HTTP POST test’, which gets executed in the “And…” card, and the action sends me a push notification containing the session ID tag that the script returned.

pff this homeyscript-language is hard to read

I feel i’m reaching the end of your helping energy, which I can understand
You wizzards cannot keep educating newbie’s :slight_smile:

but the script is not working
homey script page consol-windows says: Script error: undefined
using it in a flow i get a yellow flag saying ‘Invalid response’…
which is the error message from the script

so i put the error statement in line 6 behind slashes to disable it
//if (! response.ok) throw Error(‘Invalid response’);
just to see what happens

now then the flow returns on a yellow flag
invalid json response body at http://192.168.x.x/api.cgi
reason: Unexpected token < in JSON at position 0

this leads me to believe that this method does not return a proper Jason code but returns a Html starting with "<html’ …

Any ideas?? :pray:

I assume you changed the username and password used in the first step?

You write that you “start with a POST-request”; what method are you using for that? Perhaps the script needs to send particular headers along with the request.

You can add a bit more debugging information by adding this before the if (! response.ok) line (which you shouldn’t comment out, because that’s where it’s failing):

console.log('Response: %j', response);

Yes off course I changed UN/PW to what it should be and also replaced the x-es in the ip-adress :slight_smile:

Thanks so far for your time and energy… really appreciated :pray:

when I insert the extra line of code you provided, the console returns a long line that starts with:

    Response: {"url":"http://192.168.x.x/api.cgi","status":500,"status*
    Text":"Internal Server Error","headers"......**

an ends with

     ...."bodyUsed":false,"size":0,"timeout":0,"_raw":[],"_abort":false}*

That’s not the response I am looking for, so something’s wrong

How I started…
I started by looking at the network traffic in chrome-devtools while loggin in to the internal webserver of the device. i Looked at the api call and found this…

the json response (devtools Response-tab) is

     {"success":true,"data":{"id":{"sessionID":"igCwXt0O369F0rxAy81d3DN27w9bgWa"}}}

The second thing I tried was to copy/paste the payload in a Logic-MakeWebRequest actioncard, in a flow and executed the flow… like so:


And it kinda works… the post gets send, and the data is received properly,
How Do I know so sure that the payload is received ok and is processed properly? Well

Firstly:
When the flow as shown above is executed, immediately my browser-session get’s kicked out. Appearently the small webserver on the device allowes only for one session at a time (which makes sense)

secondly:
When I am logged in with a browser and I copy the sessionID from the Devtools window and paste it in the ‘Body’-section of a Logic-MakeWebRequest-Action card like so:

    {"command":"setdatapointvalue","data":{"sessionID":"04Ln8vt3w6G5GLh56d5GO2nYPleD2m0","uid":1,"value":1}}

The the device turns on! So as long as the sessionID is valid , I can send all kinds of “uid”.values to the server and completely control my device with flow-cards…

So I know that it actually can work,
Only I just can’t get it to work… So frustrating…

I hope you have some ideas left…
If not… Thanks for helping anyway!!

Only thing I can think of is to send along the request headers that are shown in your screenshot, starting with Accept, Accept-Language, Content-Type, Cookie, Origin and User-Agent.

I tried that already (only didn’t know that the order was important) I tried again just now with the order fixed like you suggested but I don’t get theJSON response I want

the JSON response that i get is still:

 *Response: {"url":"http://192.168.x.x/api.cgi","status":500,"statusText":"Internal Server Error"

also in there are words like

  ..."body":{"_readableState":{"objectMode":false,... 

And

  ..."bodyUsed":false,...

When I comment-out the body json.stringify part out, the response is exactly the same the same

I have a feeling that the problem is in the lay-out, the punctuation of the body somewhere.
The browser sends it as Request-Payload… does that matter?

Header order doesn’t (or shouldn’t) matter. The body-related being logged isn’t relevant because the response fails anyway.

I doubt it’s the punctuation, because from what I can see it’s exactly the same as what the original request is sending.

Did you also try sending the X-Requested-With header?

The device seems to have a very small non-compliant HTTP server, so it can be any number of things that it may not accept even though it’s valid according to the HTTP spec. Trial and error, I’m afraid,

Yeah i did, I copied the whole header-section (with the right punctuation)…didn’t do the trick…

it does accept the JSON if it is send by a flow… any idea why?

Damn this is frustrating :face_with_symbols_over_mouth:
the Github intesis-airconwithme-app created by @Ramon works fine,

Thing is… the options in flow-action-cards are very limited (just on/off and temp)
but somehow i can be done… :woozy_face:

Thanks a lot for your help and time so far
I learnt a lot

Menne

Not really no. It’s probably a combination of headers and/or their value, or perhaps it doesn’t accept that fetch will normalize header names to lowercase. In fact, thinking about it, that might actually be the problem. If so, you can’t use Homeyscript at all because you cannot prevent normalization.

Easier way is buying a tado airco controller. Works fine with homey and of course flows. I control two airco’s cooling, heating etc. No scripts less time spent

@Pieke Thx, I’'ll look into that.
Does Tado offer Advanced commands with you airco (in a flow) like fan-speed (1-4) , airco-modes (cooling, heating, fan-only) or is it just on/off and target-temperature.

I’d hoped that I could solve it with Homeyscript, and learn some Homeyscript as I go along, but I think I have to admit that Homeyscript is a bit out of my league…

Back in the days I was quite good with Visual basic, but Man… Javascripting is something else.
I feel Like learning Russian While hoping it’s as easy as learning English… :woozy_face:

I’m wondering if I can accomplish something with a http request. I can read some stats from my thermostat, a web site that displays this info:

{“result”:“ok”, “currentTemp”:“2007”, “currentSetpoint”:“2000”, “currentInternalBoilerSetpoint”:“18”, “programState”:“0”, “activeState”:“1”, “nextProgram”:"-1", “nextState”:"-1", “nextTime”:“0”,“nextSetpoint”:“0”,“randomConfigId”:“1804289383”,“errorFound”:“255”,“connection”:“0”,“burnerInfo”:“0”,“otCommError”:“0”,“currentModulationLevel”:“0”}

burnerInfo is the one I’m trying to read in a flow, it’s basically the one that says my central heating is ‘on’ and I want to use that info in a flow. There’s no other option via Homey at this moment, so I’m hoping to do this with a http request, but it’s out of my area of expertise.

I you do succeed, please show me the homeyscript…
I’ve been tinkering with it every other day for a couple of hours, but no succes so far…

My issue should be relatively easy, get the JSON value for burnerInfo from my thermostat and do something in Homey. Couldn’t get it to work the normal way, so I worked around it. I have an OpenWRT router on which I read my thermostat in a bash script on a cron schedule, if the value is 1 I send a POST command to Homey to turn on my pump for the heated floor if it’s zero I turn it off. I’ll see if I can get it to work in Javascript so there’s no extra device/step needed.

Edit: I’m tinkering with the API again and saw my old post. Figured it out in Homey after all, was actually rather obvious. Used a GET JSONPATH card:

http://thermostat/happ_thermstat?action=getThermostatInfo {} $.burnerinfo 1

Hi there, I encouter problems with the Body request parameter ‘’‘grant_type’’. Can HTTP request flows work with Body request parameters / URLSeachParam? What is the syntax in a flow for this?

This works on a Advance Rest Client:

curl “https://oauth.cleargrass.com/oauth2/token”
-X POST
-d “grant_type=client_credentials”
-H “Authorization: Basic el92ZzI4xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxDZlZDY5”
-H “Content-Type: application/x-www-form-urlencoded”

Result 200ok
{“access_token”:“EuSlSPvfxxxxxxxxxxxnnMqcdiDbP16rQU7ft1QTRss5BR1M”,“expires_in”:7199,“scope”:"",“token_type”:“bearer”}

Can I use parameters in an incoming POST card and if so, how?

I use POST commands in HomeAssistant and OpenWRT to start flows on Homey.

for instance curl -X POST http://homey/api/app/com.internet/whitelist/TEST starts the FLOW TEST, but now I have multiple flows that do the same thing, so a parameter would be better.