Lighting automations with other conditions

OK, this is what I’m trying to do with limited success.
I’m trying to make a automation that will turn my garage light on when the garage door, back door or motion is detected and only turn it off if both doors are closed and no motion is detected. What’s happening is I keep getting overlapping results.

I’m doing this with the visual editor as I don’t know how to edit code, but I was only able to copy the raw code to show what I did as shown below.

Any help is appreciated

alias: Garage light door automation
description: Turn on light when door is opened and off when closed
trigger:
  - platform: device
    device_id: 5423237e0abc2ec18b5ef90e4a3a0517
    domain: cover
    entity_id: 73faf1e4348595354a9882985ef5ecd4
    type: opening
    id: garage door opening
  - platform: device
    device_id: 5423237e0abc2ec18b5ef90e4a3a0517
    domain: cover
    entity_id: 73faf1e4348595354a9882985ef5ecd4
    type: closed
    for:
      hours: 0
      minutes: 1
      seconds: 30
    id: garage door closed
  - type: opened
    platform: device
    device_id: 7599f5938e99866ea477e34b8dbb8f85
    entity_id: 99b6c1906bd970264a824a948dae15c2
    domain: binary_sensor
    id: back door open
  - type: not_opened
    platform: device
    device_id: 7599f5938e99866ea477e34b8dbb8f85
    entity_id: 99b6c1906bd970264a824a948dae15c2
    domain: binary_sensor
    id: back door closed
    for:
      hours: 0
      minutes: 0
      seconds: 0
  - type: motion
    platform: device
    device_id: 0ce91f21afdb5699108aa390731a88f3
    entity_id: 26c20461235b00540aa9c40a21ad153b
    domain: binary_sensor
    id: Occupancy
  - type: no_motion
    platform: device
    device_id: 0ce91f21afdb5699108aa390731a88f3
    entity_id: 26c20461235b00540aa9c40a21ad153b
    domain: binary_sensor
    id: "no Occupancy "
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - garage door opening
              - back door open
              - Occupancy
        sequence:
          - service: light.turn_on
            data: {}
            target:
              device_id: e239dea5401308bb3b7552047ae699aa
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id:
                  - garage door closed
                  - back door closed
                  - "no Occupancy "
        sequence:
          - service: light.turn_off
            target:
              device_id: e239dea5401308bb3b7552047ae699aa
            data: {}
mode: single

Your second option does not work.
You need to add them as devices/entities just as your triggers.

Trigger events are not the same as entities

I’ll give that a try. Thank you

I have a similar objective to that stated by CintonJD from June 8. I’ve tried a lot of different things with no success thus far, and have been using the UI versus YAML coding.

If I have a lamp turn on from a door being opened OR motion being sensed, how do I turn off the lamp if either 5 minutes has elapsed, OR if no motion has been sensed for 5 minutes?

I realize this sounds really simple, but I can’t get it work correctly. If the trigger is the door, then I’d like the lamp to turn off after 5 minutes.

If the trigger is the motion sensor, then I’d like the lamp to stay on until the motion sensor is clear for 5 minutes.

Can this be accomplished in one automation via the UI?

This is likely pretty straight-forward, but I’ve been getting wrapped around the axle trying to make it work…

I started with HA about 2 1/2 weeks ago and am not a programmer.

When door opens set a date time helper now() + 5 min.
When motion, turn on light.
When motion clear, set date time helper now() + 5 min.
Trigger on time helper, turn off.

I think that would work.

It worked! Not only do the triggers work correctly, but the timer resets when the motion detector is active again prior to the timer being finished - it seems to reset the timer and start it again! That’s exactly what I was looking for! One reason it was driving me nuts is that I realized that one of the motion sensors in question is defective and doesn’t react to motion anymore.

I’m going to use this technique for the identical automation in my garage.

Thanks very much!

Great!
It would be nice if you post the automation so that others who search for this can find an example

OK, here is what I ended up doing. I managed to boil it down to a single automation.

There are 5 possible triggers that turn the garage lights on, and 1 trigger that will turn the light off:

The 5 triggers are all defined with the same trigger id. This way, only one corresponding Option (via a building block “Choose”) can be used to start a helper timer regardless of which device was the trigger.

The first option actions are to start the helper timer and turn on the lights.

I’ve found that defining the helper timer without a duration is the way to go. This gives you the flexibility to assign the duration of the timer within the action.

The second option simply turns off the garage lights upon expiration of the timer.

I haven’t tested every trigger yet but I’m confident this will work based on other automations I’ve created for similar scenarios (ie multiple triggers and using a timer helper).

It’s always better to post automations in text.
Images are hard to read and not something we can copy.

Copy your automation and paste it in a code block </> here on the forum to make sure the formatting is preserved.

OK. I copied the YAML and here it is:

alias: Garage Lights Toggle
description: ""
trigger:
  - type: opened
    platform: device
    device_id: b58b29b43603e3922c034098705af00e
    entity_id: 3c2b607b56397042e22791387cd515e8
    domain: binary_sensor
    id: Garage Light On Trigger
  - type: opened
    platform: device
    device_id: 85ef05b48fccf778a131375c124c85a9
    entity_id: 8ff297aeae183d3d8bd8672fe1cd4c78
    domain: binary_sensor
    id: Garage Light On Trigger
  - type: opened
    platform: device
    device_id: 67fcee5801c33606d99e65d64786ed70
    entity_id: 67aede9f0c7cf31c10d08ca76a6fa1a0
    domain: binary_sensor
    id: Garage Light On Trigger
  - type: opened
    platform: device
    device_id: d1d9478ecd394b70fae704b3d3dde7f1
    entity_id: 2b44189653ad84264e9345a0e7b6d214
    domain: binary_sensor
    id: Garage Light On Trigger
  - type: motion
    platform: device
    device_id: 87004a961d954b46fdc63ded4f4fd753
    entity_id: 3da402ec62708b9c0b24ae0108d52ac8
    domain: binary_sensor
    id: Garage Light On Trigger
  - platform: state
    entity_id:
      - timer.garage_lights_timer
    to: idle
    id: Garage Timer Finished
    for:
      hours: 0
      minutes: 10
      seconds: 0
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Garage Light On Trigger
        sequence:
          - action: timer.start
            metadata: {}
            data:
              duration: "00:10:00"
            target:
              entity_id: timer.garage_lights_timer
          - type: turn_on
            device_id: cf061f62307be6b779fb84e07d28f95b
            entity_id: fd052bdcd2f986da1bef46daab066839
            domain: light
      - conditions:
          - condition: trigger
            id:
              - Garage Timer Finished
        sequence:
          - type: turn_off
            device_id: cf061f62307be6b779fb84e07d28f95b
            entity_id: fd052bdcd2f986da1bef46daab066839
            domain: light
mode: single

Hope this helps other users… Thanks again for your advice and guidance!

Looks good!
Only thing I would change is the device triggers and device actions.
I would make them entity triggers and service calls (actions).
When a device is broken then it’s hard to replace it in automations, but entities are easy.

Awesome! Hadn’t thought about that, I’ll update my automations as you suggest. Thank you!

Another thing I just realized is that you use a state trigger on the timer.
The timer entities are supposed to be used with events Timer - Home Assistant .
In your case I don’t think it will matter since you do not use the timer manually at all.
But I recall there are some issues that comes with using the state instead of events.
Just not sure how that was.
But feel free to leave it as it is if it works.
But keep it in mind for next time.

That raises another question: in the UI, for an “event” trigger, the only option I see is a “Manual” event. I can specify the timer in YAML as required, but then how do I set the duration?

Here is what the UI creates thus far when I use the Manual Event trigger:

platform: event
event_type: timer.finished
event_data:
  timer: timer.garage_lights_timer

What is the syntax to specify the duration in this case?

The timer duration in the example is set in configuration.yaml:

timer:
  test:
    duration: "00:00:30"

So not very UI friendly :wink:

But you can use the UI by means of a helper
The preferred way to configure timer helpers is via the user interface at Settings > Devices & services > Helpers and click the add button; next choose the Timer option.

I meant event as trigger not as an action.
So keep the action as it is but it’s the trigger part I’m talking about.

OK, here is what I came up with to use an Event as the trigger for the timer being finished versus State:

platform: event
event_type: timer.finished
event_data:
  timer:
    garage_lights_timer:
      duration: "00:10:00"
id: Garage Timer Finished
enabled: true

It doesn’t work, the lights have been on for a couple of hours now. The lights on was triggered by opening one of the trigger devices, so I know that still works.

Could it be because I didn’t set the timer duration in the helper? I left it at zero so I could define the duration using the State trigger.

You don’t need any duration.
When the timer is finished then it is finished.

  trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.garage_lights_timer
    id: Garage Timer Finished

OK, this seems to work. I haven’t set durations on the timer helpers so that I can adjust the timer from the automation. When I use the timer finished state as a trigger, this is how it looks in YAML:

platform: state
entity_id:
  - timer.garage_lights_timer
to: idle
id: Garage Timer Finished
for:
  hours: 0
  minutes: 10
  seconds: 0
enabled: true

I’ve realized there are often different ways to skin the cat. I have another automation where one trigger is when motion is sensed. The related actions are to start the timer and turn on the light(s):

action: timer.start
metadata: {}
data:
  duration: "00:05:00"
target:
  entity_id: timer.night_light_timer

The timer duration is set within the action. The second trigger is when the timer is finished, turn off the light(s).

Both approaches seem to behave the same way. Not sure if one method causes more cpu usage or whatever.

Since I don’t plan on ever triggering those helper timers manually, leaving the duration at zero when setting up the timer helper gives you the flexibility of defining the timer duration “on the fly” using either approach.