If multiple conditions met... action is triggered... how?

I found a solution to do what I want, but it is quite convoluted so I’m wondering if there isn’t a better option:

Example: I want an action to be triggered if 5 conditions are met (with no delay).

  • my phone is charging
  • my phone is locked
  • no motion is detected in the living room
  • no motion is detected in the hallway
  • I’m home

… this should trigger “all lights off”

As of now, the only way I can think of, is to list each affected entity as a trigger and then again list each entity as a condition. Having to list them both as triggers and conditions seams to unnecessary create bloated code.
My question: Is there no way where I don’t have to list all the entities as triggers as well as conditions? Basically just having a trigger that is like “trigger if all conditions bellow are met”?

Yes, you can create a single trigger from a combination of entities using templates. Keep in mind that some entities may have multiple state values that could satisfy what we might think of as a single criteria. For example, both the values “charging” and “full” can indicate that the phone is plugged into a charger when using the battery state sensor. And there may be other sensors that can address the same condition, just from a different angle. For example, you could use the value of the “charger type” sensor. Without knowing which sensors you have enabled the best I can do is the following sketch:

trigger:
  - platform: template
    value_template: >
      {{ is_state('person.c', 'home') and 
      is_state('sensor.your_phone_battery_state', ['charging', 'full']) and
      is_state('sensor.your_phone_lock_state, 'locked') and
      is_state('binary_sensor.hall_motion', 'off') and
      is_state('binary_sensor.living_room_motion', 'off') }}
condition: []
action:
  - service: light.turn_off
    target: 
      entity_id: all

Oh neat. I did not realise that you could do this. :+1:

Yeah, I just learned about it last week… thanks goes to @CentralCommand for adding that functionality!

1 Like