Create random time between 8AM and 5PM?

I want to create a random time between 8AM and 5PM (so, for example, 13:39), that triggers the start of an automation. I’ve seen solutions where the waiting time is randomised (triggered at 8AM with a random waiting time), but I wondered if there’s a more elegant solution so that a timer isn’t necessarily running the whole day.

So:

alias: Automation Lorum Ipsum
description: ""
trigger:
  - platform: time
    at: "13:39:00"

But then something like

alias: Automation Lorum Ipsum
description: ""
trigger:
  - platform: time
    at: "(random hour between 8-17):(random minute between 0-59):00"

I can imagine there needs to be some sort of other automation filling in the time variable, that does trigger at 8AM, so the time is set correctly each day.

For context: I want a sound to play on our speakers whenever it’s this random time, at a set (low) volume, so we can start with exposure training with our dog.

How can this be managed?

Someone else had a similar request (turn and off at random times) and I suggested creating a Trigger-based Template Sensor that computes a new random time every day. The Time Trigger accepts a sensor whose device_class is timestamp.

Let me know if you need help implementing it.

Example

template:
  - trigger:
      - platform: time
        at: '00:00:00'
      - platform: event
        event_type: event_template_reloaded
    sensor:
      - name: Random Start Time
        unique_id: 'abc123xyz'
        state: "{{ (today_at('08:00:00') + timedelta(minutes = range(0,541) | random)).isoformat() }}"
        device_class: timestamp

There are 540 minutes between 08:00 and 17:00 and the range() function’s upper limit needs to be 1 higher than the desired value so that’s why it’s 541.

1 Like

I think this is exactly what I’m looking for! But I’m not that savvy with YAML and helpers so I’m not sure how to go about creating such a Trigger-based template sensor. Should I create a new sensor with the template helper (binary or no)? If so, what do I need to fill out in the State template / unit of measurement / state class / device etc? Device class would be timestamp I assume? Or, alternatively, how do I get to the place where I can fill out the code example you’ve shared?

Currently, you cannot create a Trigger-based Template helper.

Here’s the documentation for the Template integration. It includes a video tutorial demonstrating how to create a Trigger-based Template Sensor (a fairly advanced one for retrieving weather information).

The example I posted above is entered into your configuration.yaml file. You will need to use a text editor to modify the file. Have you ever edited this file in the past? Just trying to getting a sense of what degree of assistance you require.

1 Like

I think I have –but only as part of tutorials. My partner has as well for the SSL certificate, I think. We’ve got the studio integration installed. But no good knowledge about how to use it. But I usually stick to the visual editor in automations, just using the options available there.

The video is helpful, I think I scrolled past it the first time. So now I’ve got the template / entity and the automation I want active! Having our Sonos playing a Spotify playlist was more of a headache haha. Thanks!

1 Like