JensS
(Jens Samuelsson)
May 21, 2018, 11:22am
1
Hi,
I wonder if there is any way to create a generic switch/command based on REST that I can change the URL for from a script or automation?
Example:
I have a restful command setup like:
restful_command:
testcall:
url: ‘http://192.x.x.x/api/urlX ’
method: ‘post’
But then in realtime I’d like to change the url to ‘http://192.x.x.x/api/urlY ’.
Is anything like that possible? I’ve been trying in a few different ways now but I’m stuck.
Thanks!!
//Jens
g0g0
May 21, 2018, 11:38am
2
I’ve seen it in other applications but idk how to apply to you, looks like this:
style_template: “return 'background-image: url(”/local/icons/’ + state + ‘_left_1.png");’"
in this url “state” is variable
According to the docs the url parameter of rest_command does support templates. One way I can see using this is to create an input_text entity, and use that as the value for the url. E.g.:
input_text:
rest_cmd_url:
name: URL for RESTful Command
rest_command:
rest_cmd:
url: "http://192.x.x.x/api/{{ states('input_text.rest_cmd_url') }}"
script:
test_rest_cmd:
sequence:
- service: input_text.set_value
data:
entity_id: input_text.rest_cmd_url
value: "urlY"
- service: rest_command.rest_cmd
Actually, the doc also says, “Service call support variables for template stuff.” Unfortunately, it’s not very explicit and there’s no examples, but I would guess, if this is the case, then it might be even simpler. Maybe something like:
rest_command:
rest_cmd:
url: "http://192.x.x.x/api/{{ url }}"
script:
test_rest_cmd:
sequence:
service: rest_command.rest_cmd
data:
url: "urlY"
I haven’t tried it so I might not have the details exactly correct.
10 Likes
JensS
(Jens Samuelsson)
May 21, 2018, 3:05pm
4
Actually both ways worked!! Thanks a bunch, I don’t think I would’ve solved that on my own!
2 Likes
Hi
Sorry, could you help. I 've tried the same thing but in my case the REST call returns 400 - bad request.
rest_command:
mediaruimte_dimmer:
url: http://192.168.0.13:8081/outputs/bec5-8/state
method: PUT
payload: '{"bri": {{ url }}}'
content_type: 'application/json; charset=utf-8'
verify_ssl: false
When executing this in a script or elsewhere I get the 400 error:
test_rest_cmd:
sequence:
- data:
url: '30'
service: rest_command.mediaruimte_dimmer
What could be wrong?
thx
Koen