Just a simple random on and off during certain hours for one light?

I have a simple Zigbee smart switch set up to turn one light on at 17.30 and off at 05.30. I’d like to have it randomly go on and off during those hours. Everything I’ve seen is pretty involved with multiple lights and lots of Yaml coding. Isn’t there something real simple that would do the job?

Many thanks.

Something like this:

trigger:
  - platform: state
    entity_id: light.your_light
    to:
    for:
      minutes: "{{ range(30,90)|random }}"  # 30 to 90 minutes feel free to change
condition:
  - condition: time
    after: "17:30"
    before: "05:30"
action:
  - service: light.toggle
    entity_id: light.your_light

You will still need your light on and off automations. Alternatively they could be incorporated into this automation with a choose action and trigger ids. But that would make it more complicated.

3 Likes

Building on tom_l’s example:

alias: example
trigger:
  - id: 'turn_on'
    platform: time
    at: '17:30:00'
  - id: 'turn_off'
    platform: time
    at: '05:30:00'
  - id: 'toggle'
    platform: state
    entity_id: light.your_light
    to:
    for:
      minutes: "{{ range(30,90)|random }}"  # 30 to 90 minutes feel free to change
condition:
  - condition: time
    after: "17:30"
    before: "05:31"
action:
  - service: 'light.{{ trigger.id }}'
    target:
      entity_id: light.your_light

EDIT

Correction. Changed Time Condition limit to 05:31 to accomodate Time Trigger at 05:30.

1 Like

OK thanks. I’ll have to dig into this some more because even these simple schemes lose me.

I’ve only used the GUI to create a simple on/off automation.

A little dated, but worth taking a look at if you need a primer on translating automations from yaml to the UI editor:

Recreate a YAML automation using the Home Assistant Automation Editor

1 Like

Two things to keep in mind:

  1. You want to simply turn a light on/off but in a complex fashion that’s not merely based on a fixed schedule but a randomly varying one.

  2. The Automation Editor has limitations (and bugs). In Visual mode it supports some but not all of Home Assistant’s scripting language. All automations are stored as YAML and so the YAML version will always have the ability to employ every possible scripting feature.

Tip
When composing an automation in Visual mode, occasionally toggle between Visual and YAML modes to learn how the Automation Editor converts what you compose using the UI into YAML.

1 Like

Great tips. I’ll do some experimenting. Thanks all!

1 Like

I’m trying out this automation but get an “unknown service” message. It says, " automation 4 uses an unknown service

The automation “automation 4” (automation.dining_room_smart_plug_test) has an action that calls an unknown service: light.toggle_light.

This error prevents the automation from running correctly. Maybe this service is no longer available, or perhaps a typo caused it.

To fix this error, [edit the automation] and remove the action that calls this service.

Click on SUBMIT below to confirm you have fixed this automation."

Should I have added light.toggle_light as a service somewhere?

Thanks for your help. It’s starting to make a little more sense.

The service should just be light.toggle

1 Like

No, this will make possible to have the light on during all the day.

Did you miss this bit?

1 Like

No, if the random part turns the lights on at 05:29, it will stay on until the next toggle, which won’t happens before 17:30.

Ah! You’re right; the Time Trigger at 05:30:00 won’t pass that Time Condition. I’ll tack on an extra minute in the condition.

Or did I miss something else as well?

1 Like

If we’re talking about my original automation I did say they still need their original on and off automations.

Sorry Tom, I think I lost the flow of the discussion and didn’t realize you were replying to EdwardTFN (although, it was productive because my ‘all-in-one’ automation’s Time Condition did block the 05:30 Time Trigger and needed correction).

2 Likes

Thanks for all the help here. I’m still trying to get the toggle to work. I adjusted the time between 07:00 and 12:30 so I can test it during daylight.

I’m playing around with Tom’s automation and have both on and off switches active.

The toggle, however, doesn’t work as I get a warning:
Logger: homeassistant.helpers.service
Source: helpers/service.py:635
First occurred: 07:01:09 (1 occurrences)
Last logged: 07:01:09

Unable to find referenced entities switch.dining_room_smart_plug_switch or it is/they are currently not available

That entity is listed so I’m confused why it isn’t seen.

I found an interesting solution by “Speak to the Geek” while researching for this topic.
He provides a blueprint to single-light automations.
His approach makes sense because we cannot have the same random pattern for the bedroom, the living room and the bathroom. Each one has their timings and durations.

2 Likes

Very cool! I’ll set that up.

Many thanks!

@123 (Taras)
I tried this using a switch like this

- id: seedling_fan_operation
  alias: seedling_fan
  trigger:
    - id: 'turn_on'
      platform: time
      at: '07:31:00'
    - id: 'turn_off'
      platform: time
      at: '20:29:00'
    - id: 'toggle'
      platform: state
      entity_id: switch.fan_relay
      to:
      for:
        minutes: "{{ range(1,5)|random }}"  # 30 to 90 minutes feel free to change
  condition:
    - condition: time
      after: "07:30"
      before: "20:30"
  action:
    - service: notify.alexa_media
      data_template:
        target:
        - media_player.my_echo_show
        data:
          type: tts
        message: Toggling seedling fan 
    - service: 'switch.{{  trigger.id  }}'
      target:
        entity_id: switch.fan_relay

But I get an error shown in the debug automation information:

Stopped because an error was encountered at March 19, 2023 at 7:24:31 PM (runtime: 0.11 seconds)
Template rendered invalid service: switch.

It will announce on the echo_show but doesn’t seem to resolve “trigger.id” properly or is this something only for lights?

I’m running Home Assistant 2022.8.7
Supervisor 2022.11.2
Frontend 20220802.0 - latest

Thanks,
Bart

The error message occurred at 7:24:31 PM.

What triggered the automation at that time?