martin072
(Martin)
September 23, 2018, 4:07pm
1
I Have the following config:
kaku1:
command_on: "curl -X GET -H 'Content-Type: application/json' 'http://192.168.x.x/kaku_all.php?a=ON&i=1&d=0'"
command_off: "curl -X GET -H 'Content-Type: application/json' 'http://192.168.x.x/kaku_all.php?a=OFF&i=1&d=0'"
command_state: "curl -X GET -H 'Content-Type: application/json' 'http://192.168.x.x/kaku_all.php?s=1&i=1&d=0'"
value_template: '{{ value_json.status == "ON" }}'
friendly_name: Test Light
And the JSON return is:
[{"status" : "OFF"}]
I can switch on the light, but immediately the switch returns to the Off position. It will never update the statusā¦ What am I doing wrong here?
gpbenton
(Graham)
September 23, 2018, 7:29pm
2
The outer brackets []
indicate that this is a json array, with 1 element, so try
value_template: '{{ value_json[0].status == "ON" }}'
martin072
(Martin)
September 24, 2018, 7:09am
3
Unfortunately this still didnāt workā¦ Light goes On, but switch returns to Off position. Hereās the Debug log;
2018-09-24 09:04:20 INFO (Thread-18) [homeassistant.components.switch.command_line] Running command: curl -X GET -H 'Content-Type: application/json' 'http://192.168.x.x/kaku_all.php?a=ON&i=1&d=0&RESULT=JSON'
2018-09-24 09:04:20 INFO (Thread-22) [homeassistant.components.switch.command_line] Running state command: curl -X GET -H 'Content-Type: application/json' 'http://192.168.x.x/kaku_all.php?s=1&i=1&d=0&RESULT=JSON'
2018-09-24 09:04:20 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]: service_call_id=ad0d681d21df42bab3f64d4dc47577a5>
2018-09-24 09:04:20 DEBUG (MainThread) [homeassistant.components.websocket_api] WS 1713632304: Sending {'id': 15, 'success': True, 'result': None, 'type': 'result'}
It shows the On command and status command, however the last message seems strange, as the āresultā is None?
gpbenton
(Graham)
September 24, 2018, 8:02am
4
Sorry, I should have noticed, the state of the switch reflects the return code of the command, and curl sends the response to stdout. So you need to parse the output of the curl command in the command_line:
command to return true
or false
You can do a full json parse using a program called jq
, but you probably need to install it. In this case, it is probably easiest just to grep the output. I think this will work - and remove the value_template
line.
command_line: "curl -X GET -H 'Content-Type: application/json' 'http://192.168.x.x/kaku_all.php?s=1&i=1&d=0' | grep 'ON' "
martin072
(Martin)
September 24, 2018, 8:19am
5
Great! This worked. Many thanks!
1 Like