Page 1 of 3

[internet sensor] speedtest.net measures

Posted: Sunday 15 May 2016 11:41
by korniza
Hi guys,
I try to build a "sensor" for my home internet (adsl) connection. I found a node script that measure statistic using speedtest.net and produce a json output. You can find it on https://github.com/sindresorhus/speed-test

The output of the script produce the following:

Code: Select all

{"ping":115,"download":5.2,"upload":0.4}
Can I create a sensor for all values (respond ping, upstream Mbit/s, downstream Mbit/s) to a single entry as it is for temp/humid/pressure sensor?

Re: [internet sensor] speedtest.net measures

Posted: Sunday 15 May 2016 12:06
by gizmocuz
Yes, add the 'Dummy' hardware, then press the button 'Create virtual sensor', and choose 'Custom Sensor' as type

Re: [internet sensor] speedtest.net measures

Posted: Sunday 15 May 2016 12:36
by Westcott
That's a really neat feature, thanks for posting!
However, it takes over a minute to run on my PI, so I've added it as a cron job to write a file.
Then I can read the file into Domoticz, and JSON.decode it.

Re: [internet sensor] speedtest.net measures

Posted: Sunday 15 May 2016 12:43
by korniza
I created a virtual sensor as Custom sensor but it is just for one of the values (for example downstream). I like to combine all 3 values to 1 custom sensor.

I build the following broadbash.sh script and placed inside domoticz/script folder
Script requirements:
node
speedtest
Jq installed as described on link

You need to change the directory where cli.js is installed.
Also require to get the correct idx for the custom sensors you created as gizmocuz described.

Code: Select all

#!/bin/bash
# File: broadband.sh
# Check your internet connection using speedtest.net
# Version 0.2 || Last Update: 15 May 2016 

speedtest=`node /opt/node-v5.10.1-linux-armv7l/lib/node_modules/speed-test/cli.js --json | jq '.'`

echo $speedtest
ping=`echo $speedtest|cut -f1 -d","|cut -f2 -d":"|cut -f1 -d"}"| tr -d ' '`
downstream=`echo $speedtest|cut -f2 -d","|cut -f2 -d":"|cut -f1 -d"}"| tr -d ' '`
upstream=`echo $speedtest|cut -f3 -d","|cut -f2 -d":"|cut -f1 -d"}"| tr -d ' '`

#Vars
domoip=xxx.xxx.xxx.xxx
domoport=8080
ping_idx= xx
downstream= xx
upstream= xx

# debug
#       echo $speedtest
#       echo $ping
#       echo $downstream
#       echo $upstream

## ======= No Edit required below line ===========
curl -s -i -H "Accept: application/json" "http://$domoip:$domoport/json.htm?type=command&param=udevice&idx=$ping_idx&nvalue=0&svalue=$ping"
curl -s -i -H "Accept: application/json" "http://$domoip:$domoport/json.htm?type=command&param=udevice&idx=$downstream_idx&nvalue=0&svalue=$downstream"
curl -s -i -H "Accept: application/json" "http://$domoip:$domoport/json.htm?type=command&param=udevice&idx=$upstream_idx&nvalue=0&svalue=$upstream"
Also it required to get a cron job to your system. Remember that is use your internet banwidth, so I suggest a 15 minutes update or longer.

Code: Select all

*/15 *  * * *   root    /home/pi/domoticz/scripts/broadband.sh

Re: [internet sensor] speedtest.net measures

Posted: Sunday 15 May 2016 13:04
by korniza
Westcott wrote:That's a really neat feature, thanks for posting!
However, it takes over a minute to run on my PI, so I've added it as a cron job to write a file.
Then I can read the file into Domoticz, and JSON.decode it.
The reason of that takes a while is that use the speedtest.net mechanism that uploads/downloads a file and feedback the recorded measurements. The values do not report the real upstream/downstream. it use the free bandwith of your line. The meaning is to get a smokeping like measurement as also a scenario if ping is too high, take action.

Re: [internet sensor] speedtest.net measures

Posted: Sunday 15 May 2016 18:09
by Westcott
A somewhat different approach - first I use crontab to capture the Json data to a file once per hour -

Code: Select all

58 * * * * /usr/local/bin/speed-test --json >/home/pi/domoticz/speed-test.json 2>&1
Then 3 minutes later I read it with Lua, and decode the Json data -

Code: Select all

JSON  = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
date = os.date("*t")
if (date.min == 1) then
    file = io.open("speed-test.json", "r")
	json_text = file:read()
	speeds = JSON:decode(json_text)
	print("-- Ping="..speeds.ping.." Upload=".. speeds.download)
end

Re: [internet sensor] speedtest.net measures

Posted: Sunday 15 May 2016 20:30
by korniza
Westcott wrote:A somewhat different approach - first I use crontab to capture the Json data to a file once per hour -

Code: Select all

58 * * * * /usr/local/bin/speed-test --json >/home/pi/domoticz/speed-test.json 2>&1
Then 3 minutes later I read it with Lua, and decode the Json data -

Code: Select all

JSON  = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
date = os.date("*t")
if (date.min == 1) then
    file = io.open("speed-test.json", "r")
	json_text = file:read()
	speeds = JSON:decode(json_text)
	print("-- Ping="..speeds.ping.." Upload=".. speeds.download)
end
Nice!
Do you create a graph/sensor for these values? Don't take me wrong, I'm not an expert programmer :oops:

Re: [internet sensor] speedtest.net measures

Posted: Sunday 15 May 2016 20:51
by Westcott
Yes, a pair of custom sensors is the next step, with axes of 'ms' and 'MB/s'.
I put this code together quickly to make sure the values were captured correctly.

Re: [internet sensor] speedtest.net measures

Posted: Sunday 15 May 2016 21:29
by bizziebis
Nice idea. Mine looks like this now:

ImageImage

With a little addition to the script posted above:

Code: Select all

commandArray = {}

JSON  = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
date = os.date("*t")

if (date.min == 50) then
    file = io.open("speed-test.json", "r") 
    json_text = file:read()
    speeds = JSON:decode(json_text)
    print("Ping="..speeds.ping.." Download=".. speeds.download.." Upload=".. speeds.upload)

    commandArray[1]={['UpdateDevice']='930|0|'..speeds.download}
    commandArray[2]={['UpdateDevice']='931|0|'..speeds.upload}
    commandArray[3]={['UpdateDevice']='932|0|'..speeds.ping}
end

return commandArray

Re: [internet sensor] speedtest.net measures

Posted: Sunday 15 May 2016 22:23
by Derik
Heloo

Looking nice...
No internet send mail...
What is the best script.?

Please can some place all the scripts together, so a domdo like me make this option work....

Re: [internet sensor] speedtest.net measures

Posted: Sunday 15 May 2016 22:43
by korniza
@bizziebis: look nice and suppose it gets less resources that using curl to update values. I think can be a complete sensor for internet that can route notifications via SMS if ping is lost. Also is we had all the values in the same sensor as it is done on weather sensors will be great (another big story).
I know, if you like it just do it...but I m not think myself as a coder :)

Re: [internet sensor] speedtest.net measures

Posted: Monday 16 May 2016 12:53
by Richie1972
This looks to be a great script to include.
I'm running on a Windows system, so does anyone have a completely Lua solution rather than Bash?

Thanks,
Richie

Re: [internet sensor] speedtest.net measures

Posted: Monday 16 May 2016 13:15
by Westcott
Hi Richie,
The speed-test 'app' seems to be only Linux.
It has to run separately 'cos it takes over a minute.
Its JSON output can be read directly by Lua (see above).
Perhaps there is a Windows equivalent that can also write a Json file?

Re: [internet sensor] speedtest.net measures

Posted: Monday 16 May 2016 15:58
by korniza
Westcott wrote:Hi Richie,
The speed-test 'app' seems to be only Linux.
It has to run separately 'cos it takes over a minute.
Its JSON output can be read directly by Lua (see above).
Perhaps there is a Windows equivalent that can also write a Json file?
speed-test is a js. suppose it can run using node js for windows. I'm sure because I do not have any windows domoticz installed

Re: [internet sensor] speedtest.net measures

Posted: Sunday 22 May 2016 9:19
by Derik
perhaps possible to create a script include login by speedtetst..

And what is the complete best script i see now some one use this and other use that..
Please one script

Re: [internet sensor] speedtest.net measures

Posted: Wednesday 01 June 2016 7:32
by Mazzokun
korniza wrote:I created a virtual sensor as Custom sensor but it is just for one of the values (for example downstream). I like to combine all 3 values to 1 custom sensor.

I build the following broadbash.sh script and placed inside domoticz/script folder
Script requirements:
node
speedtest
Jq installed as described on link

You need to change the directory where cli.js is installed.
Also require to get the correct idx for the custom sensors you created as gizmocuz described.
I tried with your method, if I type by terminal ./broadband.sh then the script works well and update correctly the virtual devices.
The cronjob doesn't work properly: it updates the virtual devices to 0 every time (15 min) it's executed. :shock: why?

Re: [internet sensor] speedtest.net measures

Posted: Sunday 19 June 2016 11:59
by curious
Like Derik, I am struggling to put all files in the right place :

What i did : copied broadband.sh to /domoticz/scripts
in crontab added lines like
*/10 * * * * sudo ~/domoticz/scripts/broadband.sh
*/10 * * * * /usr/local/bin/speed-test --json >/home/pi/domoticz/speed-test.json 2>&1
in the lua scripts directory I have this file named: script_device_internetspeed.lua
with this code

Code: Select all

JSON  = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
date = os.date("*t")
if (date.min == 1) then
    file = io.open("speed-test.json", "r")
   json_text = file:read()
   speeds = JSON:decode(json_text)
   print("-- Ping="..speeds.ping.." Upload=".. speeds.download)
end

commandArray = {}

JSON  = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
date = os.date("*t")

if (date.min == 50) then
    file = io.open("speed-test.json", "r") 
    json_text = file:read()
    speeds = JSON:decode(json_text)
    print("Ping="..speeds.ping.." Download=".. speeds.download.." Upload=".. speeds.upload)

    commandArray[1]={['UpdateDevice']='131|0|'..speeds.download}
    commandArray[2]={['UpdateDevice']='132|0|'..speeds.upload}
    commandArray[3]={['UpdateDevice']='133|0|'..speeds.ping}
end

return commandArray
However : shouldn't there be a file called JSON.LUA ?
Is there a package I shpould install ?

Thnx for your help

Re: [internet sensor] speedtest.net measures

Posted: Sunday 19 June 2016 12:10
by Westcott
Hi Curious,
Yes, first you have to get JSON.lua, see the relevant Wiki.

Re: [internet sensor] speedtest.net measures

Posted: Sunday 19 June 2016 15:28
by Derik
please some willl/can make this option so you login to the account of speedtest
...
More graphs...

Re: [internet sensor] speedtest.net measures

Posted: Tuesday 21 June 2016 21:25
by curious
I copied the complete JSON.lua to the lua scripts directory.

Because I didn't get results I made a test lua with only this code :

Code: Select all

JSON  = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
In the log I get next error message
2016-06-21 21:20:38.328 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_test.lua: /home/pi/domoticz/scripts/lua/script_device_test.lua:1: attempt to call a nil value

Should I modify the JSON.lua or should I copy it to an other folder ?