Hi!
I have 2 sensors and I want to do 4 actions
Sensor 1 ON sensor 2 ON - action 1
Sensor 1 OFF sensor 2 ON - action 2
Sensor 1 ON sensor 2 OFF - action 3
Sensor 1 OFF sensor 2 OFF - action 4
How can I do this in one automation?
My case is: I have 2 doors and I want to flash one light in different color depending of door states.
For the moment I use: trigger door 1, scene.create Bulb 1, flash bulb in Red 10 times, scene turn on and it is working.
calisro
(Rob)
June 23, 2022, 5:49pm
2
a template trigger:
- condition: template
value_template: '{{ is_state('sensor.one','on') and is_state('sensor.two','on') }}'
id: action1
Then in a condition statement in the actions you can use the trigger id as a choice.
action:
- choose:
- conditions:
- condition: trigger
id: action1
sequence:
- service: notify.mobile_app_cory_tablet
data:
title: We need you. Please come.
message: TTS
- conditions:
- condition: trigger
id: action2
sequence:
- service: notify.mobile_app_emerson_tablet
data:
message: TTS
title: We need you. Please come.
calisro
(Rob)
June 23, 2022, 5:58pm
3
Alternatively, you can just trigger on the state of each of those sensors, and have the choose statement define the rules of what would occur based on that same template(s).
like this:
alias: New Automation
description: ''
mode: single
trigger:
- platform: state
entity_id:
- binary_sensor.back_door
from:
- 'on'
- 'off'
to:
- 'on'
- 'off'
- platform: state
entity_id:
- binary_sensor.front_door
from:
- 'on'
- 'off'
to:
- 'on'
- 'off'
condition: []
action:
- choose:
- conditions:
- condition: template
value_template: >-
{{ is_state('binary_sensor.back_door','on') and
is_state('binary_sensor.front_door','on') }}
sequence:
- device_id: ''
domain: ''
entity_id: ''
- conditions:
- condition: template
value_template: >-
{{ is_state('binary_sensor.back_door','on') and
is_state('binary_sensor.front_door','off') }}
sequence:
- device_id: ''
domain: ''
entity_id: ''
default: []
123
(Taras)
June 23, 2022, 6:18pm
4
calisro beat me to the post but I had already composed something offline so I’ll share it anyway. It’s effectively the same concept as calisro’s (monitor the door states and use a choose
to determine whcih action to perform) but it uses a single State Trigger, variables, and shorthand notation for the Template Conditions.
alias: example
trigger:
- platform: state
entity_id:
- binary_sensor.door1
- binary_sensor.door2
from:
- 'on'
- 'off'
to:
- 'off'
- 'on'
condition: []
action:
- variables:
door1_open: "{{ is_state('binary_sensor.door1', 'on') }}"
door2_open: "{{ is_state('binary_sensor.door2', 'on') }}"
- choose:
- conditions:
- '{{ not door1_open }}'
- '{{ not door2_open }}'
sequence:
... actions when BOTH doors are CLOSED ...
- conditions:
- '{{ not door1_open }}'
- '{{ door2_open }}'
sequence:
... actions when SECOND door is OPEN ...
- conditions:
- '{{ door1_open }}'
- '{{ not door2_open }}'
sequence:
... actions when FIRST door is OPEN ...
- conditions:
- '{{ door1_open }}'
- '{{ door2_open }}'
sequence:
... actions when BOTH doors are OPEN ...
default: []
I chose this format to display each selection in the choose
(two Template Conditions in shorthand notation that are implicitly ANDed):
- conditions:
- '{{ door1_open }}'
- '{{ not door2_open }}'
but, if you prefer conciseness, you can consolidate it into a single line like this:
- conditions: '{{ door1_open and not door2_open }}'
Thank you!
Not working, for the moment.
- id: '1656010412845'
alias: Auto Test Doors
description: ''
trigger:
- platform: state
entity_id:
- binary_sensor.contact_door_front_contact
- binary_sensor.contact_door_kitchen_contact
from: 'off'
to: 'on'
condition: []
action:
- variables:
one_open: "{{ is_state('binary_sensor.contact_door_front_contact', 'on') }}"
two_open: "{{ is_state('binary_sensor.contact_door_kitchen_contact', 'on') }}"
- choose:
- conditions:
- condition: template
value_template: '{{ one_open }}'
- condition: template
value_template: '{{ not two_open }}'
sequence:
- service: light.turn_on
data:
brightness_pct: 100
flash: short
rgb_color:
- 255
- 0
- 0
target:
device_id:
- 56a5ade279b7761955e851ffc39eb038
- conditions:
- condition: template
value_template: '{{ not one_open }}'
- condition: template
value_template: '{{ two_open }}'
sequence:
- service: light.turn_on
data:
rgb_color:
- 0
- 0
- 255
brightness_pct: 100
flash: short
target:
device_id:
- dab8e2497c63bd8c393d94f80ae89456
- conditions:
- condition: template
value_template: '{{ one_open }}'
- condition: template
value_template: '{{ two_open }}'
sequence:
- service: light.turn_on
data:
rgb_color:
- 0
- 255
- 0
brightness_pct: 100
flash: short
target:
device_id:
- dab8e2497c63bd8c393d94f80ae89456
123
(Taras)
June 24, 2022, 1:10pm
6
Because the State Trigger you created isn’t like the one I had suggested. Your version only triggers for state-changes from off
to on
and so it excludes from on
to off
.
Change the State Trigger to this:
- platform: state
entity_id:
- binary_sensor.contact_door_front_contact
- binary_sensor.contact_door_kitchen_contact
from:
- 'on'
- 'off'
to:
- 'off'
- 'on'
Thank you!
It is working.
This is my code:
- id: '1656010412845'
alias: Auto Test Doors
description: ''
trigger:
- platform: state
entity_id:
- binary_sensor.contact_door_front_contact
- binary_sensor.contact_door_kitchen_contact
from:
- 'on'
- 'off'
to:
- 'off'
- 'on'
condition: []
action:
- variables:
one_open: '{{ is_state(''binary_sensor.contact_door_front_contact'', ''on'')
}}'
two_open: '{{ is_state(''binary_sensor.contact_door_kitchen_contact'', ''on'')
}}'
- choose:
- conditions:
- condition: template
value_template: '{{ one_open }}'
- condition: template
value_template: '{{ not two_open }}'
sequence:
- service: light.turn_on
data:
brightness_pct: 100
rgb_color:
- 255
- 0
- 0
target:
device_id:
- 56a5ade279b7761955e851ffc39eb038
- dab8e2497c63bd8c393d94f80ae89456
- 1631ca3d7cc1cda05575520e9089a17a
- 32dc34d856fb18390539e535b30dee96
- 59f0bcb38dfbaafd219a0035262714f9
- conditions:
- condition: template
value_template: '{{ not one_open }}'
- condition: template
value_template: '{{ two_open }}'
sequence:
- service: light.turn_on
data:
rgb_color:
- 0
- 0
- 255
brightness_pct: 100
target:
device_id:
- dab8e2497c63bd8c393d94f80ae89456
- 56a5ade279b7761955e851ffc39eb038
- 1631ca3d7cc1cda05575520e9089a17a
- 32dc34d856fb18390539e535b30dee96
- 59f0bcb38dfbaafd219a0035262714f9
- conditions:
- condition: template
value_template: '{{ one_open }}'
- condition: template
value_template: '{{ two_open }}'
sequence:
- service: light.turn_on
data:
rgb_color:
- 0
- 255
- 0
brightness_pct: 100
target:
device_id:
- dab8e2497c63bd8c393d94f80ae89456
- 56a5ade279b7761955e851ffc39eb038
- 1631ca3d7cc1cda05575520e9089a17a
- 32dc34d856fb18390539e535b30dee96
- 59f0bcb38dfbaafd219a0035262714f9
Where should I add scene create to run this on the fly?
service: scene.create
data:
scene_id: babaciuni
snapshot_entities:
- light.hue_bulb_babaciuni
- light.hue_bulb_entrance_room
- light.hue_bulb_kitchen
- light.hue_bulb_pc
- light.hue_bulb_tv_2
enabled: true
123
(Taras)
June 24, 2022, 6:19pm
8
I don’t know the answer because I don’t understand how you intend to use that scene in the automation. Is it for restoring the lights to their original state? Do the entities you listed correspond to the device ids you used in the automation? If they are, why are you using both entity_ids and device_ids to reference the same entities?
Yes
This is the code and is not restoring:
- id: '1656010412845'
alias: Auto Test Doors
description: ''
trigger:
- platform: state
entity_id:
- binary_sensor.contact_door_front_contact
- binary_sensor.contact_door_kitchen_contact
from:
- 'on'
- 'off'
to:
- 'off'
- 'on'
condition: []
action:
- service: scene.create
data:
scene_id: doors
snapshot_entities:
- light.hue_bulb_babaciuni
- light.hue_bulb_entrance_room
- light.hue_bulb_kitchen
- light.hue_bulb_pc
- light.hue_bulb_tv_2
enabled: true
- variables:
one_open: '{{ is_state(''binary_sensor.contact_door_front_contact'', ''on'')
}}'
two_open: '{{ is_state(''binary_sensor.contact_door_kitchen_contact'', ''on'')
}}'
- choose:
- conditions:
- condition: template
value_template: '{{ one_open }}'
- condition: template
value_template: '{{ not two_open }}'
sequence:
- service: light.turn_on
data:
brightness_pct: 100
rgb_color:
- 255
- 0
- 0
target:
device_id:
- 56a5ade279b7761955e851ffc39eb038
- dab8e2497c63bd8c393d94f80ae89456
- 1631ca3d7cc1cda05575520e9089a17a
- 32dc34d856fb18390539e535b30dee96
- 59f0bcb38dfbaafd219a0035262714f9
- conditions:
- condition: template
value_template: '{{ not one_open }}'
- condition: template
value_template: '{{ two_open }}'
sequence:
- service: light.turn_on
data:
rgb_color:
- 0
- 0
- 255
brightness_pct: 100
target:
device_id:
- dab8e2497c63bd8c393d94f80ae89456
- 56a5ade279b7761955e851ffc39eb038
- 1631ca3d7cc1cda05575520e9089a17a
- 32dc34d856fb18390539e535b30dee96
- 59f0bcb38dfbaafd219a0035262714f9
- conditions:
- condition: template
value_template: '{{ one_open }}'
- condition: template
value_template: '{{ two_open }}'
sequence:
- service: light.turn_on
data:
rgb_color:
- 0
- 255
- 0
brightness_pct: 100
target:
device_id:
- dab8e2497c63bd8c393d94f80ae89456
- 56a5ade279b7761955e851ffc39eb038
- 1631ca3d7cc1cda05575520e9089a17a
- 32dc34d856fb18390539e535b30dee96
- 59f0bcb38dfbaafd219a0035262714f9
- conditions:
- condition: template
value_template: '{{ not one_open }}'
- condition: template
value_template: '{{ not two_open }}'
sequence:
- service: scene.turn_on
target:
entity_id: scene.doors
metadata: {}
enabled: true
123
(Taras)
June 24, 2022, 7:06pm
10
settercoly:
is not restoring
You’re creating a new scene snapshot each time the automation is triggered even when both doors are closed . So when you close both doors, it creates a new scene and then immediately restores it.
You should create the scene only when both doors are not closed.
Still not restore
- id: '1656010412845'
alias: Auto Test Doors
description: ''
trigger:
- platform: state
entity_id:
- binary_sensor.contact_door_front_contact
- binary_sensor.contact_door_kitchen_contact
from:
- 'on'
- 'off'
to:
- 'off'
- 'on'
condition: []
action:
- variables:
one_open: '{{ is_state(''binary_sensor.contact_door_front_contact'', ''on'')
}}'
two_open: '{{ is_state(''binary_sensor.contact_door_kitchen_contact'', ''on'')
}}'
- choose:
- conditions:
- condition: template
value_template: '{{ not one_open }}'
- condition: template
value_template: '{{ not two_open }}'
sequence:
- service: scene.create
data:
scene_id: doors
snapshot_entities:
- light.hue_bulb_babaciuni
- light.hue_bulb_entrance_room
- light.hue_bulb_kitchen
- light.hue_bulb_pc
- light.hue_bulb_tv_2
- conditions:
- condition: template
value_template: '{{ one_open }}'
- condition: template
value_template: '{{ not two_open }}'
sequence:
- service: light.turn_on
data:
brightness_pct: 100
rgb_color:
- 255
- 0
- 0
target:
device_id:
- 56a5ade279b7761955e851ffc39eb038
- dab8e2497c63bd8c393d94f80ae89456
- 1631ca3d7cc1cda05575520e9089a17a
- 32dc34d856fb18390539e535b30dee96
- 59f0bcb38dfbaafd219a0035262714f9
- conditions:
- condition: template
value_template: '{{ not one_open }}'
- condition: template
value_template: '{{ two_open }}'
sequence:
- service: light.turn_on
data:
rgb_color:
- 0
- 0
- 255
brightness_pct: 100
target:
device_id:
- dab8e2497c63bd8c393d94f80ae89456
- 56a5ade279b7761955e851ffc39eb038
- 1631ca3d7cc1cda05575520e9089a17a
- 32dc34d856fb18390539e535b30dee96
- 59f0bcb38dfbaafd219a0035262714f9
- conditions:
- condition: template
value_template: '{{ one_open }}'
- condition: template
value_template: '{{ two_open }}'
sequence:
- service: light.turn_on
data:
rgb_color:
- 0
- 255
- 0
brightness_pct: 100
target:
device_id:
- dab8e2497c63bd8c393d94f80ae89456
- 56a5ade279b7761955e851ffc39eb038
- 1631ca3d7cc1cda05575520e9089a17a
- 32dc34d856fb18390539e535b30dee96
- 59f0bcb38dfbaafd219a0035262714f9
- service: scene.turn_on
target:
entity_id: scene.doors
metadata: {}
123
(Taras)
June 24, 2022, 8:45pm
12
settercoly:
Still not restore
Still not what I suggested.
True and still not working
- id: '1656010412845'
alias: Auto Test Doors
description: ''
trigger:
- platform: state
entity_id:
- binary_sensor.contact_door_front_contact
- binary_sensor.contact_door_kitchen_contact
from:
- 'on'
- 'off'
to:
- 'off'
- 'on'
condition: []
action:
- variables:
one_open: '{{ is_state(''binary_sensor.contact_door_front_contact'', ''on'')
}}'
two_open: '{{ is_state(''binary_sensor.contact_door_kitchen_contact'', ''on'')
}}'
- choose:
- conditions:
- condition: template
value_template: not '{{ one_open and two_open }}'
enabled: true
sequence:
- service: scene.create
data:
scene_id: doors
snapshot_entities:
- light.hue_bulb_babaciuni
- light.hue_bulb_entrance_room
- light.hue_bulb_kitchen
- light.hue_bulb_pc
- light.hue_bulb_tv_2
enabled: true
- conditions:
- condition: template
value_template: '{{ one_open }}'
- condition: template
value_template: '{{ not two_open }}'
sequence:
- service: light.turn_on
data:
brightness_pct: 100
rgb_color:
- 255
- 0
- 0
target:
device_id:
- 56a5ade279b7761955e851ffc39eb038
- dab8e2497c63bd8c393d94f80ae89456
- 1631ca3d7cc1cda05575520e9089a17a
- 32dc34d856fb18390539e535b30dee96
- 59f0bcb38dfbaafd219a0035262714f9
- conditions:
- condition: template
value_template: '{{ not one_open }}'
- condition: template
value_template: '{{ two_open }}'
sequence:
- service: light.turn_on
data:
rgb_color:
- 0
- 0
- 255
brightness_pct: 100
target:
device_id:
- dab8e2497c63bd8c393d94f80ae89456
- 56a5ade279b7761955e851ffc39eb038
- 1631ca3d7cc1cda05575520e9089a17a
- 32dc34d856fb18390539e535b30dee96
- 59f0bcb38dfbaafd219a0035262714f9
- conditions:
- condition: template
value_template: '{{ one_open }}'
- condition: template
value_template: '{{ two_open }}'
sequence:
- service: light.turn_on
data:
rgb_color:
- 255
- 0
- 255
brightness_pct: 100
target:
device_id:
- dab8e2497c63bd8c393d94f80ae89456
- 56a5ade279b7761955e851ffc39eb038
- 1631ca3d7cc1cda05575520e9089a17a
- 32dc34d856fb18390539e535b30dee96
- 59f0bcb38dfbaafd219a0035262714f9
- service: scene.turn_on
target:
entity_id: scene.doors
metadata: {}
123
(Taras)
June 24, 2022, 8:53pm
14
The latest example you posted turns on the scene after each combination of open/closed doors and that’s not what I suggested. In addition, the following is invalid:
value_template: not '{{ one_open and two_open }}'
Ok but no way, for my level.
I need to study more.
Thank you!
nop
- id: '1656010412845'
alias: Auto Test Doors
description: ''
trigger:
- platform: state
entity_id:
- binary_sensor.contact_door_front_contact
- binary_sensor.contact_door_kitchen_contact
from:
- 'on'
- 'off'
to:
- 'off'
- 'on'
condition: []
action:
- variables:
one_open: '{{ is_state(''binary_sensor.contact_door_front_contact'', ''on'')
}}'
two_open: '{{ is_state(''binary_sensor.contact_door_kitchen_contact'', ''on'')
}}'
- choose:
- conditions:
- condition: template
value_template: '{{ one_open }}'
- condition: template
value_template: '{{ not two_open }}'
sequence:
- service: scene.create
data:
scene_id: doors
snapshot_entities:
- light.hue_bulb_babaciuni
- light.hue_bulb_entrance_room
- light.hue_bulb_kitchen
- light.hue_bulb_pc
- light.hue_bulb_tv_2
- service: light.turn_on
data:
brightness_pct: 100
rgb_color:
- 255
- 0
- 0
target:
device_id:
- 56a5ade279b7761955e851ffc39eb038
- dab8e2497c63bd8c393d94f80ae89456
- 1631ca3d7cc1cda05575520e9089a17a
- 32dc34d856fb18390539e535b30dee96
- 59f0bcb38dfbaafd219a0035262714f9
- conditions:
- condition: template
value_template: '{{ not one_open }}'
- condition: template
value_template: '{{ two_open }}'
sequence:
- service: scene.create
data:
scene_id: doors
snapshot_entities:
- light.hue_bulb_babaciuni
- light.hue_bulb_entrance_room
- light.hue_bulb_kitchen
- light.hue_bulb_pc
- light.hue_bulb_tv_2
- service: light.turn_on
data:
rgb_color:
- 0
- 0
- 255
brightness_pct: 100
target:
device_id:
- dab8e2497c63bd8c393d94f80ae89456
- 56a5ade279b7761955e851ffc39eb038
- 1631ca3d7cc1cda05575520e9089a17a
- 32dc34d856fb18390539e535b30dee96
- 59f0bcb38dfbaafd219a0035262714f9
- conditions:
- condition: template
value_template: '{{ one_open }}'
- condition: template
value_template: '{{ two_open }}'
sequence:
- service: light.turn_on
data:
rgb_color:
- 255
- 0
- 255
brightness_pct: 100
target:
device_id:
- dab8e2497c63bd8c393d94f80ae89456
- 56a5ade279b7761955e851ffc39eb038
- 1631ca3d7cc1cda05575520e9089a17a
- 32dc34d856fb18390539e535b30dee96
- 59f0bcb38dfbaafd219a0035262714f9
- conditions:
- condition: template
value_template: '{{ not one_open }}'
- condition: template
value_template: '{{ not two_open }}'
sequence:
- service: scene.turn_on
target:
entity_id: scene.doors
metadata: {}
123
(Taras)
June 25, 2022, 4:33pm
17
Can I ask why you want to use a scene, to store/restore the states of all 5 lights, when flashing them?
In my own tests with Philips Hue bulbs, after performing a short flash the light automatically returns to its original state (if it was originally off
it returns to off
; it it was originally on
it returns to whatever color it had been) without using a snapshot scene.
If you’re interested, you can try the following example. I successfully tested a (slightly different) version of it in my own home and it flashed the lights every time doors were opened/closed (except when both doors were closed).
- id: '1656010412845'
alias: Auto Test Doors
description: ''
trigger:
- platform: state
entity_id:
- binary_sensor.contact_door_front_contact
- binary_sensor.contact_door_kitchen_contact
from:
- 'on'
- 'off'
to:
- 'off'
- 'on'
condition: []
action:
- variables:
lights:
- light.hue_bulb_babaciuni
- light.hue_bulb_entrance_room
- light.hue_bulb_kitchen
- light.hue_bulb_pc
- light.hue_bulb_tv_2
colors:
0: [0,0,0]
1: [0,0,255]
2: [255,0,0]
3: [0,255,0]
front: "{{ is_state('binary_sensor.contact_door_front_contact', 'on') | abs * 2 }}"
kitchen: "{{ is_state('binary_sensor.contact_door_kitchen_contact', 'on') | abs }}"
- service: light.turn_on
data:
brightness_pct: 100
flash: short
rgb_color: '{{ colors.get(front + kitchen, 0) }}'
target:
entity_id: '{{ lights }}'
Original flash command, for hue, is too slow for my flash idea.
I want to flash close to stroboscope.
Later I will try your code.
Thank you
Your code is ok, but I want to resolve “my” (you’re too) code.
“My” code is ok with one or another door open. When I open two doors, the middle colour is restored.
I try to find where is the middle colour stored and later implement close to stroboscopic lights (not flash)
- id: '1656154710162'
alias: Automation Doors Alert
description: ''
trigger:
- platform: state
entity_id:
- binary_sensor.contact_door_front_contact
- binary_sensor.contact_door_kitchen_contact
from:
- 'on'
- 'off'
to:
- 'off'
- 'on'
condition: []
action:
- variables:
one_open: '{{ is_state(''binary_sensor.contact_door_front_contact'', ''on'')
}}'
two_open: '{{ is_state(''binary_sensor.contact_door_kitchen_contact'', ''on'')
}}'
- choose:
- conditions:
- condition: template
value_template: '{{ one_open }}'
- condition: template
value_template: '{{ not two_open }}'
sequence:
- service: scene.create
data:
scene_id: doors
snapshot_entities:
- light.hue_bulb_babaciuni
- light.hue_bulb_entrance_room
- light.hue_bulb_kitchen
- light.hue_bulb_pc
- light.hue_bulb_tv_2
- service: light.turn_on
data:
brightness_pct: 100
rgb_color:
- 255
- 0
- 0
target:
device_id:
- 56a5ade279b7761955e851ffc39eb038
- dab8e2497c63bd8c393d94f80ae89456
- 1631ca3d7cc1cda05575520e9089a17a
- 32dc34d856fb18390539e535b30dee96
- 59f0bcb38dfbaafd219a0035262714f9
- conditions:
- condition: template
value_template: '{{ not one_open }}'
- condition: template
value_template: '{{ two_open }}'
sequence:
- service: scene.create
data:
scene_id: doors
snapshot_entities:
- light.hue_bulb_babaciuni
- light.hue_bulb_entrance_room
- light.hue_bulb_kitchen
- light.hue_bulb_pc
- light.hue_bulb_tv_2
- service: light.turn_on
data:
rgb_color:
- 0
- 0
- 255
brightness_pct: 100
target:
device_id:
- dab8e2497c63bd8c393d94f80ae89456
- 56a5ade279b7761955e851ffc39eb038
- 1631ca3d7cc1cda05575520e9089a17a
- 32dc34d856fb18390539e535b30dee96
- 59f0bcb38dfbaafd219a0035262714f9
- conditions:
- condition: template
value_template: '{{ one_open }}'
- condition: template
value_template: '{{ two_open }}'
sequence:
- service: light.turn_on
data:
rgb_color:
- 255
- 0
- 255
brightness_pct: 100
target:
device_id:
- dab8e2497c63bd8c393d94f80ae89456
- 56a5ade279b7761955e851ffc39eb038
- 1631ca3d7cc1cda05575520e9089a17a
- 32dc34d856fb18390539e535b30dee96
- 59f0bcb38dfbaafd219a0035262714f9
- conditions:
- condition: template
value_template: '{{ not one_open }}'
- condition: template
value_template: '{{ not two_open }}'
sequence:
- service: scene.turn_on
target:
entity_id: scene.doors
metadata: {}
It creates the scene when the door opens and when the door close.
I have to create only when the door open