check_battery.sh 492 B

12345678910111213141516
  1. #!/bin/bash
  2. # Load environment variables from .env file
  3. source .env
  4. # Query Home Assistant API for battery level
  5. battery_level=$(curl -s -X GET "$HA_URL/api/states/$BATTERY_SENSOR_ENTITY" -H "Authorization: Bearer $HA_TOKEN" | jq '.state' | tr -d '"')
  6. # Check if battery is sufficiently charged
  7. if [ "$battery_level" -ge "$MIN_BATTERY_LEVEL" ]; then
  8. echo "Battery level is sufficient: $battery_level%"
  9. exit 0
  10. else
  11. echo "Battery level is too low: $battery_level%"
  12. exit 1
  13. fi