Skip to content

Instantly share code, notes, and snippets.

@Blackshome
Last active January 4, 2024 13:55
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Blackshome/e6c8f1bf846bab2fa4431934a0a85770 to your computer and use it in GitHub Desktop.
Save Blackshome/e6c8f1bf846bab2fa4431934a0a85770 to your computer and use it in GitHub Desktop.
entities-on-off-trigger-conditions.yaml
blueprint:
name: Turn Light, Switch, Entity or Scene On & Off with Trigger Conditions
description: >
# ⚙️ Turn Light, Switch, Entity or Scene On & Off with Trigger Conditions
**Version: 1.7**
Let us know what you think of this blueprint and for community support including updates: [Click Here](https://community.home-assistant.io/t/turn-light-switch-or-scene-on-off-with-trigger-conditions/527354?u=blacky)
**The Automation Process:**
- Turn ON a Light, Switch, Entity or a Scene with a entity ON & OFF state, the sun's elevation, an ambient light value or time.
- Turn OFF a Light, Switch, Entity or a Scene with a entity ON & OFF state, the sun's elevation, an ambient light value or time.
- You have the option to set weekdays. This is a global option for all trigger conditions.
- You have the option to enter in any global conditions you like.
Required = *
**Need help?** See our FAQ: [Click Here](https://community.home-assistant.io/t/turn-light-switch-or-scene-on-off-with-trigger-conditions/527354/2?u=blacky)
**If you like my blueprints, and would like to show your support or just say thank you?** [Click Here](https://www.paypal.com/donate/?hosted_button_id=WAZS3QSDTPGA8) 🙂
domain: automation
input:
entity_switch:
name: Lights - Switches - Entities - Scenes *
description: The lights that get turned ON and OFF with a binary sensor, the sun's elevation, an ambient light value or time.
You can also add switches and scenes. If adding a scene please read "Scenes To Turn OFF" below.
selector:
target:
end_scenes:
name: Scenes To Turn OFF
description: If you have selected a scene to be turned ON above in "Lights - Switches - Entities - Scenes" and you would like it to be turned OFF,
then you must create another identical scene with everything OFF and select it here.
default: []
selector:
entity:
multiple: true
filter:
domain:
- scene
include_light_control:
name: Light Control
description: Select the options you would like to use. It will only control a "light" entity that has a brightness setting.
default: dont_use_brightness
selector:
select:
options:
- label: Use brightness
value: "use_brightness"
- label: Dont use brightness
value: "dont_use_brightness"
light_brightness:
name: Brightness
description: Brightness of the lights when they are turned ON.
default: 100
selector:
number:
min: 1
max: 100
mode: slider
step: 1
unit_of_measurement: '%'
include_bypass:
name: Use The By-pass Option (Optional)
description: Select enable or disable.
default: bypass_disabled
selector:
select:
options:
- label: Enable the By-pass option
value: "bypass_enabled"
- label: Disable the By-pass option
value: "bypass_disabled"
motion_bypass:
name: By-pass
description: Select a switch that will By-pass the options and make your lights function as normal.
The entity cannot be included in the "Lights - Switches - Entities - Scenes" selection.
default: []
selector:
entity:
include_entity_input:
name: Use The Entity State Option (Optional)
description: This is used for adding a trigger when a entity changes it state from ON to OFF or from OFF to ON. It will not work correctly with a motion sensor. If you would like to use a motion sensor then please consider this blueprint
[Click Here](https://community.home-assistant.io/t/sensor-light-motion-sensor-door-sensor-sun-elevation-lux-value-scenes-time-light-control-device-tracker-night-lights/481048).
default: entity_disabled
selector:
select:
options:
- label: Enable the entity state option entity_disabled
value: "entity_enabled"
- label: Disable the entity state option
value: "entity_disabled"
entity_input:
name: Input Entity
description: Select the entity you would like to use. The entity must be ON or OFF.
default: []
selector:
entity:
include_sun:
name: Use The Sun Option (Optional)
description: This is used for adding a trigger to work when the Sun elevation crosses over its elevation value.
default: sun_disabled
selector:
select:
options:
- label: Enable the sun option
value: "sun_enabled"
- label: Disable the sun option
value: "sun_disabled"
sun_elevation:
name: Sun Elevation
description: This is the angle between the sun and the horizon. Negative values mean the sun is BELOW the horizon. Guide is -1.5 (dusk) and -4.0 (dawn).
default: -1.5
selector:
number:
min: -10
max: 5
step: 0.5
unit_of_measurement: degrees
include_ambient:
name: Use The Ambient Option (Optional)
description: This is used for adding a trigger to work when the Ambient Light LUX Value crosses over its LUX value.
default: ambient_disabled
selector:
select:
options:
- label: Enable the ambient option
value: "ambient_enabled"
- label: Disable the ambient option
value: "ambient_disabled"
ambient_light_sensor:
name: Ambient Light Sensor
description: Select the ambient light sensor.
default: []
selector:
entity:
filter:
domain: sensor
device_class: illuminance
ambient_light_value:
name: Ambient Light LUX Value - ON Trigger
description: Set the Ambient Light LUX Value. This value must be equal or lower than "LUX OFF Trigger" value below.
Guide is 20 lux (dusk).
default: 20
selector:
number:
min: 0
max: 500
step: 10
unit_of_measurement: LUX
ambient_light_value_off:
name: Ambient Light LUX Value - OFF Trigger
description: Set the Ambient Light LUX Value. This value must be equal or higher than "LUX ON Trigger" value above.
Guide is 80 lux (dawn).
default: 80
selector:
number:
min: 0
max: 1000
step: 10
unit_of_measurement: LUX
include_time:
name: Use The Time Options (Optional)
description: This is used for adding a trigger to work when the time crosses over its set value.
default: time_disabled
selector:
select:
options:
- label: Enable the time options
value: "time_enabled"
- label: Disable the time options
value: "time_disabled"
after_time:
name: On Time
description: Set the on time.
default: 00:00:00
selector:
time:
before_time:
name: Off Time
description: Set the off time.
default: 00:00:00
selector:
time:
include_weekdays:
name: Use The Weekdays Option (Optional)
description: This is used for adding a condition to only work on set weekdays. This is a global option for all trigger conditions.
default: weekday_disabled
selector:
select:
options:
- label: Enable the weekday option
value: "weekday_enabled"
- label: Disable the weekday option
value: "weekday_disabled"
weekday_options:
name: Weekdays
description: Select the days of the week you would like the automation to run.
default:
- mon
- tue
- wed
- thu
- fri
- sat
- sun
selector:
select:
multiple: true
mode: list
options:
- label: Monday
value: "mon"
- label: Tuesday
value: "tue"
- label: Wednesday
value: "wed"
- label: Thursday
value: "thu"
- label: Friday
value: "fri"
- label: Saturday
value: "sat"
- label: Sunday
value: "sun"
global_conditions:
name: Global Conditions
description: Enter any global conditions you would like to apply to the automation.
default: []
selector:
condition:
mode: restart
max_exceeded: silent
variables:
entity_switch: !input entity_switch
end_scenes: !input end_scenes
include_light_control: !input include_light_control
light_brightness: !input light_brightness
include_bypass: !input include_bypass
motion_bypass: !input motion_bypass
include_entity_input: !input include_entity_input
entity_input: !input entity_input
include_sun: !input include_sun
sun_elevation: !input sun_elevation
include_ambient: !input include_ambient
ambient_light_sensor: !input ambient_light_sensor
ambient_light_value: !input ambient_light_value
ambient_light_value_off: !input ambient_light_value_off
include_time: !input include_time
after_time: !input after_time
before_time: !input before_time
include_weekdays: !input include_weekdays
weekday_options: !input weekday_options
global_conditions: !input global_conditions
trigger:
- platform: state
id: "t1"
entity_id: !input entity_input
from: "off"
to: "on"
- platform: state
id: "t2"
entity_id: !input entity_input
from: "on"
to: "off"
- platform: numeric_state
id: "t3"
entity_id: sun.sun
attribute: elevation
below: !input sun_elevation
- platform: numeric_state
id: "t4"
entity_id: sun.sun
attribute: elevation
above: !input sun_elevation
- platform: numeric_state
id: "t5"
entity_id: !input ambient_light_sensor
below: !input ambient_light_value
- platform: numeric_state
id: "t6"
entity_id: !input ambient_light_sensor
above: !input ambient_light_value_off
- platform: time
id: "t7"
at: !input after_time
- platform: time
id: "t8"
at: !input before_time
# All Conditions
condition:
#Trigger conditions
- condition: or
conditions:
- condition: and # trigger by entity input & check trigger t1
conditions:
- "{{ include_entity_input == 'entity_enabled' }}"
- condition: trigger
id:
- 't1'
- condition: state
entity_id: !input entity_input
match: any
state: 'on'
- condition: and # trigger by entity input & check trigger t2
conditions:
- "{{ include_entity_input == 'entity_enabled' }}"
- condition: trigger
id:
- 't2'
- condition: state
entity_id: !input entity_input
match: any
state: 'off'
- condition: and # trigger by sun & check trigger t3 & t4
conditions:
- "{{ include_sun == 'sun_enabled' }}"
- condition: trigger
id:
- 't3'
- 't4'
- condition: and # trigger by ambient & check trigger t5 & t6
conditions:
- "{{ include_ambient == 'ambient_enabled' }}"
- condition: trigger
id:
- 't5'
- 't6'
- condition: and # trigger by time & check trigger t7 & t8
conditions:
- "{{ include_time == 'time_enabled' }}"
- condition: trigger
id:
- 't7'
- 't8'
# Check Motion Sensor Manual By-pass
- condition: or
conditions:
- "{{ include_bypass == 'bypass_disabled' }}"
- "{{ motion_bypass == [] }}"
- "{{ (include_bypass == 'bypass_enabled') and (states[motion_bypass].state == 'off') }}"
# Check Entity Input
- condition: or
conditions:
- "{{ include_entity_input == 'entity_disabled' }}"
- "{{ entity_input == [] }}"
- "{{ (include_entity_input == 'entity_enabled') and (states[entity_input].state == 'on') }}"
- "{{ (include_entity_input == 'entity_enabled') and (states[entity_input].state == 'off') }}"
# Check Sun Elevation
- condition: or
conditions:
- "{{ include_sun == 'sun_disabled' }}"
- "{{ (include_sun == 'sun_enabled') and (state_attr('sun.sun','elevation') <= sun_elevation | float(90)) }}"
- "{{ (include_sun == 'sun_enabled') and (state_attr('sun.sun','elevation') >= sun_elevation | float(90)) }}"
# Check Ambient Light Sensor
- condition: or
conditions:
- "{{ include_ambient == 'ambient_disabled' }}"
- "{{ ambient_light_sensor == [] }}"
- "{{ (include_ambient == 'ambient_enabled') and (states[ambient_light_sensor].state | int < ambient_light_value | int) }}"
- "{{ (include_ambient == 'ambient_enabled') and (states[ambient_light_sensor].state | int > ambient_light_value | int) }}"
# Check The Time Options
- condition: or
conditions:
- "{{ include_time == 'time_disabled' }}"
- "{{ include_time == 'time_enabled' }}"
# Check The Weekday Option
- condition: or
conditions:
- "{{ include_weekdays == 'weekday_disabled' }}"
- condition: and
conditions:
- condition: time
weekday: !input weekday_options
- "{{ include_weekdays == 'weekday_enabled' }}"
# Global Conditions
- condition: and
conditions: !input global_conditions
action:
- choose:
- conditions:
- condition: trigger
id:
- 't1'
- 't3'
- 't5'
- 't7'
sequence:
- alias: 'Turn on'
service: homeassistant.turn_on
target: !input entity_switch
- choose:
- alias: 'Set the brightness for the light switch'
conditions:
- condition: template
value_template: "{{ include_light_control == 'use_brightness' }}"
sequence:
- service: light.turn_on
target: !input entity_switch
data:
brightness_pct: !input light_brightness
- conditions:
- condition: trigger
id:
- 't2'
- 't4'
- 't6'
- 't8'
sequence:
- alias: 'Turn off'
service: homeassistant.turn_off
target: !input entity_switch
- alias: "Turn off the scenes"
service: scene.turn_on
entity_id: !input end_scenes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment