⚙ Turn Light, Switch, Entity or Scene On & Off with Trigger Conditions

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

GitHub Link Click Here

Version: 1.4

The most common automation used to turn ON and OFF a light, switch, entity or a scene with a binary sensor, the sun’s elevation, an ambient light value or time when their value changes state or crosses over their set value.

The Automation Process:

  • Turn ON a Light, Switch, Entity or a Scene with a binary sensor, the sun’s elevation, an ambient light value or time.
  • Turn OFF a Light, Switch, Entity or a Scene with a binary sensor, 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.

Need help? See our FAQ: Click Here

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.4**
    
    
    The most common automation used to turn ON and OFF a light, switch, entity or a scene with a binary sensor, the sun's elevation, an ambient light value or time when their value changes state or crosses over their set value.
    
    
    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 binary sensor, the sun's elevation, an ambient light value or time.
     - Turn OFF a Light, Switch, Entity or a Scene with a binary sensor, 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.

     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)
  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
          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_binary_sensor:
      name: Use The Binary Sensor Option (Optional)
      description: This is used for adding a condition to only work when a [Binary Sensor](https://www.home-assistant.io/integrations/binary_sensor/)
        is ON or OFF. It will not work 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: binary_disabled
      selector:
        select:
          options:
            - label: Enable the binary sensor option
              value: "binary_enabled"
            - label: Disable the binary sensor option
              value: "binary_disabled"
    binary_sensor:
      name: Binary Sensor
      description: Select the binary sensor. The binary sensor must stay ON or OFF. If you would like more than one binary sensor please use a group helper.
      default: []
      selector:
        entity:
          domain: binary_sensor
    include_sun:
      name: Use The Sun Option (Optional)
      description: This is used for adding a condition to only 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 condition to only 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:
          domain: sensor
          device_class: illuminance
    ambient_light_value:
      name: Ambient Light LUX Value
      description: Set the Ambient Light LUX Value. Guide is 20 lux (dusk) and 80 lux (dawn).
      default: 20
      selector:
        number:
          min: 0
          max: 500
          step: 10
          unit_of_measurement: LUX
    include_time:
      name: Use The Time Options (Optional)
      description: This is used for adding a condition to only 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"

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_binary_sensor: !input include_binary_sensor
  binary_sensor: !input binary_sensor
  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
  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

trigger:
  - platform: state
    id: "t1"
    entity_id: !input binary_sensor
    from: "off"
    to: "on"
  - platform: state
    id: "t2"
    entity_id: !input binary_sensor
    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
  - 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 binary sensor & check trigger t1 & t2
        conditions:
          -  "{{ include_binary_sensor == 'binary_enabled' }}"
          - condition: trigger
            id: 
              - 't1'
              - 't2'
      - 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 Binary Sensor
  - condition: or
    conditions:
      - "{{ include_binary_sensor == 'binary_disabled' }}"
      - "{{ binary_sensor == [] }}"
      - "{{ (include_binary_sensor == 'binary_enabled') and (states[binary_sensor].state == 'on') }}"
      - "{{ (include_binary_sensor == 'binary_enabled') and (states[binary_sensor].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' }}"

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

CHANGELOG

  • Version: 1.: - 28 March-28 - Select any entity you like.
  • Version: 1.3: - 13 March-23 - Update typo in variables.
  • Version: 1.2: - 31 Jan-23 - Made weekdays a global option.
  • Version: 1.1: - 30 Jan-23 - Fixed time trigger.
  • Version: 1.0: - 30 Jan-23 - Initial release.

MY OTHER BLUEPRINTS
Sensor Light - Motion Sensor - Door Sensor - Sun Elevation - LUX Value - Scenes - Time
Smart Relay - Contactor
Push Button Relay - Contactor
Toilet Exhaust Fan with time delay
Timer Relay - Run ON Timer
Bathroom Humidity Exhaust Fan
Entity - Run ON Timer
Temperature Control Exhaust Fan
Temperature Control Exhaust Fan - Inverted

7 Likes

Q: How to use the “The By-pass” without having a physical switch?

A: Go to Settings / Devices & Services / click on the “Helpers” tab / click “+ CREATE HELPER” and select “Toggle”. In the name put “Light By-pass” and click save. Then go back into the automation and in the “The By-pass” search for the name you created in this case “Light By-pass” and click save. Now in a “Dashboard” click “Edit dashboard” then click “+ ADD CARD” click on “Button” or “Entities” in the entity field again search for the name you created in this case “Light By-pass” and click save. Then click “DONE”. You now have a “By-pass” switch to disable the Light, Switches or Scenes.

Q: I would like to set a different value but the slider is not letting me?

A Just simply type the number value you would like in the field and click save. A red line will appear under the number you have entered but once you click save, exit the automation and go back into it the red line will not be there.

New update 1.2

  • Fixed time trigger
  • Made weekdays a global option for all trigger conditions.

Enjoy

Blacky :grinning:

This is looking great after a first quick glance, thanks for sharing!
I will test this tonight in combination with an existing automation that turns a night light on permanently between sunset and midnight.
Cheers,
Uwe

Thanks for your kind words.

Please make sure you update to V1.2 if you haven’t already. As I just fixed the time triggers just for what you are trying to do.

Edit: Tip, your settings for this would be the sun condition and the time would be, “On Time” could be any time between sunset and just before midnight (say 11:30 PM) and “Off Time” would be midnight.

That was fast. Even with time travel! Or are you in a time zone where it’s already Jan. 31st? :wink:

Just discovered your earlier “Sensor Light” blueprint and might switch over to this one, though. Still the same use case, but involving a motion sensor, so that looks more suitable to me.

Yep, already 31st. :+1:

Can you add Numeric state aswell of an entity?

IE - condition: numeric_Stare
entity_id:
above:
below:

@cbothma Hi Christoff

Could you give me your use case?

Help.
I’m new to HA and for the life of me i can’t get the on and off times to work.

I have the trigger as a light sensor but only want it to trigger between 9-5 if it gets too dark in the house for the plants. But it seems to trigger randomly outside of these times.

Hi @Stevie , firstly welcome to the community :+1:.

This automation works when it passes through or over the trigger condition. So if you have a light sensor every time it passes over the set point it will turn the light ON and or OFF. If you have also set the time trigger then every time it passes over the set time it will turn ON and or OFF. The only global condition is weekdays.

The automation was developed as a lot of people just wanted a simple ON / OFF for a light with time. I added extra options to choose from when developing it if time was not your thing, like LUX, sun & binary sensor. You can choose weekday as a global setting so it could be used in a office environment were Monday to Friday you need the lights ON and OFF. Example: If using time option set the time to turn the lights ON at 8 am and OFF at 6 pm Monday to Friday.

Normally you would choose just one option.

So for what your trying to do, this automation will not work.

This works fantastic, really appreciate the blueprint!! I might tweak it to add the ability to control entities (door locks, garage etc) so I can have schedules like lock the door, shut the garage and turn off all the lights at a set time and unlock the doors and turn on lights in the morning. That would basically make this an all in one starter for any automation, really.

@jclendineng

Hi Joel, I will do an update to include any entity you like. Just update to Version: 1.4 and you will be good to go.

New update 1.4

  • You can now select any entity you like to turn ON and OFF not just lights, switches or scenes.

Enjoy

Blacky :grinning:

1 Like