REST API no longer working for me (set state for switch)

I’ve been successfully using the REST API from my octoprint server to turn ON/OFF the printer when a new print comes in. This had been working for a long time, but yesterday it failed to work and I’m not able to figure out why. I’m wondering if a software update broke this and it went unnoticed because I didn’t use the printer for a while.

The switch I’m using is a Minoston Z-Wave switch, but I’m not sure how relevant it is since I can reproduce this problem with a Kasa (IP based) switch too.

My REST API calls to POST (set the state) are successful, and I do see the toggle UI element flip when I turn things ON or OFF via the API, but the plug itself does not change state. However, if I call HTTP GET after my POST, the API returns the state I set correctly, but again, that is not what the actual device is doing.

However, if I manually toggle the switch from the web UI, it works, and the switch will match what I set in the UI.

The event logs are slightly different between the Z-Wave switch and the Kasa switch, when I use the HTTP POST API:

  • Z-Wave: - Shows a “Turned on” event (but the actual device does not turn on)
  • Kasa: - Shows a “Turned on” event followed by a “Turned off” event 5 seconds later (the log doesn’t show what initiated the Off event)

Has anyone seen this before? Any way I can debug this further?

System Info:
Home Assistant 2023.9.2
Supervisor 2023.09.2
Operating System 10.5
Frontend 20230911.0 - latest

Thanks!

Here’s the answer to my problem (thanks to a nice Reddit user):

Instead of

curl -X POST http://homeassistant.lan:8123/api/states/switch.chandelier_2 -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjMmM1Y2M3NDI3ODQ0NzBlOGQ0ZjI1NTkzNTVmMDRkYyIsImlhdCI6MTY5NTY4ODQ5NCwiZXhwIjoyMDExMDQ4NDk0fQ.ZK5zV6pEvFlq08trGmbfMNENXoiFXuTUI5WR4waGMNY' -H 'Content-Type: application/json' -d '{"state" : "on"}'

I should have used

curl -X POST http://homeassistant.lan:8123/api/services/switch/turn_on -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjMmM1Y2M3NDI3ODQ0NzBlOGQ0ZjI1NTkzNTVmMDRkYyIsImlhdCI6MTY5NTY4ODQ5NCwiZXhwIjoyMDExMDQ4NDk0fQ.ZK5zV6pEvFlq08trGmbfMNENXoiFXuTUI5WR4waGMNY' -H 'Content-Type: application/json' -d '{"entity_id" : "switch.chandelier_2"}'

Still not clear why the inappropriate form used to work for me.