Automation help with multiple conditions

Hello - still consider myself a newbie with HA as everything I have setup, so far, is all fairly simplistic… This is my first more “robust” automation. I’m not terribly good (yet) with reading the YAML, but if I had a few screen shots of the GUI setup, I could find my way. Anyway, here’s what I could use some advice on:

Trigger: Between 1am and Sunrise
Conditions: If MotionSensor2 detects motion within 90 seconds of MotionSensor1 detecting
Action: turn on a light and wait 30 minutes, then turn it OFF

It’s the conditions part I’m not sure how to do. The “within 90 seconds” and “wait 30 minutes” that are throwing me.

//Brew

Try it this way:

Trigger on motion sensor 1.

Create a condition for between 1 am and sunrise.

In your actions, put a wait for trigger for motion sensor 2 with a timeout of 90 seconds.

If it didn’t time out, turn the light on.

Also start a timer with duration 30 min.

Make a separate automation that triggers when that timer finishes and turn off the light.

Thank you very much! I’ve started to learn more about timers and I think following your example will help!

Cool, hope you come right. I’m not super familiar with the UI, but hopefully the outline helps and are all options via the UI.

Here’s a YAML version though (untested):

automation:
  - alias: "Turn light on"
    trigger:
      - platform: state
        entity_id: binary_sensor.motion_sensor_1
        to: "on"
    condition:
      - condition: time
        after: "01:00:00"
      - condition: sun
        before: sunrise
    action:
      - wait_for_trigger:
          - platform: state
            entity_id: binary_sensor.motion_sensor_2
            to: "on"
        timeout:
          seconds: 90
        continue_on_timeout: false
      - service: light.turn_on
        target:
          entity_id: light.light_1
      - servic: timer.start
        target:
          entity_id: timer.timer_1
        data:
          duration: "00:30:00"

  - alias: "Turn light off"
    trigger:
      - platform: event
        event_type: timer.finished
        event_data:
          entity_id: timer.timer_1
    action:
      - service: light.turn_off
        target:
          entity_id: light.light_1

I’m not sure but I think the conditions need to be ‘and’.

the way you have it now says if it’s after 1am (from 1am till midnight) or before sunrise (from midnight till sunrise). So there is a lot of overlap/extra time using the ‘or’.

I think it should be:

condition:
      - condition: and
        conditions:
          - condition: time
            after: "01:00:00"
          - condition: sun
            before: sunrise

or even shorter since ‘and’ is the default:

condition:
  - condition: time
    after: "01:00:00"
  - condition: sun
    before: sunrise

I think I confused myself about that gotcha that catches most users out when they use both a sunrise and sunset condition crossing the midnight boundary.

My brain is kind of fried after this week’s work, but does the logic practically change things in this case? I’m really just checking myself here?

Anyway, I’ll update my version with your suggestion, especially since it’s more compact. Thanks!

I’m pretty certain it will.

the ‘or’ gives a true condition any time the time is after 1am until the following midnight

or

the time is from midnight today until sunrise

so it basically gives a true condition for any time of the day

But if you use ‘and’ then it only provides a true condition if both of the above are true…

if it’s after 1am today

and

before sunrise today

so it will only be true from 1am till sunrise since it’s the only overlapping time frame.

1 Like

There’s my error. It would’ve been different if the time was before midnight.

The time (1am) technically is before the following midnight.

Every time is always before midnight. it’s all about the frame of reference.

:wink:

1 Like

You may find this helpful:

You guys are awesome! This helped me tremendously!! Thank you.

1 Like