Simple automation question - length of time 'on'

Hello,
I’m just getting to grips with Home Assistant and now trying a few automations.

I have the first part working, to turn on a light if a certain condition is met, but I want the light to turn off - either after so many minutes or at a certain time - but only if the light came on because of the automation. In my mind I see this as setting a flag to say ‘automation triggered light’ and then at the off time checking if the flag is set or not? How should I approach that or is there a simpler method?

- id: 
  alias: morning
  trigger:
  - at: 06:59:08
    platform: time
  condition:
  - condition: state
    entity_id: media_player.living_room
    state: playing
  action:
  - alias: ''
    data:
      entity_id: light.tradfri_bulb_e27_ws_opal_980lm_3
    service: light.turn_on

thanks

I have mine turning off after a 10 minute delay`

 - delay: '00:10:00'
 - service: homeassistant.turn_off
    entity_id:
    - switch.light_outside
    - switch.light_hall2
    - switch.light_br1
    - switch.lamp_lr

`

You can also turn on a switch in this automation and build a second automation at a certain time to turn it off like you suggested.

Sorry about the way my code post, I always have trouble with this. It shows fine in the edit but changes when I hit post.

first @cyn, please post any code per the blue box at the top of the page.

As to the OP,

you could try also turning on an input_boolean when you turn on the the light with the automation. Then you could check the status of the input_boolean after a certain amount of time has passed and if ‘on’ then turn off both the light and the input_boolean.

thanks @cyn from a quick check that does the job.

@finity once I get more involved I’ll give the input_boolean a go

Is it as simple as pressing </> and then ctrl-V
That’s what I try but NEVER get it to show up.

Ironically I literally just posted the following in another thread to someone who was also having a hard time figuring it out:

“The way it works is that you copy your code snippet from your configuration.yaml file, paste it into the reply box, then you need to highlight all of the code you just pasted in and then click on the “</>”. Every once in a while it may do something funky with the first line so you may have to play with that but it’s fairly rare that you have to mess with it.”

If you need more help feel free to ask.

1 Like

I use input booleans for that.

  1. Turn on an input boolean when you turn the light on automatically
  2. Use a condition check that the boolean is on when considering turning the light off automatically
  3. Turn off the boolean any time the light turns off.
1 Like

Super @Tinkerer, very interesting resource.

Thank you so much @finity, this was driving me crazy!

it’s way easier to use the button below escape and next to the number one, ` symbol. Use 3 in a row before your code and 3 in a row after your code. It also does some pretty formatting with colors and crap.

using </>:

this:
  # Some Section for my house
  some_section:
    attribute: "Foo Foo"

vs

using three `:

this:
  # Some Section for my house
  some_section:
    attribute: "Foo Foo"

What the 3 ` looks like:

image

1 Like

Thank you @petro, now I have 2 ways to do this.