Created
December 31, 2024 10:39
-
-
Save dhcgn/80bb8b748f56475922e204d5b84017e0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # ============================================================================= | |
| # Tibber Data to a IoT internet data service | |
| # ============================================================================= | |
| # | |
| # Description: | |
| # This script fetches electricity price information from Tibber API and uploads | |
| # it to an IoT service. It handles both the complete data set as base64 encoded | |
| # JSON and individual current price values. | |
| # | |
| # Usage: | |
| # ./upload_tibber_values_to_iot.sh <tibber_api_key> [iot_upload_key] | |
| # | |
| # Arguments: | |
| # tibber_api_key - Required. Your Tibber API access token | |
| # iot_upload_key - Optional. Key for IoT service (https://iot.hdev.io/). | |
| # If not provided, a new one will be generated and displayed | |
| # | |
| # Outputs: | |
| # - Complete price data as base64 encoded JSON | |
| # - Current price value | |
| # - Current price timestamp | |
| # - Download URL for accessing the uploaded data | |
| # ============================================================================= | |
| # Check for required programs | |
| for cmd in curl jq uuidgen base64; do | |
| if ! command -v "$cmd" >/dev/null 2>&1; then | |
| echo "Error: Required program '$cmd' is not installed" | |
| exit 1 | |
| fi | |
| done | |
| # Check if TIBBER_API_KEY is provided | |
| if [ -z "$1" ]; then | |
| echo "Error: TIBBER_API_KEY is required" | |
| echo "Usage: $0 <tibber_api_key> [iot_upload_key]" | |
| exit 1 | |
| fi | |
| TIBBER_API_KEY="$1" | |
| # Generate or use provided IOT_UPLOAD_KEY | |
| if [ -z "$2" ]; then | |
| IOT_UPLOAD_KEY=$(uuidgen | sha256sum | cut -d " " -f1) | |
| echo "Generated IOT_UPLOAD_KEY (save for reuse): ${IOT_UPLOAD_KEY}" | |
| else | |
| IOT_UPLOAD_KEY="$2" | |
| fi | |
| echo "Using IOT_UPLOAD_KEY: ${IOT_UPLOAD_KEY}" | |
| # Define GraphQL query with proper formatting | |
| GRAPHQL_QUERY='{"query":"{\n viewer {\n homes {\n currentSubscription {\n priceInfo {\n current {\n total\n energy\n tax\n startsAt\n }\n today {\n total\n energy\n tax\n startsAt\n }\n tomorrow {\n total\n energy\n tax\n startsAt\n }\n }\n }\n }\n }\n}"}' | |
| # Download Tibber values | |
| TIBBER_RESPONSE=$(curl -s 'https://api.tibber.com/v1-beta/gql' \ | |
| -H 'accept: application/json' \ | |
| -H "authorization: Bearer ${TIBBER_API_KEY}" \ | |
| -H 'content-type: application/json' \ | |
| --data-raw "${GRAPHQL_QUERY}") | |
| # Check for errors in response, contains json object .errors | |
| if echo "$TIBBER_RESPONSE" | jq -e '.errors' > /dev/null; then | |
| echo "Error in Tibber response:" | |
| echo "$TIBBER_RESPONSE" | jq -r '.errors[].message' | |
| echo "Full response:" | |
| echo "$TIBBER_RESPONSE" | jq '.' | |
| exit 1 | |
| fi | |
| # echo "TIBBER_RESPONSE:" | |
| # echo "$TIBBER_RESPONSE" | |
| # Encode to base64 | |
| BASE64_DATA=$(echo "$TIBBER_RESPONSE" | base64 -w 0) | |
| # Upload to IoT service and get download URL | |
| UPLOAD_RESPONSE=$(curl -s "https://iot.hdev.io/patch/${IOT_UPLOAD_KEY}/?tibber_base64=${BASE64_DATA}") | |
| # echo "curl -s ""https://iot.hdev.io/patch/${IOT_UPLOAD_KEY}/?tibber_base64=${BASE64_DATA}""" | |
| echo "Upload response:" | |
| echo "$UPLOAD_RESPONSE" | |
| # Extract and print download URL | |
| echo "Download URL for base64:" | |
| echo "$UPLOAD_RESPONSE" | jq -r '.parameter_urls.tibber_base64' | |
| echo "Download URL for json:" | |
| echo "$UPLOAD_RESPONSE" | jq -r '.download_url' | |
| # | |
| # Upload only priceInfo.current.total | |
| # | |
| PRICEINFO_CURRENT=$(echo "$TIBBER_RESPONSE" | jq -r '.data.viewer.homes[0].currentSubscription.priceInfo.current.total') | |
| UPLOAD_RESPONSE=$(curl -s "https://iot.hdev.io/patch/${IOT_UPLOAD_KEY}/priceInfo/?current=${PRICEINFO_CURRENT}") | |
| # | |
| # Upload only priceInfo.current.startsAt | |
| # | |
| PRICEINFO_CURRENT=$(echo "$TIBBER_RESPONSE" | jq -r '.data.viewer.homes[0].currentSubscription.priceInfo.current.startsAt') | |
| UPLOAD_RESPONSE=$(curl -s "https://iot.hdev.io/patch/${IOT_UPLOAD_KEY}/priceInfo/?startsAt=${PRICEINFO_CURRENT}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment