Australian Bureau of Meteorology integration with OpenHAB

Hello,

I’am trying to understand te Pyton script, and using it with the Dutch weather KNMI (only FTP)
My Question; what is the meaning off the part 'http://192.168.1.110:8080/classicui/CMD?'
First is the IP adres and the portnumber but the rest is unclear for me.

Tks

Bert

This is call is used to set values of items in OH2 using a simple http GET.

The IP address should be set to the address of your OH2 server. The port number is the default for OH. I think that is you are using OH1.? you omit the “classicui/” bit.

The correct formatting of the command for OH2 is achieved in the setFWO routine:

        value = urllib.parse.quote_plus(value, safe='', encoding=None, errors=None)
        rawPage = urllib.request.urlopen(urlBase + OpenHABName + '=' + value)

…that you call like this:

setFWOItem("air_temperature_maximum","BOM_Temp_Max","1","element")

Where the first parameter is the name of the element in the XML and the second parameter is the first part of the name of your item in OH. The routine appends and underscore and the day number to the OH item name, so in the example above, it updates this item:

Number BOM_Temp_Max_1 "Forecast max tomorrow [%.2f °C]" <temperature> (Weather) {mqtt=">[broker:/home/BOM/BOM_Temp_Max_1/state:state:*:default]"}

Tks,

As as a beginner (In Dutch a leek :slight_smile:), I try to understand your script.
What I do not understand (not yet) why te item is accessed through the classicui.
But patience will save Me :disappointed_relieved:

Bert

The Python script is running outside the OH environment and so does not have direct access to update items like you can do inside rules. There are likely to be many ways that the Python script could update the items, I just found the first and simplest one that worked for me. You could also use the REST API or MQTT…

The reason that “classicui” is involved is just to get it working under OH2.

Happy to help you further if this is not clear.

Andrew,

Thanks, for the offer, I will first take a moment to try to understand the script.
If I “crash” i let you know :grinning:

Bert

1 Like

Folks, just posted this over at another thread

1 Like

Hi Lads,

I realise I’m more than a year late to the party, but did anyone ever have a go at pulling up to date radar images from BOM?

Cheers
Liam

I haven’t attempted that.

I have a bash script that pulls down the current radar images, converts them to an animated GIF and is displayed in Habpanel. Happy to post it if you’re interested.

Sounds like a nice touch.Would really appreciate the script.)
Chris

Here it is:

#!/bin/sh

IMAGE=IDR033.gif
URL=http://www.bom.gov.au/radar/$IMAGE
DIR=/tmp/radar
WEBDIR=/var/www/html/dashboard

mkdir -p $DIR
cd $DIR

if [ ! -f last.gif ]
then
        touch last.gif
fi

wget -q --no-proxy --cache=off $URL
if [ ! -f $IMAGE ]
then
	# Appin radar is offline - use backup
	IMAGE=IDR713.gif
	URL=http://www.bom.gov.au/radar/$IMAGE
	wget -q --no-proxy --cache=off $URL
fi

cmp -s $IMAGE last.gif
if [ $? -eq 1 ]
then
    rm -f loop.gif
    mv -f img2.gif img1.gif 2>/dev/null
    mv -f img3.gif img2.gif 2>/dev/null
    mv -f img4.gif img3.gif 2>/dev/null
    mv -f img5.gif img4.gif 2>/dev/null
    cp $IMAGE img5.gif
    mv $IMAGE last.gif
	convert -delay 50 -loop 0 img*.gif -crop 524x564+0-45 -crop 524x564+0+20 -scale 85% +repage loop.gif > /dev/null 2> /dev/null
	cp loop.gif $WEBDIR
else
    rm -f $IMAGE
fi

Just to let everyone know, BOM is moving to RESTful API soon. They’ve got an early preview up at https://developer.bom.gov.au/ but it appears no way to register for a key just yet (registration pages just diverts to a login page).

Looks like it will make it nice and easy moving forward.

1 Like

Has anyone got this working?

Still cant register on their developer website

Hi Andrew

Where can I find this? I need to get weather functionality into OH2 for my sprinkler system

Cheers

Hi There

I tried your script and installed the lxml but i get errors executing it. Any ideas? I’m using 2.7 python from memory. I’m a bit scared to install V3


kris@openhab2:/etc/openhab2/scripts$ sudo ./weather.py
./weather.py: 1: ./weather.py: import: not found
from: too many arguments
./weather.py: 4: ./weather.py: urlBase: not found
./weather.py: 5: ./weather.py: url: not found
./weather.py: 7: ./weather.py: Syntax error: "(" unexpected
kris@openhab2:/etc/openhab2/scripts$

It seems that the import at line 1 is not working, which in my code is urllib, which should be part of the standard install: https://www.quora.com/How-do-I-install-urllib-and-urllib2-for-Python-3-3-2

OK, i modified it to include this:

#!/usr/bin/python
import urllib2
from lxml import etree

It works now but I get these errors, any thoughts?


Requesting file...
File received...
Updating OpenHAB...
    BOM_Temp_Max_0=24
    Error with air_temperature_maximum::element::0
    BOM_Precipitation_Range_0=2 to 10 mm
    Error with precipitation_range::element::0
    BOM_Forecast_Icon_Code_0=16
    Error with forecast_icon_code::element::0
    BOM_Precis_0=Showers increasing. Possible afternoon storm.
    Error with precis::text::0
    BOM_Precipitation_0=80%
    Error with probability_of_precipitation::text::0
    BOM_Temp_Max_1=26
    Error with air_temperature_maximum::element::1
    BOM_Temp_Min_1=17
    Error with air_temperature_minimum::element::1
    Error with precipitation_range::element::1
    BOM_Forecast_Icon_Code_1=3
    Error with forecast_icon_code::element::1
    BOM_Precis_1=Partly cloudy.
    Error with precis::text::1
    BOM_Precipitation_1=5%
    Error with probability_of_precipitation::text::1
End

Script:


#!/usr/bin/python
import urllib2
from lxml import etree

urlBase = 'http://192.168.0.3:8080/classicui/CMD?'
url = 'ftp://ftp.bom.gov.au/anon/gen/fwo/IDN10064.xml'

def setFWOItem(BOMName,OpenHABName,day,nodeType):
    try:
        value = tree.xpath("//forecast/area[@aac='NSW_PT131']/forecast-period[@index='" + day + "']/" + nodeType + "[@type='" + BOMName + "']/text()")[0]
        OpenHABName = OpenHABName + "_" + day
        print("    " + OpenHABName + "=" + value)
        value = urllib2.parse.quote_plus(value, safe='', encoding=None, errors=None)
        rawPage = urllib2.urlopen(urlBase + OpenHABName + '=' + value)
    except:
        print("    Error with " + BOMName + "::" + nodeType + "::" + day)


print('Requesting file...')
rawPage = urllib2.urlopen(url)
print('File received...')
read = rawPage.read()
tree = etree.XML(read)

print('Updating OpenHAB...')
rawPage = urllib2.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 = urllib2.urlopen(urlBase + 'BOMUpdating=OFF')
print('End')



@dastrix80 Do you still want my items/rules/scripts?

1 Like

Hi Andrew yes please!

I notice that you are using urllib2 rather than urllib, it may implement the commands in these two lines in a different way:

        value = urllib2.parse.quote_plus(value, safe='', encoding=None, errors=None)
        rawPage = urllib2.urlopen(urlBase + OpenHABName + '=' + value)