CURL - Code executes in command prompt but not in batch or .cmd file

EDIT: My issue is now the code will run in the command prompt but not as a .cmd or .bat

I want to run a batch file to turn on a fan from my PC.

curl.exe -X POST -H "Authorization: Bearer token" -H "Content-Type: application/json" -d '{ \"entity_id\":\"script.fan_turnon\" }' http://hassip:8123/api/services/switch/turn_on

But I get this error in the HA logs:

Unable to find referenced entities script.fan_turnon or it is/they are currently not available

What have I done wrong? It’s frustrating something so simple seems so finicky.

Do you see script.fan_turnon in developer tools?


Actually I worked it out for myself, but thank you for replying! I appreciate it.

My issue was the url at the end.

IT should have been: http://192.168.1.XXX:8123/api/services/script/turn_on not http://192.168.1.XXX:8123/api/services/switch/turn_on

Actually my issue is different now, the code executes perfectly in the command prompty but not as a batch file.

Does anyone know what could be wrong here:

C:\Windows\System32\curl.exe -X POST -H "Authorization: Bearer token" -H "Content-Type: application/json" -d '{ \"entity_id\":\"script.fan_turnon\" }' http://192.168.1.XXX:8123/api/services/script/turn_on

Given that its a windows batch file, I would guess you’re in quotation mark hell. I spent an entire weekend trying to get NUT to talk to HA on windows and I swear at least 75% of the time was trying to figure out how to get quotation marks to work correctly in batch files.

Do you have @echo off at the top of your batch file? If so remove it so it actually outputs each command as it runs. Make sure they look right.

Also is it actually a batch file or a powershell script? Because escaping quotation marks has entirely different rules there. And is that actually the command being run or do you have variables for pieces? Because that added all sorts of complications for me with how windows handles variable substitution.

If it helps, here is a batch script I made and confirmed working that uses curl to call HA’s API from a windows machine. It’s not the same API but could copy and tweak it to your needs.

I agree with CentralCommand’s comment regarding the quotes. Quotes (both single and double) are a real pain to deal with in Windows, whether directly at a command prompt or in a batch file. At least you’re not using any percent signs ‘%’, as those are another pain point as well.

I recommend looking at this web page to get some clear guidelines on dealing with quotes:

If you get a specific error message, please post it here, as doing so will help us troubleshoot further.

Note, I’m not affiliated with that web site at all, other than as a satisfied user for the past 15 years or so since I found it.