Speedtestdotnet write log file and run python script to manipulate results

Hi all I have installed speedtest on hassio and I would like that function run hourly. I would like results of ping, download speed and upload speed have been written in a log file and I would like run a python script to manipulate results in order to check internet connection and data results.
This is possible? thank you very much

Yes it is all do-able.

The speedtest integration can be set to an hourly run schedule whether you use the UI or yaml, or run it via an automation on a time pattern trigger (better control of the actual time it runs, rather than the time between runs).

You can write the sensors to a file using the file notification service:

The last bit depends on your python programming skill and running a python script:

Though it may be possible to do what you want in an automation and skip the file and python script altogether.

thank you very much Tom
Until now I added the following lines in configuration.yaml:


speedtestdotnet:
  scan_interval:
    minutes: 30
  monitored_conditions:
    - ping
    - download
    - upload

I’m new in hassio and I would like learn it to implement a lot of things. For the first step can I ask you how to call the results of the speedtest to write them in a file.

Thank you for your availability and patient

Before we go to all the trouble of doing that can you explain what you want to do with the speedtest results in the python script?

You may not need to write to a file.

I would like make an average of results during day and find at what time I have min speed and max speed. Then when speedtest ping have problems or internet connection failed, I would like activate a gpio which turn off modem and return on it.

Yeah ok. I can’t see how that can be easily done with a statistics sensor. It can give you the minimum for the day but not the time that this occurred.

Add a file notification service:

notify:
 - platform: file
   name: speedtest_log
   filename: /config/www/speedtest_log.csv
   timestamp: false

Then automate writing the sensor values to the log. Ideally you should run this 5 minutes or so after a speedtest. So, you need to alter that to only run manually too.

speedtestdotnet:
  manual: true
  monitored_conditions:
    - ping
    - download
    - upload

Automation:

trigger:
  platform: time_pattern # Matches every hour at 7 minutes past the hour
  minutes: 7
action:
  - service: speedtestdotnet.speedtest # do the speed test
  - delay:
      minutes: 5 # wait for it to finish then...
  - service: notify.speedtest_log # write results to the log file: download, upload, ping
    data_template:
      message: "{{ states('sensor.speedtest_download') }}, {{ states('sensor.speedtest_upload') }}, {{ states('sensor.speedtest_ping') }}"

That’s as far as I can get you. I can’t help you with the python. Though…

None of that requires the file and python. All you have to do is trigger an automation when either the upload, ping or download goes below or above a set level (numeric state trigger). e.g.

trigger:
  platform: numeric_state
  entity_id: sensor.speedtest_download
  below: 1
action: 
  - service: switch.turn off
    entity_id: switch.your_modem_switch
  - delay:
      seconds: 10
  - service: switch.turn on
    entity_id: switch.your_modem_switch
  - service: notify.myphone
    data_tempalte:
      message: " The modem has been reset, download speed = {{ states('sensor.speedtest_download') }} MB/s. "

You can also trigger the automation by a state change of one of the Speedtest sensors and optionally add a delay action for a minute.

I think this can be achieved with the statistics sensor if you want it over the last 24 hours instead of a calendar day.

Yeah I missed that. Statistics sensor would be the way to go for that value.

thank you very much,
I will try with some test

Hi,

I’m new to home assistant.
I don’t know if anyone is still looking at this post.
I followed the post but I used the visual editor, made a few tweaks, and it works kind of.
If I manually trigger the automation by going into Automations and select RUN ACTIONS, I get the data in the log file but if I let it run by itself I get unavailable for the download, upload and ping values.
Here is a copy of the output. I changed my ExternalIP but it is showing the correct data every time.

“Download, Upload, Ping, ExternalIP, Date, Time”
“23.5, 5.82, 59, x.x.x.x, 2022-02-08, 01:40:00” ← manually run
“22.67, 5.33, 56, x.x.x.x, Tuesday February 8, 02:03 AM” ← manually run
“unavailable, unavailable, unavailable, x.x.x.x, Tuesday February 8, 02:35 AM”
“unavailable, unavailable, unavailable, x.x.x.x, Tuesday February 8, 03:05 AM”
“unavailable, unavailable, unavailable, x.x.x.x, Tuesday February 8, 03:35 AM”
“24.62, 5.83, 53, x.x.x.x, Tuesday February 8, 03:40 AM” ← manually run

Here is what I added to /config/configuration.yaml

notify:
 - platform: file
   name: speedtest_log
   filename: /share/speedtest_log.csv
   timestamp: false
   
speedtestdotnet:
  manual: true
  monitored_conditions:
    - ping
    - download
    - upload

Here is the /config/automations.yaml

- id: '1644299020560'
  alias: Speedtest
  description: ''
  trigger:
  - platform: time_pattern
    minutes: /30
  condition: []
  action:
  - service: speedtestdotnet.speedtest
    data: {}
  - delay:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  - service: notify.speedtest_log
    data:
      message: '"{{ states(''sensor.speedtest_download'') }}, {{ states(''sensor.speedtest_upload'')
        }}, {{ states(''sensor.speedtest_ping'') }}, {{ states(''sensor.myip'') }},
        {{ states(''sensor.date_and_time'') }}"'
  mode: single