Multiple Triggers - AND

I am looking for the same thing where 2 kitchen lights are turned off, I would like the breakfast room light to turn off as well. That is using Lutron dimmers and the breakfast room is Philips Hue. I was thinking to have separate automation to record timestamp when light is turned off for each kitchen light and another automation on either light state changing to off compare these timestamps and if the difference is no more than 1 second, turn off another light, what I wanted to turn off. Going to try to develop these automations… I have 2 kitchen lights, one is the main light and another is over the sink. Either could be turned off separately and I do not want to turn off the breakfast room in that case. I have a Lutron Peco remote, which turns both kitchen lights at the same time. That is where I want my automation to start to turn off the breakfast room light.

My situation is a little different: I’d lke to fire my ‘action’ after all of my 5 PIR Sensors do not ‘see’ any presence or movement for X minutes. I should achieve a logical AND for 5 triggers. Am I wrong? Is there a way to write such an automation ? Thank you for every kind help.

Create a group with all of your PIR sensors and trigger if the group has been off for X minutes.

2 Likes

Thank you very much! It has been running effortless for 3 months, now!

Hi,

For those who wants 2 triggers and 2 actions

Below code says,

Before sunset

If tv goes on then close the cover,
If tv goes off then open the cover

You can modify for your scenario

alias: TV Mode
description: ''
trigger:
  - platform: state
    entity_id:
      - media_player.sony_bravia_tv
    to: playing
    from: 'off'
  - platform: state
    entity_id:
      - media_player.sony_bravia_tv
    from: playing
    to: 'off'
condition:
  - condition: sun
    before: sunset
    after: sunrise
action:
  - if:
      - condition: state
        entity_id: media_player.sony_bravia_tv
        state: playing
    then:
      - service: cover.close_cover
        data: {}
        target:
          entity_id: cover.veranda_sag
    else:
      - service: cover.open_cover
        data: {}
        target:
          entity_id: cover.veranda_sag
mode: single

3 Likes

I know this thread is old, but I found it when looking to see if Home Assistant was any better than Vera in handling Boolean logic in automations and it doesn’t appear to be (I’m in the process of moving from Vera to Home Assistant). You might look at the “Reactor” add-on to Home Assistant - I have been using it with Vera for a few years now and it is also available for HA and after reading this I think I will put my automations in it instead of native HA. It provides a UI for complex logic - nested AND/OR stuff as deep as you want to go without any code or yaml. Very solid product. It has a few limitations because of the limitations of the HA API, but still is quite powerful.

Have you considered Node-RED ?
It’s a popular alternative to native HA automations that gives a more visual layout.

Your point is well taken.

Ok - this is a case where I shouldn’t post on a subject until I’ve lived with it a while. I stopped using Reactor and figured out the power of trigger templates. Between the Developer Tools and the auto-complete feature it is a very cool toolset - is it writing code? Only a little.

I have used Node Red for a few years now - a great and super stable tool. I run it outside of HA on the OS as a tool to bridge devices on serial ports into HA. Serial Port <=> Node Red <=> MQTT <=> HA. I use the Z-Wave JS server to do the same with Z-Wave because I never got the Z-Wave JS add-on to load.

I have been running HA on VirtualBox because it promised to bridge into serial ports and USB devices but am rethinking that - might move to Hyper-V because I’m not enamored with VirtualBox and have turned off the USB and serial port bridging anyway. Scoop online is that it is not stable unless you limit it to a single CPU (which certainly was the case for me). Which is a pretty big hole for a hypervisor.

Thanks for the feedback.

This is exactly what I’m looking for as well, I’m hoping you found a solution?

I’m not so much surprised this feature is not implemented yet (one has to make choices), but it does surprise me that the use case is not recognized by some.

I’ve got several automations that now have duplicated logic between triggers and conditions. This irritates me and it causes bugs or incomplete scenario’s, due to the added complexity.

For example I have several triggers/conditions for turning on the heating: winter has started, I’m coming home, it’s day time, to name the most obvious. But I can’t put this in just the triggers: they have to be all true! This is what defines the AND-trigger.

Another example: when to trigger to turn on the safety light when I’m not at home? Well, when I’m not at home AND it’s night time. It’s so obvious.

I know this can be done with templates / node red / etc. But I think this is basic functionality that would fit HA automations very well. I also think it would be easy to implement. Anyone?

I have 2 questions?

  • “Conditions” solves this problem I think. Why is it not enough?
  • Also template triggers should be enough I think?

What I did was add all my door locks into one automation since the trigger id’s allow selection of actions in the action block but now I would like to add an additional condition “door open” to each trigger so that if the door was unlocked 1 hour and 59 minutes ago I am not locked out if I go out the door for 1 minute.
As I cannot find any way to add an and function to each trigger I must create 4 automations with conditions or move this all to Node-Red. I imagine I could define new entities that would combine the 2 states into 1 trigger variable but this spreads what should be a simple task to multiple code segments.


alias: Security - Lock doors
description: ""
trigger:
  - platform: state
    entity_id:
      - lock.laundry_door_lock
    to: unlocked
    for:
      hours: 2
      minutes: 0
      seconds: 0
    id: Laundry door unlocked for 2 hours
  - platform: state
    entity_id:
      - lock.u_bolt_pro_z_wave_smart_lock
    to: unlocked
    for:
      hours: 2
      minutes: 0
      seconds: 0
    id: Garage door unlocked for 2 hours
  - platform: state
    entity_id:
      - lock.front_door_lock
    to: unlocked
    for:
      hours: 2
      minutes: 0
      seconds: 0
    id: Front door unlocked for 12 hours
  - platform: time_pattern
    hours: "20"
    minutes: "00"
    seconds: "00"
    id: 8:00pm
condition: []
action:
  - alias: Laundry door
    if:
      - condition: trigger
        id:
          - 8:00pm
          - Laundry door unlocked for 2 hours
    then:
      - service: lock.lock
        target:
          entity_id: lock.laundry_door_lock
        data: {}
  - alias: Garage door
    if:
      - alias: Garage door
        condition: trigger
        id:
          - Garage door unlocked for 2 hours
          - 8:00pm
    then:
      - service: lock.lock
        target:
          entity_id:
            - lock.u_bolt_pro_z_wave_smart_lock
        data: {}
  - alias: Front door
    if:
      - condition: trigger
        id:
          - 8:00pm
          - Front door unlocked for 12 hours
    then:
      - service: lock.lock
        target:
          entity_id:
            - lock.front_door_lock
        data: {}
mode: restart

I recently learned that if I want this to survive a restart I must also give up using “for x time” and instead trigger a seperate timer further complicating the use of automation.