Added new version of the function, so there is a choice of grep or sed. Added a Home Assistant sensor setup config.

This commit is contained in:
2024-11-09 22:11:33 +00:00
parent 2dcc769a40
commit 6e4f0ccc13
6 changed files with 119 additions and 16 deletions
+61
View File
@@ -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.
+16
View File
@@ -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> <span class="gas-price">\([^<]\{5\}\).*/\1/p' | grep -oE '^.{5}' )
costco_premium_unleaded=$(echo $costco_gateshead_site | sed -n 's/.*"gas-title">Premium Unleaded Petrol<\/span><\/br> <span class="gas-price">\([^<]\{5\}\).*/\1/p' | grep -oE '^.{5}' )
costco_unleaded=$(echo $costco_gateshead_site | sed -n 's/.*"gas-title">Unleaded Petrol<\/span><\/br> <span class="gas-price">\([^<]\{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\"}"
-16
View File
@@ -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</span></br> <span class="gas-price">\K.*' | grep -oE '^.{5}')
costco_premium_unleaded=$(echo $costco_gateshead_site | grep -oP '"gas-title">Premium Unleaded Petrol</span></br> <span class="gas-price">\K.*' | grep -oE '^.{5}')
costco_unleaded=$(echo $costco_gateshead_site | grep -oP '"gas-title">Unleaded Petrol</span></br> <span class="gas-price">\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"
}
+37
View File
@@ -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</span></br> <span class="gas-price">\K.*' | grep -oE '^.{5}')
costco_premium_unleaded=$(echo $costco_gateshead_site | grep -oP '"gas-title">Premium Unleaded Petrol</span></br> <span class="gas-price">\K.*' | grep -oE '^.{5}')
costco_unleaded=$(echo $costco_gateshead_site | grep -oP '"gas-title">Unleaded Petrol</span></br> <span class="gas-price">\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> <span class="gas-price">\([^<]\{5\}\).*/\1/p' | grep -oE '^.{5}' )
costco_premium_unleaded=$(echo $costco_gateshead_site | sed -n 's/.*"gas-title">Premium Unleaded Petrol<\/span><\/br> <span class="gas-price">\([^<]\{5\}\).*/\1/p' | grep -oE '^.{5}' )
costco_unleaded=$(echo $costco_gateshead_site | sed -n 's/.*"gas-title">Unleaded Petrol<\/span><\/br> <span class="gas-price">\([^<]\{5\}\).*/\1/p' | grep -oE '^.{5}' )
echo "Premium Diesel - $costco_diesel p"
echo "Premium Unleaded - $costco_premium_unleaded p"
echo "Unleaded - $costco_unleaded p"
}
+5
View File
@@ -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