0Raccogli raccolte in Home Assistant

I’ve been devel­op­ing my Home Assist­ant based smarthome setup for around a year now (since leav­ing SmartTh­ings) and after an ini­tial steep learn­ing curve I’ve been veyr happy with home assist­ant so I have star­ted work­ing to expand the things it does as I am con­if­dent it offers a long term basis for my smarthome. One thing I wanted to add was bin col­lec­tion data to remind me which bin is due when. As I live in Shef­field I needed to pull this data from the loc­al author­it­ies out-sources pro­vider (veolia). The fol­low­ing details how I did this, build­ing very much on the work of oth­ers who have done it already for oth­er regions

I used a script which i mod­i­fied from vari­ous examples on a HA sup­port thread by Rob­Brad. For this script to run I needed to install the beau­ti­ful­soup mod­ule on home-assistant.

Installa il modulo python BeautifulSoup

  • Install the Super­visor add-on ‘SSH and Web Terminal’
  • Open the ter­min­al from the main left-hand menu
  • Use the com­mand pip3 installa beautifulsoup4

Aggiungi uno script

  • I use the ‘Samba Share’ super­visor addon to access my home assist­ant files dir­ectly from my win­dows PC — unless you already have a pre­ferred access meth­od I recom­mend doing the same
  • passare a \\ha-ip-address config
  • cre­ate a script python cartella
  • Cre­ate a new python script in this folder — I called mine bin_collection.py
  • Insert the fol­low­ing 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 num­ber for your prop­erty 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 num­ber 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 fre­quency 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

  • Imme­di­ately under the above sec­tion (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 res­ults on the front end simply add a card with the 3 sensori su di esso (spe­cific­ally sensor.black_bin, sensor.blue_bin, e sensor.brown_bin)
  • Note you may need to restart home assist­ant 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