Skip to content

Instantly share code, notes, and snippets.

@jaimerodas
Created March 31, 2024 03:30
Show Gist options
  • Save jaimerodas/b01237a16478f74bb3c4a0ffea1a8ea9 to your computer and use it in GitHub Desktop.
Save jaimerodas/b01237a16478f74bb3c4a0ffea1a8ea9 to your computer and use it in GitHub Desktop.
Increase/decrease HomeAssistant lamp brightness with keyboard shortcut

Using the script below, I added a macro for F1 and F2 in Keyboard Maestro that run the script like so:

~/Development/scripts/change_light_brigthness.sh up

and

~/Development/scripts/change_light_brigthness.sh down
#!/bin/bash
# Set the direction based on the first argument, default to 'up'
direction=${1:-up}
token="token"
# Endpoint and entity ID
endpoint="http://homeassistant.local:8123/api"
entity_id="light.desk_light_office"
# Get current status
status=$(curl -s -H "Authorization: Bearer ${token}" -H "Content-Type: application/json" "${endpoint}/states/${entity_id}")
# Extract brightness, default to 0 if not found
brightness=$(echo $status | jq '.attributes.brightness // 0')
# Adjust brightness
if [[ "$direction" == "down" ]]; then
brightness=$((brightness - 26))
else
brightness=$((brightness + 26))
fi
# Make sure brightness is within range
brightness=$(($brightness < 0 ? 0 : $brightness))
brightness=$(($brightness > 255 ? 255 : $brightness))
# Set new brightness or turn off
if [[ $brightness -gt 0 ]]; then
curl -s -H "Authorization: Bearer ${token}" -H "Content-Type: application/json" --data '{"entity_id": "'"${entity_id}"'", "brightness": '"${brightness}"'}' "${endpoint}/services/light/turn_on"
else
curl -s -H "Authorization: Bearer ${token}" -H "Content-Type: application/json" --data '{"entity_id": "'"${entity_id}"'"}' "${endpoint}/services/light/turn_off"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment