# Home Assistant Settings url_base="https:///api/states" token="IsImlcwNzIzOTE0MyoxNzA3MjM5MTR9..." # Server name srv_name="vps_server" # Constants for device info DEVICE_IDENTIFIERS='["amnisia_server"]' DEVICE_NAME="Server Disk Space Monitor" DEVICE_MANUFACTURER="Amnisia" DEVICE_MODEL="Disk Monitor" # Function to send data to Home Assistant send_to_ha() { local sensor_name=$1 local value=$2 local friendly_name=$3 local icon=$4 local unique_id=$5 local url="${url_base}/${sensor_name}" local device_info="{\"identifiers\":${DEVICE_IDENTIFIERS},\"name\":\"${DEVICE_NAME}\",\"manufacturer\":\"${DEVICE_MANUFACTURER}\",\"model\":\"${DEVICE_MODEL}\"}" local payload="{\"state\":\"${value}\",\"attributes\":{\"friendly_name\":\"${friendly_name}\",\"icon\":\"${icon}\",\"device\":${device_info}}}" curl -X POST -H "Authorization: Bearer ${token}" -H 'Content-Type: application/json' --data "${payload}" "${url}" } # Function to check disk space check_disk_space() { # Get disk space usage and free space disk_usage_kb=$(df / | awk 'NR==2 {print $3}') # Used space in KB disk_free_kb=$(df / | awk 'NR==2 {print $4}') # Free space in KB # Convert KB to GB disk_usage_gb=$(df / | awk 'NR==2 {printf "%.2f", $3/1024/1024}') # Used space in GB disk_free_gb=$(df / | awk 'NR==2 {printf "%.2f", $4/1024/1024}') # Free space in GB # Send disk usage and free space to Home Assistant send_to_ha "sensor.${srv_name}_disk_usage" "${disk_usage_gb}" "Disk Used Space (GB)" "mdi:harddisk" "${srv_name}_disk_usage" send_to_ha "sensor.${srv_name}_disk_free" "${disk_free_gb}" "Disk Free Space (GB)" "mdi:harddisk" "${srv_name}_disk_free" } # Run the function to check disk space check_disk_space