Random Offset For the Automation Time Trigger

I propose an addition to the automation time trigger so that the actual trigger varies around the set time.

For example, house lights are set to go off at 10:45. I’d like to see the option to make that happen at 10:45 plus or minus 5 minutes, (or any other user inputted value). This would give the house a more lived-in look while on vacation or away.

I agree this would be a nice enhancement. When should the randomized time be calculated?

In your example, it obviously can’t be calculated at 10:45 because the randomly selected offset might be -5 and the resulting time, 10:40, is in the past. Perhaps it should be calculated at the start of every day and whenever Home Assistant is restarted.


Until this FR is implemented (if ever) here’s one way to do it now.

Create a Trigger-based Template Sensor that calculates the randomly offset time at the start of every day and at startup (and whenever Template entities are reloaded).

template:
  - trigger:
      - platform: time
        at: '00:00:00'
      - platform: event
        event_type: event_template_reloaded
      - platform: homeassistant
        event: start
    sensor:
      - name: 'Morning Offset'
        unique_id: 'morning_offset'
        state: "{{ today_at('10:45') + timedelta(minutes = [5, -5] | random) }}"
        device_class: timestamp

Then simply use sensor.morning_offset in a Time Trigger:

alias: example
trigger:
  - platform: time
    at: sensor.morning_offset
condition: []
action:
 ... etc ...

Instead of hardcoding the offset values (5 and -5) they can be input numbers.


EDIT

Correction. Added missing quote.

Yeah don’t know what I was thinking when I said plus or minus :blush:

Your solution looks great! Thanks for taking the time to rough it out. I knew there had to be some sort of workaround but my automation knowledge hasn’t gotten me to this stage.

I had something similar during my x10 days. The x10system was controlled by oneof their dongles on a pi that transmitted to an x10 wall receiver. The pi ran a program that I called various times of the day via cron. At the beginning of each day, I re-wrote the cron tables using values that were offset from a desired time. When I wrote this enhancement request, I forgot completely about the offset generator running every morning to vary the on/off times.

Ahhh memories.

Damned by faint praise for the “roughed out workaround”. :slightly_smiling_face:


Your other option is to compose a cron-scheduled shell script that employs sed to modify automations.yaml and set the Time Trigger’s value to a randomly offset time. Then it should execute ha core restart to load the modified file.

Now that’s a “roughed out workaround”. :wink:

I really said rough, didn’t I? Was not at all what i meant… It’s quite eloquent actually.

Hi @Taras,

I am learning HA and I would find it really helpful if you could adapt your example to include the input number for the offset values as opposed to them being hardcoded.
Thanks

template:
  - trigger:
      - platform: time
        at: '00:00:00'
      - platform: event
        event_type: event_template_reloaded
      - platform: homeassistant
        event: start
      - platform: state
        entity_id: input_number.whatever
    sensor:
      - name: 'Morning Offset'
        unique_id: 'morning_offset'
        state: >
          {% set x = states('input_number.whatever') | int(0) %}
          {{ today_at('10:45') + timedelta(minutes = [x, -x] | random) }}"
        device_class: timestamp

NOTE

FWIW, my post above was created in 2022. For future reference:

  • The forum’s software shows the month and date if the post was made in the current year (Nov 17).
  • If the post was made in a previous year, it shows the month followed by an apostrophe and the last two digits of the year (Nov '17).

Thank you very much.

When I clicked post, I wondered why it said 1 MONTH LATER. D’oh. Not too confusing then :slight_smile: .

Could I trouble you for an example of a timer trigger where the variable x is set, please?

Thanks again.

I don’t understand what you’re requesting.

In the part where the template is consumed:

alias: example
trigger:
  - platform: time
    at: sensor.morning_offset
condition: []
action:
 ... etc ...

How do I set the variable defined as x or maybe it’s input_number.whatever. I guess the template defines a state called input_number.whatever and I have to set it somehow, somewhere.

Sorry if it’s a Noob question, but I’m new to all this.

Thanks again.

You have added a Time Trigger whose time is determined by the value of a sensor. That means the sensor’s device_class must be timestamp and its value is either a time or datetime string.

Do you want that time value to be incorporated into the template’s calculation? If so, how?

I misunderstood what your code does. This was the bit of information I was missing: Input Number - Home Assistant

It took me a while to twig input numbers are a “thing”.

This is what I have done:

templates.yaml

- trigger:
    - platform: time
      at: '00:00:00'
    - platform: event
      event_type: event_template_reloaded
    - platform: homeassistant
      event: start
  sensor:
    - name: 'Random time'
      unique_id: 'random_time'
      state: >
        {% set x = states('input_number.random_time_offset') | int(0) %}
        {{ today_at('00:14') + timedelta(minutes = [x, -x] | random) }}
      device_class: timestamp

configuration.yaml

# standard bits...

automation: !include automations.yaml 
template: !include templates.yaml 

input_number:
  random_time_offset:
    name: random_time_offset
    initial: 10
    min: 0
    max: 59
    step: 1
    unit_of_measurement: minutes

The documentation says to do this by UI, under Settings > Devices & Services > Helpers, but I couldn’t see the initial attribute in the UI which I believed was the way to set the value, but this is not the case as @123 says:

This is what I have done:

automations.yaml

alias: lights-outdoor-off-22:40r
  description: Turn off outside lights 
  trigger:
  - platform: time
    at: sensor.random_time
  condition: []
  action:
   ... etc ...

This has been a great learning exercise, but it seems a little overkill for what I was trying to achieve. All I want is to be able to set a variable with the value of x

…further reading…

…perhaps the term “constant” is more appropriate here. I don’t wish to get into any arguments about programming terminology, but in another automation tool (Azure DevOps) I can do this:
somefile.yaml

variables:
 - name: projectName
   value: contoso

steps: 
- bash: echo $(projectName)
- powershell: echo $(projectName)
- script: echo $(projectName)

Source: Define variables - Azure Pipelines | Microsoft Learn

In this case, I get that the “variable” is not changing, so “constant” is possibly more appropriate, but, it is possible to programmatically change the value of projectName so I guess this is why Microsoft call them variables, Although, in Hashicorp’s Terraform, they use the term “variable” and their values don’t change programmatically. Their values are set at runtime.

As discussed here, it would be nice to set a variable/constant and refer back to it, so if it changes, it only has to be changed once. So for example:
Theoretical example - doesn’t work

variables:
- name: time_offset
  value: 10

sensor:
  - name: 'Random time'
    unique_id: 'random_time'
    state: >
      {{ today_at('00:14') + timedelta(minutes = [var.time_offset, -var.time_offset] | random) }}
    device_class: timestamp

I hope that makes sense and thanks for the help.

Your version of my example omits the State Trigger listening to the input_number. That means when you change the value of the input_number, your version won’t recalculate immediately. It will recalculate only at midnight, or when Reload Template Entities is executed, or when Home Assistant is restarted. Practically speaking, it means the new input_number value will be employed during the recalculation at midnight. If you want it to be more responsive than that, add the State Trigger as shown in my example above.

Very new to HA and struggling as hell with all the different ways to realize a goal.
But: I think there is a “'” lacking before the 10:45 in this line… true?

def should be ‘10:45’

You are correct. The first example I had posted lacked an important quote. Thanks for bringing it to my attention; I have corrected it.

All subsequent examples I had posted appear to be correct and have the required quote.

I don’t see why it can’t be done. If it can be done by default in a rule for a KlikAanKlikUit/Trust ICS-2000 controller then it should also be possible in Home Assistant.

All it takes is for someone with the time, programming expertise, and interest to add the feature to Home Assistant. So far, no one has volunteered to do it.

1 Like