From 6e4f0ccc1303cbe2ff6107725d3e094fff889bda Mon Sep 17 00:00:00 2001 From: Phil Date: Sat, 9 Nov 2024 22:11:33 +0000 Subject: [PATCH] Added new version of the function, so there is a choice of grep or sed. Added a Home Assistant sensor setup config. --- HomeAssistant/README.md | 61 +++++++++++++++++++ HomeAssistant/fetch_costco_prices.sh | 16 +++++ function | 16 ----- functions/Costco_fuel_price | 37 +++++++++++ functions/README.md | 5 ++ .../function_export_to_site.sh | 0 6 files changed, 119 insertions(+), 16 deletions(-) create mode 100644 HomeAssistant/README.md create mode 100644 HomeAssistant/fetch_costco_prices.sh delete mode 100644 function create mode 100644 functions/Costco_fuel_price create mode 100644 functions/README.md rename function_export_to_site.sh => functions/function_export_to_site.sh (100%) diff --git a/HomeAssistant/README.md b/HomeAssistant/README.md new file mode 100644 index 0000000..1495b08 --- /dev/null +++ b/HomeAssistant/README.md @@ -0,0 +1,61 @@ +## Costco Fuel Price script for a Home Assistant sensor + +### Home Assistant configuration.yaml + +To execute your shell script every 6 hours and expose the extracted fuel prices as sensors in Home Assistant, you'll follow these steps: + +1) Create the Shell Command in Home Assistant +Ensure that the shell command integration is enabled in your Home Assistant configuration by adding this to configuration.yaml: + +``` +shell_command: + fetch_costco_fuel_prices: /config/scripts/fetch_costco_prices.sh +``` + +Save your script as fetch_costco_prices.sh in the /config/scripts directory or modify the path accordingly. + +2) Add a Command-Line Sensor for Each Variable +Use command-line sensors to expose the extracted values. Here's how you can do it in your configuration.yaml file: + +``` +sensor: + - platform: command_line + name: Costco Premium Diesel + command: "bash /config/scripts/fetch_costco_prices.sh | jq -r '.premium_diesel'" + scan_interval: 21600 # Every 6 hours + - platform: command_line + name: Costco Premium Unleaded + command: "bash /config/scripts/fetch_costco_prices.sh | jq -r '.premium_unleaded'" + scan_interval: 21600 # Every 6 hours + - platform: command_line + name: Costco Unleaded Petrol + command: "bash /config/scripts/fetch_costco_prices.sh | jq -r '.unleaded'" + scan_interval: 21600 # Every 6 hours +``` + +Here, scan_interval: 21600 sets the sensors to update every 6 hours (21600 seconds). The command extracts the respective JSON values from the script output using jq. + +3) Make Sure Dependencies Are Installed +For this setup: + +curl should already be installed on most systems. +Ensure jq is installed (used to parse JSON output). On Debian-based systems, you can install it using: + +``` +sudo apt-get install jq +``` + +4) Testing the Configuration +Before restarting Home Assistant, validate your configuration: + +``` +ha core check +``` + +If no errors are shown, restart Home Assistant to apply the new configuration: + +``` +ha core restart +``` + +This configuration will execute the script every 6 hours to retrieve the latest fuel prices and expose them as sensors in Home Assistant. You can then use these sensors in automations, dashboards, or other components as needed. diff --git a/HomeAssistant/fetch_costco_prices.sh b/HomeAssistant/fetch_costco_prices.sh new file mode 100644 index 0000000..5384189 --- /dev/null +++ b/HomeAssistant/fetch_costco_prices.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# Get website data with curl +costco_gateshead_site=$(curl -s https://www.costco.co.uk/store-finder/Gateshead) + +# Extract text for different fuel prices using sed +costco_diesel=$(echo $costco_gateshead_site | sed -n 's/.*"gas-title">Premium Diesel<\/span><\/br> \([^<]\{5\}\).*/\1/p' | grep -oE '^.{5}' ) + +costco_premium_unleaded=$(echo $costco_gateshead_site | sed -n 's/.*"gas-title">Premium Unleaded Petrol<\/span><\/br> \([^<]\{5\}\).*/\1/p' | grep -oE '^.{5}' ) + +costco_unleaded=$(echo $costco_gateshead_site | sed -n 's/.*"gas-title">Unleaded Petrol<\/span><\/br> \([^<]\{5\}\).*/\1/p' | grep -oE '^.{5}' ) + +# Print fuel prices in JSON format for Home Assistant +echo "{\"premium_diesel\": \"$costco_diesel\", \"premium_unleaded\": \"$costco_premium_unleaded\", \"unleaded\": \"$costco_unleaded\"}" + + diff --git a/function b/function deleted file mode 100644 index ddb195a..0000000 --- a/function +++ /dev/null @@ -1,16 +0,0 @@ -function costco-fuel-price { -## Get website data with curl -## Change Store location to closest location -costco_gateshead_site=$(curl -s https://www.costco.co.uk/store-finder/Gateshead) - -## Grep text from website for different fuel -costco_diesel=$(echo $costco_gateshead_site | grep -oP '"gas-title">Premium Diesel
\K.*' | grep -oE '^.{5}') -costco_premium_unleaded=$(echo $costco_gateshead_site | grep -oP '"gas-title">Premium Unleaded Petrol
\K.*' | grep -oE '^.{5}') -costco_unleaded=$(echo $costco_gateshead_site | grep -oP '"gas-title">Unleaded Petrol
\K.*' | grep -oE '^.{5}') - -## Print fuel prices -echo "Premium Diesel - $costco_diesel p" -echo "Premium Unleaded - $costco_premium_unleaded p" -echo "Unleaded - $costco_unleaded p" -} - diff --git a/functions/Costco_fuel_price b/functions/Costco_fuel_price new file mode 100644 index 0000000..1e68c82 --- /dev/null +++ b/functions/Costco_fuel_price @@ -0,0 +1,37 @@ +# Using grep +function costco-fuel-price { +## Get website data with curl +## Change Store location to closest location +costco_gateshead_site=$(curl -s https://www.costco.co.uk/store-finder/Gateshead) + +## Grep text from website for different fuel +costco_diesel=$(echo $costco_gateshead_site | grep -oP '"gas-title">Premium Diesel
\K.*' | grep -oE '^.{5}') +costco_premium_unleaded=$(echo $costco_gateshead_site | grep -oP '"gas-title">Premium Unleaded Petrol
\K.*' | grep -oE '^.{5}') +costco_unleaded=$(echo $costco_gateshead_site | grep -oP '"gas-title">Unleaded Petrol
\K.*' | grep -oE '^.{5}') + +## Print fuel prices +echo "Premium Diesel - $costco_diesel p" +echo "Premium Unleaded - $costco_premium_unleaded p" +echo "Unleaded - $costco_unleaded p" +} + +# Using sed +function costco-fuel-price { +# Get website data with curl +costco_gateshead_site=$(curl -s https://www.costco.co.uk/store-finder/Gateshead) + +# Extract text for different fuel prices using sed +costco_diesel=$(echo $costco_gateshead_site | sed -n 's/.*"gas-title">Premium Diesel<\/span><\/br> \([^<]\{5\}\).*/\1/p' | grep -oE '^.{5}' ) + +costco_premium_unleaded=$(echo $costco_gateshead_site | sed -n 's/.*"gas-title">Premium Unleaded Petrol<\/span><\/br> \([^<]\{5\}\).*/\1/p' | grep -oE '^.{5}' ) + +costco_unleaded=$(echo $costco_gateshead_site | sed -n 's/.*"gas-title">Unleaded Petrol<\/span><\/br> \([^<]\{5\}\).*/\1/p' | grep -oE '^.{5}' ) + + +echo "Premium Diesel - $costco_diesel p" +echo "Premium Unleaded - $costco_premium_unleaded p" +echo "Unleaded - $costco_unleaded p" +} + + + diff --git a/functions/README.md b/functions/README.md new file mode 100644 index 0000000..a25fc00 --- /dev/null +++ b/functions/README.md @@ -0,0 +1,5 @@ +## Costco Fuel Price function + +A function that can be added as a alias in linux to show the fuel prices for Costco. + +There are two different versions of the function, one uses grep to parse the html and the other used sed diff --git a/function_export_to_site.sh b/functions/function_export_to_site.sh similarity index 100% rename from function_export_to_site.sh rename to functions/function_export_to_site.sh