Skip to content

Instantly share code, notes, and snippets.

@aallan
Last active May 29, 2022 15:50
Show Gist options
  • Star 38 You must be signed in to star a gist
  • Fork 27 You must be signed in to fork a gist
  • Save aallan/bafc70a347f3b9526d30 to your computer and use it in GitHub Desktop.
Save aallan/bafc70a347f3b9526d30 to your computer and use it in GitHub Desktop.
Modified version of Henrik Bengtsson's speedtest-cli code which will dispatch the test results to the IFTTT Maker Channel.
#!/usr/bin/env bash
###########################################################################
# Originally written by: Henrik Bengtsson, 2014
# https://github.com/HenrikBengtsson/speedtest-cli-extras
# Modified to use IFTTT by: Alasdair Allan, 2015
# License: GPL (>= 2.1) [http://www.gnu.org/licenses/gpl.html]
###########################################################################
# Character for separating values
# (commas are not safe, because some servers return speeds with commas)
sep=";"
# Temporary file holding speedtest-cli output
user=$USER
if test -z $user; then
user=$USERNAME
fi
log=/tmp/$user/speedtest-csv.log
# Local functions
function str_extract() {
pattern=$1
# Extract
res=`grep "$pattern" $log | sed "s/$pattern//g"`
# Drop trailing ...
res=`echo $res | sed 's/[.][.][.]//g'`
# Trim
res=`echo $res | sed 's/^ *//g' | sed 's/ *$//g'`
echo $res
}
# Display header?
if test "$1" = "--header"; then
start="start"
stop="stop"
from="from"
from_ip="from_ip"
server="server"
server_dist="server_dist"
server_ping="server_ping"
download="download"
upload="upload"
share_url="share_url"
else
mkdir -p `dirname $log`
start=`date +"%Y-%m-%d %H:%M:%S"`
if test -n "$SPEEDTEST_CSV_SKIP" && test -f "$log"; then
# Reuse existing results (useful for debugging)
1>&2 echo "** Reusing existing results: $log"
else
# Query Speedtest
/usr/local/bin/speedtest-cli --share > $log
fi
stop=`date +"%Y-%m-%d %H:%M:%S"`
# Parse
from=`str_extract "Testing from "`
from_ip=`echo $from | sed 's/.*(//g' | sed 's/).*//g'`
from=`echo $from | sed 's/ (.*//g'`
server=`str_extract "Hosted by "`
server_ping=`echo $server | sed 's/.*: //g'`
server=`echo $server | sed 's/: .*//g'`
server_dist=`echo $server | sed 's/.*\\[//g' | sed 's/\\].*//g'`
server=`echo $server | sed 's/ \\[.*//g'`
download=`str_extract "Download: "`
upload=`str_extract "Upload: "`
share_url=`str_extract "Share results: "`
fi
# Standardize units?
if test "$1" = "--standardize"; then
download=`echo $download | sed 's/Mbits/Mbit/'`
upload=`echo $upload | sed 's/Mbits/Mbit/'`
fi
# Send to IFTTT
secret_key="SECRET_KEY"
value1=`echo $server_ping | cut -d" " -f1`
value2=`echo $download | cut -d" " -f1`
value3=`echo $upload | cut -d" " -f1`
json="{\"value1\":\"${value1}\",\"value2\":\"${value2}\",\"value3\":\"${value3}\"}"
curl -X POST -H "Content-Type: application/json" -d "${json}" https://maker.ifttt.com/trigger/speedtest/with/key/${secret_key}
@HughP
Copy link

HughP commented Mar 17, 2018

Is there a way to record the mac address of the triggering machine?

@davedavis
Copy link

@mrminus Did you ever figure that out? Could you share?

@GenerlAce
Copy link

is this still working? i cannot for the life of me get this to create a spreadsheet on google.

@gitricko
Copy link

me too !

@dantgal
Copy link

dantgal commented Oct 18, 2018

Doesn't work anymore, probably cause of changes with API Google:
Applet failed
Oct 18 - 10:44 PM
Webhooks
Log Speedtest results to Spreadsheet
There was a problem with the Google Sheets service.

@RealDyllon
Copy link

@dantgal

The script does work if you follow the instructions here: http://grantglazer.com/automatically-log-your-internet-speeds-with-a-raspberry-pi/

If you still experience the Google Sheets error, the disconnect the Google Drive service in IFTTT, and reconnect it. That should fix your issue.

@mistergrumpy
Copy link

The script doesn't work. I think it's something to do with IFTT and Google and the triggers.

@paperjam2032
Copy link

i got it working previously, but now, im trying to look for the secretkey in ifttt.com. its just not there anymore.
i know thati need the secretkey to get it working tho.

@jackdanpr
Copy link

Hi - i was wondering if there was a way to get this to pass a 4th value to the IFTTT channel.

I added the relevant values here (value4)

value1=echo $server_ping | cut -d" " -f1
value2=echo $download | cut -d" " -f1
value3=echo $upload | cut -d" " -f1
value4=echo $server | cut -d" " -f1
json="{"value1":"${value1}","value2":"${value2}","value3":"${value3}","value4":"${value4}"}"
curl -X POST -H "Content-Type: application/json" -d "${json}" https://maker.ifttt.com/trigger/speedtest/with/key/${secret_key}

But when i go to the IFTTT applet and att |||{{value4}} to the applet it says there is an error and wont let me save :(

I know it's an old post, but, did you ever figure it out? It's suppose to parse the new value but doesn't work...
Thanks,

@aallan
Copy link
Author

aallan commented Mar 4, 2020 via email

@kkasberg
Copy link

I am getting leading decimals in my output.

@kkasberg
Copy link

kkasberg commented Jun 1, 2020

I am getting leading decimals in my output.

I was able to resolve this using sed to replace leading decimal points with nothing.

github

@jonmoore-pulse
Copy link

I am not getting anything in google drive. When clicking the check now in the IFTTT recipe I get a 'There was an error during check process' message. Any ideas??

@kkasberg
Copy link

I am not getting anything in google drive. When clicking the check now in the IFTTT recipe I get a 'There was an error during check process' message. Any ideas??

Did you set the trigger in ifttt to "speedtest"

@jonmoore-pulse
Copy link

@kkasberg sorry I am new to IFTTT but if you mean the title of the applet then yes.

Screenshot 2020-06-17 at 18 18 33

@kkasberg
Copy link

kkasberg commented Jun 17, 2020

@kkasberg sorry I am new to IFTTT but if you mean the title of the applet then yes.

Screenshot 2020-06-17 at 18 18 33

I mean where it says "Event Name" (blank above) use "Speedtest".

@jonmoore-pulse
Copy link

jonmoore-pulse commented Jun 17, 2020

https://maker.ifttt.com/trigger/speedteest/with/key/ gives me:
Congratulations! You've fired the speedtest event

But still nothing in google drive

@kkasberg
Copy link

https://maker.ifttt.com/trigger/speedteest/with/key/ gives me:
Congratulations! You've fired the speedtest event

But still nothing in google drive

It will give that result because the event fired is "speedtest". I in ifttt you don't enter that as the event name it won't post to google sheets.

@jonmoore-pulse
Copy link

I see this in the activity:
Screenshot 2020-06-17 at 18 26 44

@kkasberg
Copy link

I see this in the activity:
Screenshot 2020-06-17 at 18 26 44

It is saying that your trigger field is still empty. It needs to be "speedtest" under "Event Name".

@jonmoore-pulse
Copy link

Ok I have added that and getting somewhere now however nothing appears in Google Sheets unless I hit the Check Now button in IFTTT. Doing this seems to then send all the 'queued' entries.
What could be causing this?

@jonmoore-pulse
Copy link

It actually seems to do nothing for a while then several readings all come through together. I have it set to every 15mins in the cronjob but it takes a few hours to get written to google sheets.

@kkasberg
Copy link

It actually seems to do nothing for a while then several readings all come through together. I have it set to every 15mins in the cronjob but it takes a few hours to get written to google sheets.

This happened to me also, and I resolved it by deleting my webhook secret key and having a new one created (then adding the new one back into the scripts).

@jonmoore-pulse
Copy link

Thanks @kkasberg I will try that.

@jonmoore-pulse
Copy link

Working!

@johenkel
Copy link

Everything was working great. On the sudden the script started filling it into a new spreadsheet speedtest(1) .... not sure where the copy came from and why its just posting it in there.

@aallan
Copy link
Author

aallan commented Nov 19, 2020

Everything was working great. On the sudden the script started filling it into a new spreadsheet speedtest(1) .... not sure where the copy came from and why its just posting it in there.

Google Sheets have a maximum number of rows. Once you exceed that number it will auto-magically generate a new sheet.

@JeremyDurnell
Copy link

This has been working great since I started using it over a year ago, but it looks like something has changed on the IFTTT platform. The recipe, and my applet, both 404 now. It looks like it stopped working on March 8, 2021 around 2 PM CST. I'm wondering if anyone has found a workaround?

@RandomSnake
Copy link

@JeremyDurnell Ran into the same issue on the same date but only noticed now. I found that IFTTT was not at fault as the webhook and the speadsheet update commands had not changed.

The issue I found was due to an update on the speedtest side which was failing due to some server changes. The error I receive when running the command manually is:
ValueError: invalid literal for int() with base 10

There are 2 solutions I have found.

  1. Setup your speed tests from scratch with the updated configs (not from aallan at the time of this post)

  2. Modify the speedtest.py file manually by;
    Open up speedtest.py file (Mine was located: /usr/local/lib/python2.7/dist-packages/speedtest.py)
    Head to line 1174
    Comment it out and replace with the new line from the below article:
    sivel/speedtest-cli@6e9a667

Hopefully this helps you or the next person that runs into this

Copy link

ghost commented May 26, 2021

Hey! What IFTTT applets are you using? I can't find "speedtest" nor "Log Speedtest results to Spreadsheet"...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment