-------------------------------CODE BEGINS-------------------------------------------
@echo off
set "home_assistant_url=http://YOUR_HOME_ASSISTANT_URL/api/states/light.living_room"
set "access_token=YOUR_LONG_LIVED_ACCESS_TOKEN"
set "light_entity=light.living_room"
:: Get the current state of the light
for /f "delims=" %%i in ('curl -s -X GET "%home_assistant_url%" -H "Authorization: Bearer %access_token%" -H "Content-Type: application/json"') do set light_state=%%i
:: Check if the light is on or off and toggle it
echo Current state: %light_state%
echo %light_state% | find "state":"on"" >nul
if %errorlevel%==0 (
:: Light is on, so turn it off
curl -X POST "%home_assistant_url:8123/api/services/light/turn_off" ^
-H "Authorization: Bearer %access_token%" ^
-H "Content-Type: application/json" ^
-d "{"entity_id": "%light_entity%"}"
echo Light turned off.
) else (
:: Light is off, so turn it on
curl -X POST "%home_assistant_url:8123/api/services/light/turn_on" ^
-H "Authorization: Bearer %access_token%" ^
-H "Content-Type: application/json" ^
-d "{"entity_id": "%light_entity%"}"
echo Light turned on.
)
pause
-------------------------------CODE ENDS-------------------------------------------