Happy to share! However the one that @Zeunas has for Daily works for the Energy dashboard, my amendment definitely does not as it stands!
It’s a bit of a botch job at the moment, so could probably be tidied up a bit.
First
You’ll need to add influxdb into the python packages in the configuration of appdaemon and then restart the addon.
You’ll also need to download the influxdb add on, start it,
then open the Web UI and create a database called “openhab_db” (that’s the name that was in the original script!)
Create a username and password and give it read and write permissions for the openhab_db
Next
Add the following to /config/appdaemon/apps/apps.yaml
WaterUsageHourly:
module: GetWaterUsageHourly
class: GetWaterUsageHourly
Then
Save the following script as GetWaterUsageHourly.py in config/appdaemon/apps
Adding your username and passwords for anglian water and influxdb in the process.
from influxdb import InfluxDBClient
from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import appdaemon.plugins.hass.hassapi as hass
import datetime
import time
import json
import ctypes
#####################
# App configuration #
#####################
# Complete with your settings
anglia_username = "USERNAME"
anglia_password = "PASSWORD"
influxdb_username = "USERNAME"
influxdb_password = "PASSWORD"
influxdb_url = "192.168.5.121"
influxdb_port = 8086
##################
# WATER USAGE APP #
##################
webpage = r"https://my.anglianwater.co.uk/"
class GetWaterUsageHourly(hass.Hass):
# Starting APP #
def initialize(self):
self.log ("intialising")
self.run_every(self.StartProcess, "now", 8*60*60)
#Callback Function to Start the process
def StartProcess(self, kwargs):
self.log("starting process")
usage = self.GetHourlyUsage()
# Get Water Usage from Anglia Water website #
def GetHourlyUsage(self):
try:
self.log("starting request")
service = Service(executable_path=r'/usr/bin/chromedriver')
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-dev-shm-usage')
browser = webdriver.Chrome(service=service, options=chrome_options)
browser.set_window_size(1600, 1200)
self.log("logging in")
browser.get(webpage)
sbox = browser.find_element(By.ID,'existUser')
sbox.send_keys(anglia_username)
sbox = browser.find_element(By.ID,'existPass')
sbox.send_keys(anglia_password)
button = browser.find_element(By.ID,'existingLogIn')
button.click()
self.log("selecting usage")
button = browser.find_element(By.ID,'btnViewUsage')
button.click()
time.sleep(2)
self.log("selecting hourly")
button = browser.find_element("xpath", '//*[@id="dbserialnumber"]/div/div[1]/div[1]/label/span')
button.click()
time.sleep(2)
self.log("saving usage")
html = browser.page_source
browser.quit()
startdata = html.find("var myUsageDetails_days = ") + 26
enddata = html.find(";;", startdata)
result = html[startdata: enddata]
data = json.loads(result.replace("'", '"'))
json_body = []
for x in data:
date_object = datetime.datetime.strptime(x['groupDate'] + " " + x['time'], "%d-%b-%Y %H:%M")
item = {"measurement": "anglia_water_usage", "time": date_object, "fields": {"value": x["usage"] * 1000}}
json_body.append(item)
try:
client = InfluxDBClient(influxdb_url, influxdb_port, influxdb_username, influxdb_password, 'openhab_db')
client.write_points(json_body)
except:
self.log("Influxdb Error")
except:
self.log
If you get all this working, then next step is linking grafana, building dashboards and also potentially getting data into HA as sensors. I’m still working on this bit! But I’ve got sensors working and a chart in grafana, so I’m half way there!
I’m happy to share the work I’ve done getting the sensors into HA in both the methods as discussed in previous posts. None are really useful as yet for the energy dashboard, but I’ve got sensor for last 48hours, 7dayd and 30days working.
The limitation at the moment that I’ve not managed to get a way of pulling specifically yesterdays data in as a influxql query yet. I think it can be done in flux. But last 24hours or 48hours plus from “now()” so it doesn’t work the way I’d want it yet.
That’s why I may just ignore the energy dashboard and create my own water dashboard in grafana, as it’s much easier to use.