Below you’ll find a complete solution on integrating the internet connection bandwidth speed test from Speedtest.net in your OpenHAB setup. This is accomplished by using the speedtest-cli
script, which you have to provide on your system.
Version Info: The guide was updated for openHAB 2.0 but should work on other versions as well.
Functionality:
- execute speed test once or twice a day, every hour or upon command
- show summarized results on the sitemap
- show all results on an extra page after click
- a button is defined to start the speedtest, either through the sitemap or your own rules
Installation and Test
Linux
#Install easy_install if not yet available
sudo apt-get install python-setuptools
# Installation of speedtest-cli
sudo easy_install speedtest-cli
# Testing
speedtest-cli
speedtest-cli --simple
Please make sure the script is working before you continue.
openHAB: You’ll need the Exec Binding. Please install and configure it in your preferred way.
Windows
You can find an unofficial speedtest-cli for Windows here: https://github.com/zpeters/speedtest
Place the executable on your harddrive, e.g. “C:\openHAB\speedtest.exe
”. All further steps are descried below.
OpenHAB files
Icons
All png and svg icons in one archive:
Click image to download
Extract all icons to the icons folder under $OPENHAB_CONF/icons/classic/
(openHAB 1.x: $OPENHAB_HOME/webapps/images/
)
Icons source (free for non-commercial use): http://www.iconarchive.com/show/polygon-icons-by-graphicloads.html
Items (speedtest.items
)
Group gSpeedtest <"network-icon"> (Whg)
String SpeedtestSummary "Speedtest [%s]" <"speedtest_network"> (gSpeedtest)
Number SpeedtestResultPing "Ping [%.3f ms]" <"speedtest_next5"> (gSpeedtest)
Number SpeedtestResultDown "Downlink [%.2f Mbit/s]" <"speedtest_download"> (gSpeedtest)
Number SpeedtestResultUp "Uplink [%.2f Mbit/s]" <"speedtest_upload"> (gSpeedtest)
String SpeedtestRunning "Speedtest running ... [%s]" <"speedtest_new"> (gSpeedtest)
Switch SpeedtestRerun "Start manually" <"speedtest_reload2"> (gSpeedtest)
DateTime SpeedtestResultDate "Last executed [%1$td.%1$tm.%1$tY, %1$tH:%1$tM Uhr]" <"speedtest_problem4"> (gSpeedtest)
Sitemap (Part of myhome.sitemap
)
...
Text item=SpeedtestSummary {
Frame label="Ergebnisse" {
Text item=SpeedtestResultDown
Text item=SpeedtestResultUp
Text item=SpeedtestResultPing
}
Frame label="Steuerung" {
Text item=SpeedtestResultDate
Text item=SpeedtestRunning label="Speedtest [%s]" visibility=[SpeedtestRunning != "-"]
Switch item=SpeedtestRerun mappings=[ON="Start"]
}
Frame label="Statistik" {
Text label="..." icon="speedtest_analytics8"
}
}
...
Side Note on Statistics: The three dots under statistics are just a placeholder for whatever statistics you want to generate and present out of your measurements over time (see persistence).
One option is to persist the introduced items to InfluxDB and am drawing Grafana graphs from them. These are then displayed under statistics. For more details check the InfluxDB+Grafana tutorial (a speedtest graph example can be seen in the middle of the first posting).
Rule (Linux speedtest.rules
)
val String filename = "speedtest.rules"
rule "Speedtest init"
when
System started
then
createTimer(now.plusSeconds(195)) [|
if (SpeedtestRerun.state == NULL) SpeedtestRerun.postUpdate(OFF)
if (SpeedtestRunning.state == NULL) SpeedtestRunning.postUpdate("-")
if (SpeedtestSummary.state == NULL || SpeedtestSummary.state == "")
SpeedtestSummary.postUpdate("⁉ (unbekannt)")
]
end
rule "Speedtest"
when
//Time cron "0 0 5,13 * * ?" or
Time cron "0 0 * * * ?" or
Item SpeedtestRerun received command ON
then
logInfo(filename, "--> speedtest executed...")
SpeedtestRunning.postUpdate("Messung läuft...")
// update timestamp for last execution
SpeedtestResultDate.postUpdate(new DateTimeType())
// execute the script, you may have to change the path depending on your system
var String speedtestCliOutput = executeCommandLine("/usr/local/bin/speedtest-cli@@--simple", 120*1000)
// for debugging:
//var String speedtestCliOutput = "Ping: 43.32 ms\nDownload: 21.64 Mbit/s\nUpload: 4.27 Mbit/s"
//logInfo(filename, "--> speedtest output:\n" + speedtestCliOutput + "\n\n")
SpeedtestRunning.postUpdate("Datenauswertung...")
// starts off with a fairly simple error check, should be enough to catch all problems I can think of
if (speedtestCliOutput.startsWith("Ping") && speedtestCliOutput.endsWith("Mbit/s")) {
var String[] results = speedtestCliOutput.split("\\r?\\n")
var float ping = new java.lang.Float(results.get(0).split(" ").get(1))
var float down = new java.lang.Float(results.get(1).split(" ").get(1))
var float up = new java.lang.Float(results.get(2).split(" ").get(1))
SpeedtestResultPing.postUpdate(ping)
SpeedtestResultDown.postUpdate(down)
SpeedtestResultUp.postUpdate(up)
SpeedtestSummary.postUpdate(String::format("ᐁ %.1f Mbit/s ᐃ %.1f Mbit/s (%.0f ms)", down, up, ping))
SpeedtestRunning.postUpdate("-")
logInfo(filename, "--> speedtest finished.")
} else {
SpeedtestResultPing.postUpdate(0)
SpeedtestResultDown.postUpdate(0)
SpeedtestResultUp.postUpdate(0)
SpeedtestSummary.postUpdate("(unbekannt)")
SpeedtestRunning.postUpdate("Fehler bei der Ausführung")
logError(filename, "--> speedtest failed. Output:\n" + speedtestCliOutput + "\n\n")
}
SpeedtestRerun.postUpdate(OFF)
end
// vim: syntax=Xtend
That’s it. If you have problems, just activate the logging lines and have a look in your openhab.log
to get an idea of what’s going on.
Rule (Windows speedtest.rules
)
The following changes are needed compared to the Linux rule above:
...
var String speedtestCliOutput = executeCommandLine("c:\\openHAB\\speedtest.exe@@--report", 120*1000)
...
if (speedtestCliOutput.startsWith("201")) {
var String[] results = speedtestCliOutput.split("\\|")
var float ping = new java.lang.Float(results.get(3))
var float down = new java.lang.Float(results.get(4))
var float up = new java.lang.Float(results.get(5))
SpeedtestResultPing.postUpdate(ping)
SpeedtestResultDown.postUpdate(down/1024)
SpeedtestResultUp.postUpdate(up/1024)
...