Grab data from a website by polling and trigger events?


#1

I would like to have the final entry in the HN24 cm column from http://mountainweather.ca/data/STONEHENGE.HTM available as a tile/device, so I can trigger the so-called “20cm rule” at the local ski hill. What the best way to poll a website and get the date into my home automation system? Can I have my ST hub do the polling or do I need to install a service on my Mac? Or is something available through IFTTT?


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #2

Your “hub” won’t do the polling… Remember that custom SmartApps run only in the SmartThings Cloud

Regardless, yes… you can create a “cloud to cloud” Device type and have a SmartApp schedule a periodic refresh. I would not have it poll too frequently though; that’s discouraged and unnecessary.

http://docs.smartthings.com/en/latest/cloud-and-lan-connected-device-types-developers-guide/understanding-the-service-manage-device-handler-design-pattern.html#cloud-connected-devices


#3

Thanks. Is there a recommended library/routine/pattern for extracting data from an HTML page? (Or maybe the page is available as a JSON.)


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #4

The SmartThings “http” libraries available to DHs and SmartApps are rather self-explanatory.

http://docs.smartthings.com/en/latest/ref-docs/device-handler-ref.html#httpget


NB: Not saying this is a better or worse way to go, but … WebCoRE is a popular tool for building automations without writing SmartApp code. I don’t know all its features, but it might include some basic REST-API query functionality. https://community.webcore.co/


#5

This is correct. You can poll the API with webcore, then create a tile, email it or text it to yourself, or whatever. @music_and_cities Hit me up on the webcore forum if you’d like more information on this.


#6

I ended up writing a bash script to run every 15 minutes on my Mac, which sends it to IFTTT. On my Mac I could use a combination of nokogiri (for parsing the html) tail (for getting the most recent obversvation) awk (for getting the 8th entry) and head (for removing the extra line). I don’t think nokogiri, tail, awk, and head are available in the Smartthings platform.

echo `date '+%Y-%m-%d %H:%M:%S'`
snow=$(/usr/local/bin/nokogiri -e 'print @doc.at_css("body/pre")' http://mountainweather.ca/data/STONEHENGE.HTM | tail -2 | awk -F " " '{print $8}' | head -1)
echo "snow is" $snow

if (( $(echo "$snow > $1" | bc -l) )); then 
    curl https://maker.ifttt.com/trigger/24_new_snow/with/key/igge-this-isnt-my-real-key-tho-If?value1=$snow
else
    echo $snow "is less than "$1" cm :("
fi

If mountainweather.ca supported json I’m sure I could have done it in the SmartThings platform, but they seem to only have html.

There’s some snow in the forecast in the next few days, so hopefully I’ll be able to both test my notifications and also go skiing in some fresh pow.