From f4c5b19b5c1fc6141a517ae8f01d78115c415d29 Mon Sep 17 00:00:00 2001 From: "Adrian A. Baumann" Date: Fri, 5 Dec 2025 22:49:23 +0100 Subject: [PATCH] Add HA connection validation and improved error debugging - Add test_ha_connection() function to validate API access at startup - Improve error logging with response body details - Add debug logs showing exact API endpoints being called - Capture stderr from curl to catch connection errors --- thinkcenter_monitor.sh | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/thinkcenter_monitor.sh b/thinkcenter_monitor.sh index 3382dcc..1ba2f54 100644 --- a/thinkcenter_monitor.sh +++ b/thinkcenter_monitor.sh @@ -33,6 +33,20 @@ log() { echo "[$timestamp] $message" | tee -a "$LOG_FILE" } +test_ha_connection() { + # Test Home Assistant connectivity at startup + log "Testing Home Assistant connection..." + local http_code=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: Bearer $HA_TOKEN" "$HA_URL/api/" 2>/dev/null) + + if [[ "$http_code" == "200" ]]; then + log "Home Assistant API connection successful" + return 0 + else + log "ERROR: Home Assistant API returned HTTP $http_code" + return 1 + fi +} + check_target() { # Use curl to get HTTP response code without following redirects local response_code=$(curl -s -o /dev/null -w "%{http_code}" -m 5 "$TARGET_URL" 2>/dev/null) @@ -65,11 +79,12 @@ trigger_power_cycle() { # Turn off log "Sending turn_off request to Home Assistant ($turn_off_service)..." + log "DEBUG: POST to $HA_URL/api/services/$turn_off_service with entity_id=$entity" local response=$(curl -s -w "\n%{http_code}" -X POST \ -H "Authorization: Bearer $HA_TOKEN" \ -H "Content-Type: application/json" \ -d "{\"entity_id\": \"$entity\"}" \ - "$HA_URL/api/services/$turn_off_service") + "$HA_URL/api/services/$turn_off_service" 2>&1) local http_code=$(echo "$response" | tail -n 1) local body=$(echo "$response" | head -n -1) @@ -77,7 +92,8 @@ trigger_power_cycle() { if [[ "$http_code" =~ ^[2][0-9]{2}$ ]]; then log "Turn off request sent successfully (HTTP $http_code)" else - log "ERROR: Failed to send turn_off request (HTTP $http_code). Response: $body" + log "ERROR: Failed to send turn_off request (HTTP $http_code)" + log "ERROR: Response body: $body" fi # Wait 10 seconds @@ -86,11 +102,12 @@ trigger_power_cycle() { # Turn on log "Sending turn_on request to Home Assistant ($turn_on_service)..." + log "DEBUG: POST to $HA_URL/api/services/$turn_on_service with entity_id=$entity" local response=$(curl -s -w "\n%{http_code}" -X POST \ -H "Authorization: Bearer $HA_TOKEN" \ -H "Content-Type: application/json" \ -d "{\"entity_id\": \"$entity\"}" \ - "$HA_URL/api/services/$turn_on_service") + "$HA_URL/api/services/$turn_on_service" 2>&1) local http_code=$(echo "$response" | tail -n 1) local body=$(echo "$response" | head -n -1) @@ -99,7 +116,8 @@ trigger_power_cycle() { log "Turn on request sent successfully (HTTP $http_code)" log "Power cycle completed for $entity" else - log "ERROR: Failed to send turn_on request (HTTP $http_code). Response: $body" + log "ERROR: Failed to send turn_on request (HTTP $http_code)" + log "ERROR: Response body: $body" fi # Reset state @@ -115,6 +133,12 @@ log " HA_ENTITY: $HA_ENTITY" log " GRACE_PERIOD: ${GRACE_PERIOD}s ($(( GRACE_PERIOD / 60 )) minutes)" log " CHECK_INTERVAL: ${CHECK_INTERVAL}s" +# Test HA connection before entering main loop +if ! test_ha_connection; then + log "FATAL: Cannot connect to Home Assistant. Exiting." + exit 1 +fi + # Main monitoring loop while true; do RESPONSE_CODE=$(check_target)