Why this door contact automation is not running

I have 2 door contact sensor & want to run automation when 2 door contact sensor is closed but somehow this automation didn’t run as intended, is it something wrong with my code? I can confirm that both sensor is running well as there is a triggered sign on UI when both sensor is on closed state. Below is my code

alias: Master door n Guest door CLOSE - OFF ALL light n fan hardware SW
description: ""
trigger:
  - type: not_opened
    platform: device
    device_id: xxx1
    entity_id: binary_sensor.master_door_sensor_opening
    domain: binary_sensor
    id: Master door closed
  - type: not_opened
    platform: device
    device_id: xxx2
    entity_id: binary_sensor.guest_door_sensor_opening
    domain: binary_sensor
    id: Guest door closed
condition: []
action:
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id: Master door closed
              - condition: trigger
                id: Guest door closed
        sequence:
          - service: script.off_all_light_hardware_switch_except_master_50w_sw
            data: {}
    enabled: true
mode: single

Thanks in advance :slight_smile:

Only one trigger at a time will fire. You’re testing for both triggers happening at once, which will never happen. Triggers are an instantaneous event.

  1. Master door closes. Trigger fires, your condition fails as the guest door trigger didn’t fire that time.
  2. Guest door then closes. Trigger fires, your condition fails as the master door trigger didn’t fire that time.

You should use the condition block, and I think your script call is wrong. Here’s how I’d write it without all the device cruft, assuming the door contacts are off when closed:

alias: Master door n Guest door CLOSE - OFF ALL light n fan hardware SW
trigger:
  - platform: state
    entity_id:
      - binary_sensor.master_door_sensor_opening
      - binary_sensor.guest_door_sensor_opening
    to: 'off'
condition:
  - condition: state
    entity_id:
      - binary_sensor.master_door_sensor_opening
      - binary_sensor.guest_door_sensor_opening
    state: 'off'
action:
  - service: script.turn_on
    target:
      entity_id: script.off_all_light_hardware_switch_except_master_50w_sw

Triggers from either door closing, only proceeds if both are closed.

1 Like

@Troon , you save my day, it work !! Actually this problem is been bother me so long, seem like some condition can’t set from the UI side, have to switch to YAML editor to write the command.

That’s just shorthand: you can set it up as two separate state triggers and two separate state conditions, and it’ll work exactly the same.

Glad it worked for you. A lot of people struggle to understand the workings of triggers.

2 Likes

@Troon ,sometime the automation trigger is quite confusing especially including many combination of devices.

And that’s exactly it: a trigger shouldn’t be a combination of anything. In this example, the triggers are:

  • master door closing; or
  • guest door closing

Keep triggers simple: it’s then up to the condition block to decide what happens based on the state of the system as a whole, and thus whether the action should run. It’s possible to build a load of logic into the action block, but it’s usually better to minimise this with logic in the condition block or the use of trigger variables.

As far as devices go, I’d always recommend using simple state or numeric state triggers working off entity states, rather than pulling in all the complexity of device triggers.

yes, I like simplify automation also so that less error encounter.

1 Like

convenient for u to show another example " two separate state triggers and two separate state conditions" which based on my door sensor condition using UI?

I don’t have door sensors, so here’s what I mean using motion sensors on my system (Clear == off == Closed).

All are state triggers / conditions. First one looks like this expanded:

Triggers are OR by default; conditions are AND — either door will trigger it, but both have to be shut to pass the condition block.

1 Like

did you mean if I put multiple device state in condition, all device state in condition must true in order for the action to run? if one of the device in condition is not true, then the action won’t run.

That is correct by default. There is an Or condition for when you need it. An example from my system that handles turning my hall lamp on:

  trigger:
    - platform: state
      entity_id: sun.sun
      from: 'above_horizon'
      to: 'below_horizon'
    - platform: homeassistant
      event: start
    - platform: state
      entity_id: binary_sensor.occupied
    - platform: state
      entity_id: binary_sensor.wfh_solo
    - platform: state
      entity_id: binary_sensor.hall_light
      to: 'on'
      for:
        minutes: 2

   condition:
    - condition: state
      entity_id: light.hall_light_plug
      state: 'off'
    - or:
        - and:
            - condition: state
              entity_id: binary_sensor.occupied
              state: 'on'
            - condition: state
              entity_id: binary_sensor.wfh_solo
              state: 'off'
            - condition: state
              entity_id: binary_sensor.hall_light
              state: 'on'
        - and:
            - condition: state
              entity_id: binary_sensor.occupied
              state: 'off'
            - condition: state
              entity_id: sun.sun
              state: 'below_horizon'

  action:
    - service: light.turn_on
      entity_id: light.hall_light_plug

This triggers:

  • when the sun goes down
  • when HA restarts
  • when the house occupancy state changes
  • when a template binary sensor that detects if I’m working from home on my own changes
  • when a template binary sensor that detects if light is needed in the hall (based off external light levels and time of day) is on for two minutes

Conditions are:

  • the light must be off; and
  • either:
    • the house is occupied but not just me WFH and the light is required; or
    • the house is unoccupied and the sun has gone down.

Action simply turns the light on.

1 Like

can I use device status as condition or as trigger as well, or is it must use device state more stable? as both condition seem to be true also.

image

Those should work identically and you only need one of them. I prefer the state version to the device version as the generated YAML is smaller and tidier. Compare:

trigger:
  - type: not_opened
    platform: device
    device_id: xxx1
    entity_id: binary_sensor.master_door_sensor_opening
    domain: binary_sensor

with

trigger:
  - platform: state
    entity_id: binary_sensor.master_door_sensor_opening
    to: 'off'

I just notice that state condition is much more tidier.

@Troon seem we are talking about the door sensor automation, I have another automation for you to look into as this also have trouble if you free.

alias: >-
  Main door UNLOCK OR Grill door OPEN - ON main door 20W 50% 5700K AUTO OFF
  2mins
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.grill_door_sensor_opening
    to: "on"
    id: grill door open
  - platform: state
    entity_id:
      - binary_sensor.main_door_sensor_contact
    to: "on"
    id: main door unlock
  - platform: state
    entity_id:
      - light.main_door_20w_light
    to: "on"
    id: main door 20W ON
    for:
      hours: 0
      minutes: 0
      seconds: 2
  - platform: state
    entity_id:
      - binary_sensor.grill_door_sensor_light
    to: "on"
    id: grill door light detected
  - platform: state
    entity_id:
      - binary_sensor.grill_door_sensor_opening
    to: "off"
    id: grill door closed
  - platform: state
    entity_id:
      - binary_sensor.main_door_sensor_contact
    to: "off"
    id: main door locked
condition: []
action:
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: trigger
                id: grill door open
              - condition: trigger
                id: main door unlock
        sequence:
          - type: turn_on
            device_id: xx1
            entity_id: switch.linp_q4s1_ac2b_switch
            domain: switch
      - conditions:
          - condition: trigger
            id: main door 20W ON
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.main_door_20w_50_5700k
            metadata: {}
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id: grill door closed
              - condition: trigger
                id: main door locked
        sequence:
          - delay:
              hours: 0
              minutes: 0
              seconds: 15
              milliseconds: 0
          - type: turn_off
            device_id: xx1
            entity_id: switch.linp_q4s1_ac2b_switch
            domain: switch
      - conditions:
          - condition: trigger
            id: grill door light detected
        sequence:
          - delay:
              hours: 0
              minutes: 2
              seconds: 0
              milliseconds: 0
          - type: turn_off
            device_id: xx1
            entity_id: switch.linp_q4s1_ac2b_switch
            domain: switch
    default:
      - delay:
          hours: 0
          minutes: 0
          seconds: 30
          milliseconds: 0
      - type: turn_off
        device_id: xx1
        entity_id: switch.linp_q4s1_ac2b_switch
        domain: switch
mode: parallel
max: 10

This automation suppose to

  1. turn on the main door light when either one of the main door sensor or grill door sensor “open”. Above automation seem do the job.

  2. when the main door light is ON, I need to run a script to change the main door light color. which the automation seem to be running.

  3. when both door is close, I need to off the main door light in 15secs which is not working but it will run the default 30sec

  4. when grill door light sensor detect main door light, it will auto off it in 2mins which not working as well.

Do this as a separate automation. As in the original automation, trigger off either door, and include the time delay in the trigger (so 'off' for 15s). Add conditions for both doors being closed, and action is simply to turn off the light.

Do that as a separate automation too. You have too much logic (in my opinion) in your action and are getting confused.

previously I do have separate automation but I saw one post said that you can do the similar multiple automation in one automation which save up your automation line.