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

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).

1 Like

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?

Thanks for the quick response Taras.
I was starting the automation manually to run as a test (hence the 1,5 minute random numbers). Apparently, turning it on that way did not have a trigger id which I believe caused the failure.
This morning it started at the appropriate time and seems to be working on it’s own.

So bottom line, it can’t be triggered manually. If I stop it to make changes, it probably won’t work until tomorrow (or at turn_off at the off time). So it would be nice to have a trigger_id of ‘default’ in case the system gets restarted during the allowed times. I’ll have to come up with something for that but I don’t know what yet.

Cheers

I have similar request. My light was connected to a smart plug. I set 2 automatic task. one is turn on light when sun senor dusk. the next I tried to do is turn off the light near 11pm. I tried using GUI to create the task. I really like to turn off the light ramdonly between 10:50-11:30pm. After reading this topic, I tried following. but failed. Any experienced guru can help to correct my script?

the orginial set time one is here.
alias: turn off light before mid night
description: “”
trigger:

  • platform: time
    at: “22:55:00”
    condition:
    action:
  • service: script.turn_off_p110_plug
    data: {}
    mode: single

This will turn off light exactly at 10:55pm.

I tried to modify as below
alias: turn off light before mid night
description: “”
trigger:

  • platform: state
    entity_id: switch.smart_plug_p110
    to:
    for:
    minutes: “{{ range(10,30)|random }}” # 10 to 30 minutes feel free to change
    condition:
  • condition: time
    after: “22:55”
    before: “23:30”
    action:
  • service: script.turn_off_p110_plug
    data: {}
    entity_id: switch.smart_plug_p110
    mode: single

HA script can be saved. Test run the script turns off the light. However, the actual run doesn’t do anything. Any problem of the script?

https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371#oneone-format-it-properly-16

EDIT: oops. Sorry did not realise that was a two month old post. I was here looking at something else when I saw it.

1 Like