phairplay
(Phairplay)
November 19, 2017, 2:37pm
1
hi,
could would like to run a script on home assistant via a command on my pc.
i’ve looked at the RESTful API and can confirm when i run
curl -X GET -H “x-ha-access: mypassword” -H “Content-Type: application/json” http://192.168.0.13:8123/api/
i get a call back saying API running,
yet when i run
curl -X POST -H “x-ha-access: mypassword” -H “Content-Type: application/json” -d ‘{“entity_id”: “script.lightandtelly_on”}’ http://192.168.0.13:8123/api/services/script/turn_on
i get the following error
curl: (3) [globbing] unmatched close brace/bracket in column 24
{“message”: “Data should be valid JSON”}
could someone please tell me what I’m doing wrong?
treno
(travis reno)
November 19, 2017, 7:31pm
2
Looks like you are using a word processor for your commands. Your single tics and double tics are replaced with stylized open and closed quotes. Replacing these symbols with the simple single and double tics solves the problem.
Here is the result:
curl -X POST -H "x-ha-access: mypassword" -H "Content-Type: application/json" -d '{"entity_id": "script.lightandtelly_on"}' http://192.168.0.13:8123/api/services/script/turn_on
1 Like
phairplay
(Phairplay)
November 19, 2017, 8:01pm
3
thank you for the reply but i’m still getting the same error message
you could try it without the single quotes.
it expects json and you give a string.
i just looked at it again and its complaining about column 24.
i think the problem lies in how you present the password.
or maybe you have something in your password that breaks the command.
that could be something like a / \ or "or ’
thats what i can come up with (but i never used curl, so if thats not it i hope someone else see the problem)
phairplay
(Phairplay)
November 20, 2017, 8:47am
7
The password it not an issue as I use it with ifttt webhook
I’ve experienced similar punctuation mark troubles, so I went a different route.
https://home-assistant.io/developers/python_api/
If the machine you’re using the curl command on can run python3 then the above will help you write a little python script that calls Hass just the same. You’ll need to install Hass to the machine, but you don’t have to run it - just to get the remote python module (well, that’s how I did it).
A sample of how I made mine work
#!/usr/bin/env python3
import homeassistant.remote as remote
api = remote.API('192.168.1.13', '[password]')
domain = 'media_player'
switch_name = 'media_player.hifi_pi'
media_link = remote.get_state(api, 'sensor.hifipi_volume')
if media_link.state == "unknown": media_link.state = "40"
counter = int(float(media_link.state))
remote.set_state(api, 'sensor.rotary_encoder_value', new_state=counter)
You’ll probably want to go with remote.call_service
rather than remote.set_state
in my example, there’s some more ideas in the link. It will definitely work, and it’s how I avoided long curl commands with quotes all over.
I’ve tested in on OSX and the command runs without issue… Are you using windows? Someone else had a similar problem last week (note the use of only double-quotes):
Ok, I figured out this, on Windows it should like to be like: -d “{ “key1”: “value1” }”
2 Likes
phairplay
(Phairplay)
November 20, 2017, 1:11pm
10
hi yes I’m using windows I no longer get an error stating
curl: (3) [globbing] unmatched close brace/bracket in column 24
yet I’m still getting the below error
{“message”: “Data should be valid JSON”}
the problem is the when I use
{“entity_id”: “script.lightandtelly_off”}
in ifttt webhook its works fine
Can you copy and paste the whole command (and error) you’re using? Thanks
phairplay
(Phairplay)
November 20, 2017, 1:30pm
12
curl -X POST -H "x-ha-access: password" -H "Content-Type: application/json" -d "{ "entity_id": "script.lightandtelly" }" http://192.168.0.13:8123/api/services/script/turn_on
“message”: “Data should be valid JSON”}
PhyberApex
(Phyber Apex)
November 20, 2017, 1:48pm
13
Try
curl -X POST -H "x-ha-access: password" -H "Content-Type: application/json" -d "{ \"entity_id\": \"script.lightandtelly\" }" http://192.168.0.13:8123/api/services/script/turn_on
~Cheers
5 Likes
zarthan
November 20, 2017, 2:04pm
15
The problem is your quotes. I don’t know what editor you are using but you need something that is Linux friendly. Notepad ++ for Windows.??
phairplay
(Phairplay)
November 20, 2017, 2:09pm
16
I am using notepad +
PhyberApex post works
"{ \"entity_id\": \"script.lightandtelly\" }
Thank everyone for helping me out
zarthan
November 20, 2017, 2:14pm
17
I understand. Your post with your version of the command shows odd quotes. You need to figure out why your editor is using them and change settings etc for the future???
phairplay
(Phairplay)
November 20, 2017, 2:26pm
18
I just edited my above post
What fixed the problem was adding \ within the json
treno
(travis reno)
November 20, 2017, 4:27pm
19
Adding “I’m using windows” would have gotten you to a solution much faster.
2 Likes
phairplay
(Phairplay)
November 20, 2017, 4:31pm
20
you are very right, a mistake I’m ashamed to have made.
sorry guys
1 Like