Excellent work Andrew, glad it was of use and @watou 's addition is great too. @Andrew_Pawelski
Today’s forecast is day 0 in my def statement:
def setFWOItem(BOMName,OpenHABName,day,nodeType):
Call it with:
setFWOItem("air_temperature_maximum","BOM_Temp_Max","0","element")
This retrieves the nodes with:
<forecast-period index="0"
The forecasts for today are there early in the day but are dropped out of the XML as the day progresses, that’s why I trap any errors in the xpath call and ignore them. But, it’s OK as your OH items will remember the last value that you successfully retrieved from the XML when it was last there.
My code is now:
import urllib.request
from lxml import etree
urlBase = 'http://192.168.1.110:8080/classicui/CMD?'
url = 'ftp://ftp.bom.gov.au/anon/gen/fwo/IDW14199.xml'
def setFWOItem(BOMName,OpenHABName,day,nodeType):
try:
value = tree.xpath("//forecast/area[@aac='WA_PT058']/forecast-period[@index='" + day + "']/" + nodeType + "[@type='" + BOMName + "']/text()")[0]
OpenHABName = OpenHABName + "_" + day
print(" " + OpenHABName + "=" + value)
value = urllib.parse.quote_plus(value, safe='', encoding=None, errors=None)
rawPage = urllib.request.urlopen(urlBase + OpenHABName + '=' + value)
except:
print(" Error with " + BOMName + "::" + nodeType + "::" + day)
print('Requesting file...')
rawPage = urllib.request.urlopen(url)
print('File received...')
read = rawPage.read()
tree = etree.XML(read)
print('Updating OpenHAB...')
rawPage = urllib.request.urlopen(urlBase + 'BOMUpdating=ON')
setFWOItem("air_temperature_maximum","BOM_Temp_Max","0","element")
setFWOItem("precipitation_range","BOM_Precipitation_Range","0","element")
setFWOItem("forecast_icon_code","BOM_Forecast_Icon_Code","0","element")
setFWOItem("precis","BOM_Precis","0","text")
setFWOItem("probability_of_precipitation","BOM_Precipitation","0","text")
setFWOItem("air_temperature_maximum","BOM_Temp_Max","1","element")
setFWOItem("air_temperature_minimum","BOM_Temp_Min","1","element")
setFWOItem("precipitation_range","BOM_Precipitation_Range","1","element")
setFWOItem("forecast_icon_code","BOM_Forecast_Icon_Code","1","element")
setFWOItem("precis","BOM_Precis","1","text")
setFWOItem("probability_of_precipitation","BOM_Precipitation","1","text")
rawPage = urllib.request.urlopen(urlBase + 'BOMUpdating=OFF')
print('End')
Items:
Number BOM_Temp_Max_0 "Forecast max today [%.2f °C]" <temperature> (Weather) {mqtt=">[broker:/home/BOM/BOM_Temp_Max_0/state:state:*:default]"}
String BOM_Precipitation_Range_0 "Forecast Rain today [%s]" (Weather) {mqtt=">[broker:/home/BOM/BOM_Precipitation_Range_0/state:state:*:default]"}
String BOM_Precipitation_0 "Precip probability today [%s]" (Weather) {mqtt=">[broker:/home/BOM/BOM_Precipitation_0/state:state:*:default]"}
Number BOM_Forecast_Icon_Code_0 "Forecast Icon Code today [%d]" (Weather) {mqtt=">[broker:/home/BOM/BOM_Forecast_Icon_Code_0/state:state:*:default]"}
String BOM_Precis_0 "Forecast Condition today [%s]" (Weather) {mqtt=">[broker:/home/BOM/BOM_Precis_0/state:state:*:default]"}
Number BOM_Temp_Max_1 "Forecast max tomorrow [%.2f °C]" <temperature> (Weather) {mqtt=">[broker:/home/BOM/BOM_Temp_Max_1/state:state:*:default]"}
Number BOM_Temp_Min_1 "Forecast min tomorrow [%.2f °C]" <temperature> (Weather) {mqtt=">[broker:/home/BOM/BOM_Temp_Min_1/state:state:*:default]"}
String BOM_Precipitation_Range_1 "Forecast Rain tomorrow [%s]" (Weather) {mqtt=">[broker:/home/BOM/BOM_Precipitation_Range_1/state:state:*:default]"}
String BOM_Precipitation_1 "Precip probability tomorrow [%s]" (Weather) {mqtt=">[broker:/home/BOM/BOM_Precipitation_1/state:state:*:default]"}
Number BOM_Forecast_Icon_Code_1 "Forecast Icon Code tomorrow [%d]" (Weather) {mqtt=">[broker:/home/BOM/BOM_Forecast_Icon_Code_1/state:state:*:default]"}
String BOM_Precis_1 "Forecast Condition tomorrow [%s]" (Weather) {mqtt=">[broker:/home/BOM/BOM_Precis_1/state:state:*:default]"}
Switch BOMUpdating "Data is being retrieved from BOM"
I am pushing out the values to MQTT as they come in so I can pick them up in an ESP8266 driving an analogue weather clock that I am building.
The switch “BOMUpdating” moves to ON when updating and to OFF when finished. This allows rules to be run when all of the weather items have been retrieved and updated.