Hoping Home Assistant Devs can add the randomise timer option as standard

Hi All the experts,

I’m new to HA, and loved it so much because it can integrate many different products onto 1 single platform along with Zigbee2mqtt.

I originally used Philips Hue only, and their automation works like a dream especially for when I’m going on vacation, their randomised timer (see screenshot attached) allows me to turn on the device, in my instance a light, at a set time with options to choose how many minutes in total to randomised in between before and after my set time, then turning the light randomly at the end of the automation.

So, in my screenshot, I set the light to turn on the light at 8:30pm but randomised for 45 minutes, so between 22.5 minutes before or 22.5 minutes after this time, then turning off the light at 9:30pm with the same randomised timer.

The randomised timer also allow me to set the maximum of 60 minutes, so 30 minutes before and 30 minutes after the set time, or other length of timer, like total of 10, 15 and 30 minutes.

After enjoying the Hue smart lights in my house, I started buying more smart devices, like Amazon Plug, Ikea Zigbee devices, innr lights, but none of their apps has this randomised timer available.

Now, I have joined the Home Assistant family, and integrated the devices onto Home Assistant, I would like to request this randomised timer option to be added as standard options please.

I have already searched through the forum and online, but people mentioned yaml script did not work, or an add-on from HACS which learns from the previous histories, but my habit was to turning things off from the mains rather than turning off from the app to save the electric, which means HA most likely would not recognise my smart devices usage pattern.

Hope what I said make sense to everyone, and hope this can be added as standard options.

Thanks for your time to read my post, hope you all have a nice day.

Those people did not try hard enough. This can certainly be done in yaml.

1 Like

Jinja can randomize numbers, so certainly do-able.

my mockupancy does that and a bit more:

  - id: home_mode_locked_lights
    mode: restart
    trigger:
      platform: state
      entity_id: binary_sensor.uithuis_verlichting_actief
      to: 'on'
    action:
      - repeat:
          while:
            - condition: state
              entity_id: binary_sensor.uithuis_verlichting_actief
              state: 'on'
          sequence:
            - if:
                !include /config/includes/include_donker_buiten_off.yaml
              then:
                <<: &lampen_uit
                  service: light.turn_off
                  target:
                    entity_id: #all
                      - light.home_mode_locked_lights_1
                      - light.home_mode_locked_lights_2

            - wait_template: >
                {{is_state('sun.sun','below_horizon') or
                  is_state('binary_sensor.donker_buiten','on')}}
            - wait_template: >
                {{today_at('17:00') <= now() or now() < today_at('01:30')}}
            - delay:
                minutes: >
                  {{range(2,27)|random}}
            - service: script.turn_on
              target:
                entity_id: script.home_mode_locked_lights
              data:
                variables:
                  random_group: light.home_mode_locked_lights_1
                  random_entity_id: >
                    {{state_attr('light.home_mode_locked_lights_1','entity_id')
                     |select('has_value')|list|random}}
                  light_count_limit: 6
            - delay:
                seconds: >
                  {{range(9,45)|random}}
            - service: script.turn_on
              target:
                entity_id: script.home_mode_locked_lights
              data:
                variables:
                  random_group: light.home_mode_locked_lights_2
                  random_entity_id: >
                    {{state_attr('light.home_mode_locked_lights_2','entity_id')
                     |select('has_value')|list|random}}
                  light_count_limit: 6
            - if:
                >
                  {{now() > today_at('01:30')}}
              then:
                - <<: *lampen_uit
                - service: light.turn_on
                  target:
                    entity_id: light.slaapkamer_tafel
                - delay:
                    minutes: >
                      {{range(3,27)|random}}
                - service: light.turn_off
                  target:
                    entity_id: light.slaapkamer_tafel

      - <<: *lampen_uit

and the script.home_mode_locked_lights it calls is

  home_mode_locked_lights:
    mode: parallel
    sequence:
      - condition: >
          {%- set count = expand(random_group)|selectattr('state','eq','on')|list|count %}
          {{is_state(random_entity_id,'on') or
            is_state(random_entity_id,'off') and count < light_count_limit|int}}
      - service: light.toggle
        data:
          entity_id: >
            {{random_entity_id}}

only thing I manually did is create those 2 light groups (light.home_mode_locked_lights_x ) in the UI.

So check the use of |random here which is used for randomizing lights and duration.
No need for this FR, as the functionality already is available.

1 Like

Not to detract from your FR, but there is a Presence Simulation integration (via HACS) that does all the work for you, and can vary times by a random amount.

Don’t do this with ZigBee devices or you’ll run into issues with your mesh.
If you really have to switch off at the mains, get bulbs that don’t route the mesh, like Sengled.

1 Like

I do exactly what you want to do, in an automation:

- id: away_lr_lamp_off
  alias: Away - LR Floor Lamp Off 11PM
  initial_state: 'true'
  mode: single
  trigger:
  - platform: time
    at: '23:05:30'
  action:
  - delay:
      minutes: '{{ range(5, 21) | random }}'
  - service: switch.turn_off
    target:
      entity_id: switch.my_switch_entity_id

When this automation is active, (when I’m “away”) the living room lamp goes off between five and 21 minutes after the trigger time of 23:05:30.

Hope this helps.

This is solved and can be closed IMHO.

Thank you all for replying.

From what I understand from those solutions, they look like they can only trigger AFTER the set timer, but not before?

Like if I wanna set to turn on let’s say 9PM with randomised of 60 minutes, so plus or minus 30 minutes before or after 9PM, which could be randomised to turn on at 8:42PM, it is doable on Philips Hue app.

I do aware there are solutions out in the wide to accomplish this kind of situation, but that’s a manual yaml editing, however, people like me who’s just starting out and not an expert with Linux commands, this is something that’s not an easy job to achieve, let alone just to understand what each lines of code does.

So I’m hoping this function can be added as standard in the automation section rather than editing with the yaml editor.

Hope you all understand what I am trying to achieve. I’m sure everyone was a Linux newbie sometime ago that don’t understand every single comands, and I’m this person now :crazy_face:

1 Like

Then you’d set the trigger at 8:30 PM, with a random 0-60 delay:

  - delay:
      minutes: '{{ range(0,60) | random }}'

I rarely use UNIX commands. HAOS requires no UNIX knowledge, and I connect to it from my Windows laptop via the web-based user interface or SAMBA shares. I’ll connect and do some command-line stuff like stop, update and start the HA Core, but that’s a personal preference. I could do the same thing from the web-based UI.

That said, I know what you mean. HA is a product of a developer community and, although great strides have been made “GUI-fying” it for non-programmers, there’s still an underlying assumption of a certain baseline of knowledge. Believe me, a lot of it was new and baffling to me at first, even after a career in IT. Keep at it. You’ll eventually learn the jargon and the secret handshakes and feel more comfortable being part of the “club.”

Oh, and, don’t be afraid to ask. You’ll almost always find someone willing to help you out here.

2 Likes

yaml has nothing to do with linux commands. Where did you get his idea?

Is it text fear? Y’know even windows has scripting. In fact they made major attempts to adopt text scripting some time ago. Look up powershell.

The solution is so simple, as has been illustrated to you, why not try and find a problem that really does need fixing.

@CaptTom
Really appreciated, your words are encouraging!

However, I have been looking through setting up the automation, but not too sure where I could put your suggested codes?

Is it in the “Then do” section > Add Action > choose an action (i.e. Lights > Turn on) > then press the 3 dots > Edit in YAML? (Like in my screenshot attached)

I would really appreciate if you could point me to the right direction if its not the right place.

Not sure how much help I can be. I pretty much gave up on using the UI for creating automations years ago, when it wasn’t nearly as functional. I just edit the YAML in automations.yaml.

That said, you can probably use the UI for most, maybe all of this. Then, as you show, go into the YAML and see what the UI created for you. It’s actually a good way to learn what the various settings turn into once saved as YAML. And it allows you to see where to insert lines from examples like the one I posted, above.

That code I posted is one entire automation. The UI will probably give yours a random ID, but the rest of it should be similar to what I’ve posted. Add the -delay: and minutes: lines under action: The one most important thing about YAML is it’s very, very picky about indentation. It’s not always clear what needs to be indented under what. Worse, things can be done in totally different order, but still be equivalent.

I’d recommend going back and forth between the UI and the YAML. Try something in the UI and see what YAML it generates. Obviously you can also read up on YAML. But to be honest, you won’t “get it” until you’ve seen it in action. I still don’t fully understand the structure but, given good examples, I can usually tweak them to do what I want.