[SOLVED] Weather forecast text2speech

hi,
I´ve installed a couple of squeezbox devices throughout my house for listening to music and to get notifications from openhab. Next I thought about streaming a weather forecast podcast to one of the devices but unfortunately there are no such podcasts available for Austria (at least I didn´t find one). I could use info out of one of the weather bindings but assembling text together results in just very simple sentences. A more qualitative way would be to download text from one of the weather forecast texts from the internet and stream that to squeezebox. the problem is how to download text from a homepage and insert it to openhab (eg. as a string item)?
in detail I would like to hear the text for the weather forecast from this URL: https://www.zamg.ac.at/cms/de/wetter/wetter-oesterreich/salzburg/heute_vormittag
streaming text to squeezebox is not the problem, I need to know how to download text from a homepage to openhab.
thanks for your ideas!

You will need to use the HTTP binding or sendHttpGetRequest to fetch the webpage. Then use the REGEX transformation to extract that text from the web page. Unfortunately, I don’t think the text is actually part of the source on the web page and is instead fetched separately. So you will need to find an API from this service, or find an alternative service that provides the text as part of the web page that gets returned on the initial GET.

I found a way to scrape a website with python:


It works really easy and I`m able to extract the text portion I want into a textfile. Now, how do I get the textfile into a string item so I can further work with it?

If anyone is interested in my scrape script, here is a very short manual:

sudo easy_install pip 
pip install BeautifulSoup4
sudo nano scrape.py

scrape.py:

# import libraries
import urllib2
from bs4 import BeautifulSoup

# specify the url
quote_page = "https://www.zamg.ac.at/cms/de/wetter/wetter-oesterreich/salzburg/heute_nachmittag"

# query the website and return the html to the variable "page"
page = urllib2.urlopen(quote_page)

# parse the html using beautiful soup and store in variable "soup"
soup = BeautifulSoup(page, "html.parser")

# Take out the <div> of name and get its value
name_box = soup.find("div", attrs={"id": "prognosenText"})

name = name_box.text.encode('utf-8').strip() # strip() is used to remove starting and trailing, utf-8 wegen umlauten
head, sep, tail = name.partition("aktualisiert") # entfernet alles inkl. nach aktualisiert am...
# print head

file = open("wettervorhersage.txt","w")
file.write(head)
file.close()

start script with

python scrape.py

You have lots of options. Probably the best option would be to have your Python script sendCommand the text through the OH REST API.

Sending the message as MQTT would be easy as well.

You can call the Python from a Rule using executeCommandLine or the Exec binding.

You can use JSR223, import the library and write a Jython Rule.

I can’t believe it! that works on the second try!!!
the API is really easy to access, only 2 lines of code?! here is the complete script:


# import libraries
import urllib2
from bs4 import BeautifulSoup
import requests

# specify the url
quote_page = "https://www.zamg.ac.at/cms/de/wetter/wetter-oesterreich/salzburg/heute_nachmittag"

# query the website and return the html to the variable "page"
page = urllib2.urlopen(quote_page)

# parse the html using beautiful soup and store in variable "soup"
soup = BeautifulSoup(page, "html.parser")

# Take out the <div> of name and get its value
name_box = soup.find("div", attrs={"id": "prognosenText"})

name = name_box.text.encode('utf-8').strip() # strip() is used to remove starting and trailing
head, sep, tail = name.partition("aktualisiert")
# print head

file = open("wettervorhersage.txt","w")
file.write(head)
file.close()

requests.put("http://192.168.1.151:8080/rest/items/Wettervorhersageitem/state", data=head)

2018-10-29 20:20:40.804 [vent.ItemStateChangedEvent] - Wettervorhersageitem changed from NULL to In der Nacht gibt es verbreitet Sturm und wechselhaftes Wetter mit Regenschauern und im Norden aufgelockerten Wolken. In den Tauerntälern wird es lange anhaltend und bis Mitternacht stark regnen. Die Temperaturen sinken auf 10 bis 2 Grad. In Föhnschneisen können die Windspitzen in den Stunden knapp vor Mitternacht über 100 km/h erreichen, lokal ist orkanartiger Sturm möglich.

Why not use a home assistant such as alexa?

I was playing around with Google assistant, but was not really happy:
1.) asking for the weather gives just a very simple info about temperature and sky condition, little more than having a look out of the window.
2.) calling “OK Google” was funny the first days, my wife can’t hear that anymore.
3.) I don’t want to purchase Alexa or Google for every room.

the original idea was to play a podcast which is recorded by a human but there are no weather podcasts for austria. I run a news podcast which is automatically played when I spend my 10mins in the bathroom.

however, I guess there are much more meaningful usecases for scraping text from websites and forwarded to OH than just reading the weather forecast.