[internet sensor] speedtest.net measures

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

korniza
Posts: 157
Joined: Thursday 27 August 2015 18:12
Target OS: Raspberry Pi / ODroid
Domoticz version: V3.6028
Location: Greece
Contact:

[internet sensor] speedtest.net measures

Post 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?
>>>> Google Home <<<<<
SBC: Odroid XU4 * Raspberry Pi2 * banana Pi v1
Peripherals: rfxtrx433E, aeon z-stick gen5, bluetooth dongles
Extended Software packages: Xeoma (video NVR), FHEM (extra home automation software)
User avatar
gizmocuz
Posts: 2546
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: [internet sensor] speedtest.net measures

Post by gizmocuz »

Yes, add the 'Dummy' hardware, then press the button 'Create virtual sensor', and choose 'Custom Sensor' as type
Quality outlives Quantity!
User avatar
Westcott
Posts: 423
Joined: Tuesday 09 December 2014 17:04
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: UK - Glos
Contact:

Re: [internet sensor] speedtest.net measures

Post 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.
Zwave - Sigma Z+ stick, Fibaro, Horstmann, Neo Coolcam, EUROtronic
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
korniza
Posts: 157
Joined: Thursday 27 August 2015 18:12
Target OS: Raspberry Pi / ODroid
Domoticz version: V3.6028
Location: Greece
Contact:

Re: [internet sensor] speedtest.net measures

Post 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
>>>> Google Home <<<<<
SBC: Odroid XU4 * Raspberry Pi2 * banana Pi v1
Peripherals: rfxtrx433E, aeon z-stick gen5, bluetooth dongles
Extended Software packages: Xeoma (video NVR), FHEM (extra home automation software)
korniza
Posts: 157
Joined: Thursday 27 August 2015 18:12
Target OS: Raspberry Pi / ODroid
Domoticz version: V3.6028
Location: Greece
Contact:

Re: [internet sensor] speedtest.net measures

Post 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.
>>>> Google Home <<<<<
SBC: Odroid XU4 * Raspberry Pi2 * banana Pi v1
Peripherals: rfxtrx433E, aeon z-stick gen5, bluetooth dongles
Extended Software packages: Xeoma (video NVR), FHEM (extra home automation software)
User avatar
Westcott
Posts: 423
Joined: Tuesday 09 December 2014 17:04
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: UK - Glos
Contact:

Re: [internet sensor] speedtest.net measures

Post 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
Zwave - Sigma Z+ stick, Fibaro, Horstmann, Neo Coolcam, EUROtronic
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
korniza
Posts: 157
Joined: Thursday 27 August 2015 18:12
Target OS: Raspberry Pi / ODroid
Domoticz version: V3.6028
Location: Greece
Contact:

Re: [internet sensor] speedtest.net measures

Post 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:
>>>> Google Home <<<<<
SBC: Odroid XU4 * Raspberry Pi2 * banana Pi v1
Peripherals: rfxtrx433E, aeon z-stick gen5, bluetooth dongles
Extended Software packages: Xeoma (video NVR), FHEM (extra home automation software)
User avatar
Westcott
Posts: 423
Joined: Tuesday 09 December 2014 17:04
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: UK - Glos
Contact:

Re: [internet sensor] speedtest.net measures

Post 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.
Zwave - Sigma Z+ stick, Fibaro, Horstmann, Neo Coolcam, EUROtronic
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
User avatar
bizziebis
Posts: 182
Joined: Saturday 19 October 2013 14:00
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8805
Location: The Netherlands
Contact:

Re: [internet sensor] speedtest.net measures

Post 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
Derik
Posts: 1602
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: [internet sensor] speedtest.net measures

Post 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....
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
korniza
Posts: 157
Joined: Thursday 27 August 2015 18:12
Target OS: Raspberry Pi / ODroid
Domoticz version: V3.6028
Location: Greece
Contact:

Re: [internet sensor] speedtest.net measures

Post 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 :)
>>>> Google Home <<<<<
SBC: Odroid XU4 * Raspberry Pi2 * banana Pi v1
Peripherals: rfxtrx433E, aeon z-stick gen5, bluetooth dongles
Extended Software packages: Xeoma (video NVR), FHEM (extra home automation software)
User avatar
Richie1972
Posts: 44
Joined: Saturday 09 April 2016 10:23
Target OS: Windows
Domoticz version:
Contact:

Re: [internet sensor] speedtest.net measures

Post 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
Richie

Aeo S5 USB
3 FGSD002 Smoke
1 FGSM Motion
4 FGBS Binary
1 Horstmann ASR-ZW
2 Aeo Relay
1 FGS222 2xRelay
2 Aeo Multi Gen4
2 Everspring relay
1 Aeo Repeater
1 Popp Switch
1 TKB socket
4 Popp Dimmer
User avatar
Westcott
Posts: 423
Joined: Tuesday 09 December 2014 17:04
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: UK - Glos
Contact:

Re: [internet sensor] speedtest.net measures

Post 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?
Zwave - Sigma Z+ stick, Fibaro, Horstmann, Neo Coolcam, EUROtronic
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
korniza
Posts: 157
Joined: Thursday 27 August 2015 18:12
Target OS: Raspberry Pi / ODroid
Domoticz version: V3.6028
Location: Greece
Contact:

Re: [internet sensor] speedtest.net measures

Post 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
>>>> Google Home <<<<<
SBC: Odroid XU4 * Raspberry Pi2 * banana Pi v1
Peripherals: rfxtrx433E, aeon z-stick gen5, bluetooth dongles
Extended Software packages: Xeoma (video NVR), FHEM (extra home automation software)
Derik
Posts: 1602
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: [internet sensor] speedtest.net measures

Post 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
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
Mazzokun
Posts: 89
Joined: Thursday 28 April 2016 23:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Milan, Italy
Contact:

Re: [internet sensor] speedtest.net measures

Post 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?
curious
Posts: 132
Joined: Saturday 02 April 2016 19:38
Target OS: -
Domoticz version:
Contact:

Re: [internet sensor] speedtest.net measures

Post 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
User avatar
Westcott
Posts: 423
Joined: Tuesday 09 December 2014 17:04
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: UK - Glos
Contact:

Re: [internet sensor] speedtest.net measures

Post by Westcott »

Hi Curious,
Yes, first you have to get JSON.lua, see the relevant Wiki.
Zwave - Sigma Z+ stick, Fibaro, Horstmann, Neo Coolcam, EUROtronic
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
Derik
Posts: 1602
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: [internet sensor] speedtest.net measures

Post by Derik »

please some willl/can make this option so you login to the account of speedtest
...
More graphs...
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
curious
Posts: 132
Joined: Saturday 02 April 2016 19:38
Target OS: -
Domoticz version:
Contact:

Re: [internet sensor] speedtest.net measures

Post 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 ?
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest