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
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.
Master door closes. Trigger fires, your condition fails as the guest door trigger didn’t fire that time.
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.
@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.
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.
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?
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.
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:
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.