I have HA and an express API running on different images under one Docker container on my ubuntu server. I attempted to configure a switch using the rest platform, but it failed to show up as a switch on my HA dashboard. So, I changes the platform to command_line, using curl to make the requests- this made the switch show up on my dashboard.
I’ve confirmed that the curl commands work when I ssh into the server, and the endpoints on my API are all working as expected. The switch, however, does nothing, fails to log to the Logbook, and the home-assistant.log file is filled with errors and warnings about timeouts.
home-assistant.log:
"home-assistant.log" [readonly] 3L, 617B 1,1 All
2023-06-21 21:44:37.320 ERROR (SyncWorker_0) [homeassistant.components.command_line.utils] Timeout for command: curl -X POST -H "Content-Type: application/json" -d "{\"state\":\"dark\"}" http://192.168.0.41:8081/roku/state
2023-06-21 21:44:37.324 ERROR (MainThread) [homeassistant.components.command_line] Command failed: curl -X POST -H "Content-Type: application/json" -d "{\"state\":\"dark\"}" http://192.168.0.41:8081/roku/state
2023-06-21 21:44:37.326 WARNING (MainThread) [homeassistant.components.command_line] Updating Command Line Switch roku_dark_mode took longer than the scheduled update interval 0:00:30
configuration.yaml:
switch:
- platform: command_line
switches:
roku_dark_mode:
command_on: 'curl -X POST -H "Content-Type: application/json" -d "{\"state\":\"dark\"}" http://192.168.0.41:8081/roku/state'
command_off: 'curl -X POST -H "Content-Type: application/json" -d "{\"state\":\"light\"}" http://192.168.0.41:8081/roku/state'
command_state: 'curl http://192.168.0.41:8081/roku/state | jq -r ".state"'
value_template: '{{ value == "dark" }}'
# Below is a test switch to confirm that switches were bring picked up.
- platform: rest
name: "jsonplaceholder_test"
resource: https://jsonplaceholder.typicode.com/posts
body_on: '{"title":"foo","body":"bar","userId":1}'
body_off: '{"title":"baz","body":"qux","userId":1}'
headers:
Content-Type: "application/json; charset=utf-8"
Previous attempt at a rest platform:
- platform: rest
name: "roku_dark_mode"
resource: http://192.168.0.41:8081/roku/state
body_on: '{"state":"dark"}'
body_off: '{"state":"light"}'
is_on_template: "{{value_json.state == 'dark'}}"
headers:
Content-Type: application/json