#Can someone help me with a script template for windows batch file that can toggle lights in a room?

1 messages · Page 1 of 1 (latest)

marsh lava
#

I want to create a macro for my keyboard that will allow me to turn on and off lights in the room my PC is in.
I was able to create a script to turn the lights off. And also create a script to turn the lights on. But thes are two different scripts.
I used Chat GPT to create the script...
When I asked ChatGPT to create a script that toggles between on/off states... it doesn't work.

Can anyone provide a script template for me to use that will work?

shadow stirrup
# marsh lava I want to create a macro for my keyboard that will allow me to turn on and off l...

you could just install and use HASS.Agent on your windows system. I use it to have shortcut keys for light and fan in the room. (and to feed some data from the pc back into home assistant as sensors.)

if you really want to do it with a script then you could make an automation that's triggered by webhook then just hit the webhook with the script with curl or something

I am also curious as to what the AI even suggested.

marsh lava
#

I uhh, dont know how to do the webhook thing.

#

This is the script to turn off the lights (Off only... not toggle)
-------------------------------CODE BEGINS-------------------------------------------
@echo off
set "home_assistant_url=http://YOUR_HOME_ASSISTANT_URL/api/services/light/turn_off"
set "access_token=YOUR_LONG_LIVED_ACCESS_TOKEN"
set "entity_id=light.living_room"

:: Send the POST request using curl
curl -X POST "%home_assistant_url%" ^
-H "Authorization: Bearer %access_token%" ^
-H "Content-Type: application/json" ^
-d "{"entity_id": "%entity_id%"}"

echo Lights turned off.
pause
-------------------------------CODE ENDS-------------------------------------------

#

This is teh script it created for toggle:

#

-------------------------------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-------------------------------------------

#

I did get the long lived access token, and corrected the entity id and URL obviously, but it still didn't work

gentle glacier
#

In the first script, just change the second line to end with toggle instead of turn_off

marsh lava
#

LOL!!! That actually I worked, and I can't believe how stupid simple that was! Thanks a bunch!

gentle glacier
#

you might consider using a webhook instead, so that you don't need to have a long-term-access-token in your script which could be bad in the wrong hands. You can create a webhook by creating a new automation and selecting a webhook as the trigger. Then for the action you can have it toggle your light. All your script would have to do is go to http://your_home_assistant_url/api/webhook/some_webhook_id. You can even hit that URL with a browser since you don't need to pass data and you can do it with GET method if you so desire. The webhook ID itself is the only security, but the positive thing is that if an evil actor obtains the webhook ID all they can do is toggle your light.

shadow stirrup
#

as an example to turn off a light

marsh lava
#

I'll have to look into this

#

I could use a simple desktop shortcut, set it to go to the webhook URL!

#

Actually scratch that

#

That would open up the actual URL as well, which would be overly intrusive