Run speed test at random times

I’m trying to run a speed test (using Speedtest.net sensor) at random times during the day.

I’ve set the sensor to manual and call it using an automation, so I can add conditions. Here is my automation:

- id: speed_tests
  alias: 'Run speed test periodically'
  trigger:
    - platform: time
      at: '06:00:00'
    - platform: time
      at: '12:00:00'
    - platform: time
      at: '18:00:00'
  condition:
    # conditions
  action:
    - delay: '{{ (range(0, 4)|random|int) }}:{{ (range(1, 59)|random|int) }}:00'
    - service: sensor.update_speedtest

Unfortunately the delay is not working, the automation runs at exactly 06:00, 12:00 etc.

I’ve tried creating a random time trigger but that doesn’t seem to work either:

  trigger:
    platform: time
    hours: '/{{ (range(6, 9)|random|int) }}'
    minutes: 0
    seconds: 0

Any other ideas?

The closest I’ve come to this is using a random delay after a fixed time_pattern trigger.

Randomising time_pattern doesn’t seem to work, but triggering an automation every 5 hours that delays with a random range of 0 to 4hr 50min achieves a similar result:

- id: speed_tests
  alias: "Run speed test randomly"
  trigger:
    platform: time_pattern
    hours: "/5"
  action:
    - delay: "{{ (range(00,04)|random|int) }}:{{ (range(00,50)|random|int) }}:00"
    - condition: state
      entity_id: binary_sensor.gaming_pc
      state: "off"
    - service: speedtestdotnet.speedtest

Unfortunately I’ve had to remove the automation for this case though, due to incorrect speed test results: HassOS, Raspberry Pi 3 Model B+, slow speedtest and fast.com

Is it possible that, in the first example, the issue with delay’s template is that it does not zero-pad single digits so sometimes it produces strange time formats like this:

Screenshot%20from%202019-09-07%2008-08-29

The following template produces zero-padded values:

{{ '{0:02d}:{1:02d}:00'.format(range(0, 4)|random, range(1, 59)|random) }}

Sorry, do you prefer Taras or 123 ?
That’s very interesting, I’ve spent some time looking over this and I still can’t see what’s going on.
Petros explained adequately why you need to be consistent with quotes (single versus double) and I sort of understand that enclosing in single { } brackets means “this is an embedded command, interpret it as a command” and double {{ }} means “interpret this as a value and display as such”
Not sure how accurate that is as most of my templating (as you have personally seen) is done throwing speculative requests at the JSON interpreter. The above padding technique is a new one on me. Can you recommend a resource to read up on JSON, especially as it relates to implementation for HA.?
Thanks in advance
Mutt

@123 thanks yeah the delay in my first example isn’t great. I tried to account for padding in the delay in my second example though, that should work right? Or do I need to use your different syntax?

Anyway, my last frustration here is you don’t seem to be able to template the time trigger, which would be great if it worked:

  trigger:
    platform: time
    hours: '/{{ (range(6, 9)|random|int) }}'

Either works for me.

The above padding technique is a new one on me.

It’s a python thing and your interpretation of it is correct. Here’s another example:

{{ "The {0} brown {1} jumped {3} the lazy dog's {2}!".format('quick', 'fox', 'back', 'over') }}

This is good reference for python’s string formatter:
https://pyformat.info/

I tried it in the Template Editor and it produced numbers without zero-padding:
Screenshot%20from%202019-09-07%2012-59-43

I believe the Time Trigger does not accept a template.

1 Like