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 |