Using for: in a trigger with multiple triggers

Ok, standard disclaimer that I’ve been using HA for a whopping 3 days and can’t find another answer to this.

Background: I’m using an Aqara FP2 sensor with 2 zones in my kitchen. One in front of the sink to (eventually) turn on the over-sink light, the other as the whole kitchen to see when I’m cooking and turn on the undercabinet lighting. Aqara won’t let 2 zones overlap, so I’m checking to see if I’m in either zone.

Now that I have the basics working, I’m trying to make it only trigger after I’m in the kitchen for X minutes, because I don’t need those lights for small tasks like getting a snack. If it were only one zone, I think I could use for: ‘00:03:00’, but that means I’d have to be in only one zone, when in practice I’m walking between zones. Any advice is greatly appreciated.

Note I used the visual configurator for this, I’m also open to best practices and eventually planning to use the same automation to turn them off, but for now I’m keeping it simple.

alias: Kitchen - Undercabinet Lights - On
description: Turns on kitchen lights
trigger:
  - type: occupied
    platform: device
    device_id: b03c5d9053567f78181a1c0eab1d8f96
    entity_id: 9def8b2a5a5b32ba98640f7a1c9cd35e
    domain: binary_sensor
  - type: occupied
    platform: device
    device_id: b03c5d9053567f78181a1c0eab1d8f96
    entity_id: 500eee79741d50b38378bbe1a79c1bee
    domain: binary_sensor
condition:
  - condition: sun
    after: sunset
action:
  - type: turn_on
    device_id: 04a648ba7989c4ad6bdaf3fccaec5637
    entity_id: 77e0f6ef6750252d0bf9eade1afc23ff
    domain: switch
  - type: turn_on
    device_id: cc78ffa9ad0304d41f6ccc5f349cf5bb
    entity_id: af4ed37e8e9a26cdb45e80bccf11cbd3
    domain: switch
mode: single

Maybe create a group binary sensor, that will be on when either is on, and you can use the state of the group.

If one turns off before the other turns on then a template binary sensor with delay_off could help.

Welcome to the forum! The first and foremost advice: don’t use device triggers and conditions. They tend to break when you change devices. Home Assistant is at it best when using state triggers/conditions, numeric state triggers/conditions (template conditions for advanced stuff) and service calls for actions.

When you need to use a delay across multiple triggers, my first weapon of choice would be a timer. You can find those in the helper section (Settings, devices and services, helper tab). You can start, restart, stop them at will, see if they are running and of course trigger something when the timer runs out.

The above advice, put things in a group and respond on that is also good advice. The upside to using timers is that they can be set to survive a reboot, which is advantageous for longer periods.

2 Likes

I would use the group as @Tinkerer suggests.

I understood about two thirds of that which means I’m learning! And I know where to look up the bits I don’t know, so progress!

OK, I’ll have to wait to test it, but I think I’ve got it!

alias: Kitchen - Undercabinet Lights - On
description: Turns on kitchen lights
trigger:
  - platform: state
    entity_id:
      - binary_sensor.kitchen_presence
    to: "on"
    for:
      hours: 0
      minutes: 1
      seconds: 0
condition:
  - condition: sun
    after: sunset
action:
  - type: turn_on
    device_id: 04a648ba7989c4ad6bdaf3fccaec5637
    entity_id: 77e0f6ef6750252d0bf9eade1afc23ff
    domain: switch
  - type: turn_on
    device_id: cc78ffa9ad0304d41f6ccc5f349cf5bb
    entity_id: af4ed37e8e9a26cdb45e80bccf11cbd3
    domain: switch
mode: single

Any advice on moving the “turn off” automation to the same one?

You never shared the off automation, but the same logic would apply.

trigger:
  - platform: state
    entity_id:
      - binary_sensor.kitchen_presence
    to: "off"
    for:
      hours: 0
      minutes: 1
      seconds: 0

Yep, I did it the same way, but haven’t figured an elegant way to combine these both into one automation.

Thanks for the pointing me in the right direction!

Side note, Edwin said it’s better to not reference devices, such as the smartplugs, should I be using a scene or something else? I created a switch group, but then couldn’t find it in the visual editor.

Don’t?

There’s no benefit to doing that.

You should use call a service and use the entity. For example:

action:
  - service: switch.turn_on
    entity_id: switch.whale_bay_doors

Thanks!

Side note, I have a slight issue with the binary sensor. When I move from one zone to another there is a super slight delay where it will clear both zones before marking the other as present. That is causing the binary sensor to never be active for the full duration while moving between the two zones. I really wish aqara would just allow zones to overlap.

Then, as I said, a template binary sensor is what you want.

template:
  - binary_sensor:
      - name: "Kitchen occupied"
        device_class: motion
        state: >
          {{ is_state('binary_sensor.zone_one','on') or is_state('binary_sensor.zone_two','on') }}
        delay_off: '00:00:02'

Adjust the delay for whatever you need.

1 Like

Thanks, I had already implemented the template sensor, but didn’t realize that adding a delay will just keep it ‘on’. Thanks!

For some reason I’m not able to get it to activate. Also, I can’t figure out how to get to the yaml from the GUI interface for helpers. I have the state template as:

>
{{ is_state('binary_sensor.presence_sensor_fp2_7780_presence_sensor_4','on') or is_state('binary_sensor.presence_sensor_fp2_7780_presence_sensor_2','on') }}
delay_off: '00:00:05'

and it to show as an occupancy sensor.

You need to remove the delay_off line - that’s not supported in the UI

OK, but thats the whole reason I went down the template road as you suggested instead of just continuing to use the binary sensor group.

I’ve installed studiocode to get to the yaml, but I can’t figure out where helpers are stored to modify it. Could you help point me in the right direction?

Then you have to do that in YAML.

You don’t modify it. You add the lines I gave you to configuration.yaml and then reload templates.

And just to confirm, how does it change if I’m not using motion, and rather presence as in my code above?

Not sure what you mean. You can replace the two binary sensors in my example with any two binary sensors and it’ll work.

You’ve got the device class as motion, thats the part I just want to check. I’m still very new so I’m trying to understand the implications with it.

That’s from here - I picked motion because that fits your description, but use whatever you want (or none).