#HTTP Request
110 messages · Page 1 of 1 (latest)
So that requires installing the traefik addon?
No that has nothing to do with traefik
that's just an example
you need to setup a rest_command integration
I know, but the docs don't easily say how 🙂
Seems to be a YAML-only thing, and not doable in the UI
Looks like you've made some progress.
To parse the response, you would follow that with another action with a template that uses the gotbindata variable.
my URL there returns JSON, but I can't see how to parse it
Ok
So, YAML coding only?
Yes this will likely involve yaml/jinja.
response_variable:gotbindata
Templates are generally not supported in the visual editor.
You can still use the overall visual editor, but the individual elements with a template will need to be edited in yaml.
Again you can still stay in the visual editor, you don't need to edit automations.yaml directly
Yep, however I'm trying to somehow get this response
somehow
and I don't see exactly where I'm supposed to do that
Ok. Lets start from the beginning.
What do you want to do with this response.
🙂
- So, every hour I want an http request to fire (rest or however) and that returns a JSON element, from my website
- This JSON will contain some vars and values, and I really only want to see one value from that and do something with that value, basically looking for a 1 or 0
Ok so go back to your visual editor. After the rest_command, add an if/then condition action (sorry).
Select add condition, then template condition
- I also have a button on the dashboard to trigger the same event to force update (well)
Here?
you can use your response variable there in the template (actually I guess it doesn't require yaml mode)
then you can do one thing or another thing based on some property of that response
Ah, I see
Right, so When >> trigger happens
Then do >> send restful command
then straight afterwards it automatically parses it and checks for value gotbindata in the response at that point?
Everything in the action sequence after the restful command has a template variable called gotbindata available that it can read.
that will have your response json in it
In the "then" section, can I output a debug or log msg just to check "Value was 1" type message?
or "Value was 2"
You can use persistent_notification.create, or logbook.log
Easier for debugging is maybe to just use the Trace viewer though
ok sorry use == not ===
Ok, that saves
(I've been writing too much javascript 🤣)
Yeah, constantly switching languages is quite interesting!
Ok, I'm giving that a test. Thanks for your help so far
so far 😄
Explore around in the trace viewer if you haven't played with it.
It's confusing at first but it's powerful for debugging.
Will show you what data it got back from the rest command and such
Ok, the trace viewer is very good
in >> Automations I did "Run actions" to manually trigger it, and then the trace viewer shows the whole JSON package returned into that variable
The next output however didn't fire
The "value was 1" in the UI is transposing to data: message: Value was 1 <--- does that need speechmarks in the YAML?
Yaml doesn't require quotation marks for strings
so your template was not true
Can you screenshot the full json response?
its format is {"aaa":"123", "bbb","123123", "ccc":"123123123", ... etc "testrestval":"1" }
Maybe also try gotbindata["testrestval"] == "1"
could be forgetting the exact syntax
no worries
Hmm well I'm willing to debug further if you're willing share the whole trace on a codeshare. I understand you might be trying to keep things secret, but I can't tell much more from here.
or you can try just using the example I posted above and try to work it out yourself
Sadly it's not working so still need an expert's help here. Not fussed about privacy at this stage. Happy to look at codeshare if it keeps this post from getting too wild
it's probably soemthing simple
Totally, and likely something silly I've done
use the "Download trace" button in the top right of the trace viewer under 
Please use a code share site to share code or logs, for example:
- https://dpaste.org/ (select YAML for the language, and consider picking a longer expiry)
- http://pastie.org/ (select YAML for the language)
- https://paste.debian.net/ (you guessed it, select YAML as the language)
Please don't use Pastebin, since it can randomly add spaces to the main view. Please also don't share text as images since it makes it harder for people to help you. Remember that others may have colour blindness, impaired vision, etc.
Done. PM'd you codeshare link
Ok a couple things. For one I guess the data is actually in gotbindata['content']['testrestval']
And another issue is that this data doesn't actually appear to be json, but it's like a stringified version of json
E.g. here's what I get back when I hit a json test server:
that's from
rest_command:
example_jsontest:
url: http://jsonplaceholder.typicode.com/users
Yours looks like this:
Yep I see that from the trace log too
Hmm, you're right
I've always output JSON the same way as other apps will "json parse" that string... I never realised
maybe you need a content_type option in your rest_command to tell it that you want json
I'm not exactly sure
You could hack your data out of that string somehow, but it's a bit non-standard
Ok, I edited the PHP to add a json content type (oops)
and now the trace shows differently
old
new
I changed the condition variable format
So this is your variable format for the condition:
🎉