Skip to content

Instantly share code, notes, and snippets.

@Br3nda

Br3nda/seatoun_sewage.py

Last active Feb 25, 2020
Embed
What would you like to do?
#!/usr/bin/env python
from xml.etree import ElementTree
import requests
import json
import sys
import urllib
site = sys.argv[1] # e.g.'Seatoun Beach at Inglis Street',
if (len(sys.argv) > 2):
key = sys.argv[2]
else:
key = None
url = "http://hilltop.gw.govt.nz/Data.hts"
params = {
'Service': 'Hilltop',
'Request': 'GetData',
'Site': site,
'Measurement': 'Enterococci Bacteria'
}
# WRC's site only accepts the old %20 style of encoding
params = urllib.parse.urlencode(params, quote_via=urllib.parse.quote)
response = requests.get(url, params=params)
tree = ElementTree.fromstring(response.content)
reading = tree.find('Measurement').find('Data').find('E')
data = {'Enterococci': reading.find('Value').text}
for p in reading.findall("Parameter"):
data[p.attrib['Name']] = p.attrib['Value']
if key:
print(data.get(key))
else:
print(json.dumps(data))
@Br3nda

This comment has been minimized.

Copy link
Owner Author

@Br3nda Br3nda commented Feb 25, 2020

sample output:

{
  "Enterococci": "4",
  "Lab Sample Number": "20/8801-12",
  "Method": "Enteroc-2",
  "GWRC Programme": "Rec WQ",
  "Sample type": "Routine Sample",
  "Weather": "Overcast",
  "Wind Direction": "NE",
  "Wind strength": "Moderate",
  "Tide": "Flood",
  "Tidal height": "High",
  "Sewage Overflow": "N",
  "Seaweed %": "0"
}

Note that the Enterococci never gets to zero, and instead says <4. e.g. { "Enterococci": "<4"}

@Br3nda

This comment has been minimized.

Copy link
Owner Author

@Br3nda Br3nda commented Feb 25, 2020

sample home assistant config:

switch:
- platform: command_line
  name: Seatoun Enterococci Bacteria
  command: "/home/brenda/.homeassistant/config/sensors/seatoun_sewage.py 'Seatoun Beach at Inglis Street' 'Enterococci'"
  scan_interval: 43200 #12 Hours
  unit_of_measurement: "n/100ml"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment