I’ve been developing my Home Assistant based smarthome setup for around a year now (since leaving SmartThings) and after an initial steep learning curve I’ve been veyr happy with home assistant so I have started working to expand the things it does as I am conifdent it offers a long term basis for my smarthome. One thing I wanted to add was bin collection data to remind me which bin is due when. As I live in Sheffield I needed to pull this data from the local authorities out-sources provider (veolia). The following details how I did this, building very much on the work of others who have done it already for other regions
I used a script which i modified from various examples on a HA support thread by RobBrad. For this script to run I needed to install the beautifulsoup module on home-assistant.
Installa il modulo python BeautifulSoup
- Install the Supervisor add-on ‘SSH and Web Terminal’
- Open the terminal from the main left-hand menu
- Use the command pip3 installa beautifulsoup4
Aggiungi uno script
- I use the ‘Samba Share’ supervisor addon to access my home assistant files directly from my windows PC — unless you already have a preferred access method I recommend doing the same
- passare a \\ha-ip-address config
- create a script python cartella
- Create a new python script in this folder — I called mine bin_collection.py
- Insert the following script
from bs4 import BeautifulSoup import datetime from dateutil import parser import requests import json url = 'https://wasteservices.sheffield.gov.uk/property/############' page = requests.get(url) se page.status_code != 200: Uscita(1) soup = BeautifulSoup(page.text, "html.parser") fuori = {} bh3s = soup.find_all("td", class_ ="Nome di Servizio") bpds = soup.find_all("td", class_ ="prossimo servizio") per i nel raggio d'azione(solo(bpds)): bin_colour = str(bh3s[io].Contenuti[3]).inferiore().Diviso('>')[1].Diviso('')[0] su[bin_colour] = parser.parse(bpds[io].Contenuti[2].lstrip().Diviso(',')[0]).strftime('% Y-% m-% d') Stampa(json.dumps(su))
- Dovrai sostituire il file ############ with the unique number for your property which I will explain how to get in the next step
Ottieni il tuo indirizzo univoco
- Individuare https://wasteservices.sheffield.gov.uk/property/
- Inserisci il tuo codice postale e seleziona il tuo indirizzo dall'elenco
- Prendi nota del nuovo URL. Use it in the url line in the script above — the only part you will need to change is the 12 digit number at the end
Chiama il tuo script da configuration.yaml
- Una semplice voce in configuration.yaml è tutto ciò che è necessario per eseguire lo script
- You may want to adjust the frequency of the run. Mine runs once per day.
sensore: - piattaforma: command_line name: "Raccolte bin" comando: "python3 /config/python-scripts/bin_collection.py" scan_interval: 86400 command_timeout: 60 #necessario in quanto il sito web è lento a rispondere e spesso va in timeout se lasciato per impostazione predefinita
Crea entità per ogni tipo di contenitore utilizzando configuration.yaml
- Immediately under the above section (quindi ancora sotto il sensore: se-zione) aggiungere il seguente
- piattaforma: modello sensori: black_bin: device_class: timestamp value_template: '{{ (stati("sensor.bin_collections")|from_json())["nero"] }}' unique_id: "black_bin" brown_bin: device_class: timestamp value_template: '{{ (stati("sensor.bin_collections")|from_json())["Marrone"] }}' unique_id: "brown_bin" blue_bin: device_class: timestamp value_template: '{{ (stati("sensor.bin_collections")|from_json())["blu"] }}' unique_id: "blue_bin"
Visualizza i risultati sul front-end
- To show the results on the front end simply add a card with the 3 sensori su di esso (specifically sensor.black_bin, sensor.blue_bin, e sensor.brown_bin)
- Note you may need to restart home assistant to load the new sensors you added to configuration.yaml
Inviateci i vostri pensieri commentando qui sotto! Se si desidera iscriversi si prega di utilizzare il link iscriviti sul menu in alto a destra. È inoltre possibile condividere con i tuoi amici usando i link sottostanti sociali. Saluti.
lascia un commento