Trigger for dark clouds and light

Hi guys,

I have the following issue.
I had an automation which is triggered by sunset.
Sometimes it gets darker earlier when dark clouds gather.
Therefore I set up the trigger: If illuminance is below 200lx.

My problem is now i.e. I want to start looking a film and turn off all lights.
Unfortunately I am not able to turn them off, because my automation kicks in.
How do you handle this topic?

alias: sonnenuntergang licht wohnzimmer
description: ''
trigger:
  - platform: numeric_state
    entity_id: sensor.terasse_eingang_sensor_illuminance
    below: '200'
    for:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
condition: []
action:
  - service: light.turn_on
    target:
      entity_id: light.wohnzimmer
    data:
      brightness_pct: 60
      color_name: pink
  - service: light.turn_on
    target:
      entity_id: light.kuche
    data:
      brightness_pct: 30
  - service: light.turn_on
    target:
      entity_id: light.wohnzimmer_spots
    data:
      brightness_pct: 25
  - service: light.turn_off
    target:
      entity_id:
        - light.trager
        - light.signify_netherlands_b_v_lwe002_level_on_off
        - light.esstisch
mode: single

I’ve just recently been dealing with this question (reverse case - turn lights on if it gets too dark inside during the day). For now, I ended up with a template binary sensor, which turns on if attribute „daylight“ is true. The sensor’s state is used as condition in my automation.

I have an input_boolean called “lightning_night_mode” which is turned on when it is getting dark. I have an automation which reacts to the state changing to on. This automation uses the Choose action, to do the logic. So for example - if I am away from home, all that happens is one light turns on in the livingroom - the rest will activate with motion detectors - for the dog.

If you have a device that you are watching the movie on, that Home assistant can see the state of, then you can simply use it in the conditions of the choose (I use NOT to cover all the bases: NOT playing or off). If you don’t have a device like this, just create an input_boolean like “livingroom_media_override” which you can then use in the condition. So if the state is on, that part of the choose action returns false and the automation doesn’t do anything.

In the UI Automation editor, you can do this for multiple rooms, by simply adding a Choose action under default actions and then that Choose block will be run after the one above it, each time you do this, another default actions block will appear at the bottom of the automation, so you can do it again.

In YAML it looks something look this:

alias: 'AUTO Lighting: Livingroom'
description: ''
trigger:
  - platform: device
    type: turned_on
    device_id: f2895dc2612fdd5d8a39ce4d28e100d6
    entity_id: light.fireplace_light
    domain: light
    id: fireplace_on
  - platform: device
    type: turned_on
    device_id: 8e9341d5bbe8f7ab55e1bb2492774367
    entity_id: light.livingroom_window_light
    domain: light
    id: window_on
  - platform: state
    entity_id: input_number.livingroom_light
    id: level_change
  - platform: state
    entity_id: input_boolean.home_state_home
    id: home_state
  - platform: state
    entity_id: input_boolean.lighting_night_mode
    id: evening_lighting
    from: 'off'
    to: 'on'
condition:
  - condition: state
    entity_id: input_boolean.livingroom_media_lighting_override
    state: 'off'
action:
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: trigger
                id: fireplace_on
              - condition: trigger
                id: window_on
              - condition: trigger
                id: led_on
        sequence:
          - service: light.turn_on
            data_template:
              entity_id: '{{ trigger.entity_id }}'
              brightness_pct: '{{ states(''input_number.livingroom_light'')|int }}'
      - conditions:
          - condition: trigger
            id: level_change
        sequence:
          - service: script.turn_on
            target:
              entity_id: script.adjust_brightness_if_on
            data:
              variables:
                light_target: light.fireplace_light
                brightness_target: '{{ states(''input_number.livingroom_light'')|int }}'
          - service: script.turn_on
            target:
              entity_id: script.adjust_brightness_if_on
            data:
              variables:
                light_target: light.livingroom_window_light
                brightness_target: '{{ states(''input_number.livingroom_light'')|int }}'
    default: []
  - choose:
      - conditions:
          - condition: trigger
            id: home_state
          - condition: state
            entity_id: input_boolean.home_state_home
            state: 'on'
          - condition: state
            entity_id: input_boolean.lighting_night_mode
            state: 'on'
          - condition: state
            entity_id: input_boolean.livingroom_media_override
            state: 'off'
        sequence:
          - service: light.turn_on
            target:
              area_id: e5bc628a4b6246edb87cb986144a32cb
      - conditions:
          - condition: trigger
            id: home_state
          - condition: state
            entity_id: input_boolean.home_state_home
            state: 'off'
          - condition: state
            entity_id: input_boolean.lighting_night_mode
            state: 'on'
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.fireplace_light
    default: []
mode: parallel
max: 10

I’m always struggling with the automation modes, so why did you choose „parallel“?

I have multiple things that can trigger the automation, eg - turning on a light in the livingroom will set the brightness level of the light to the input_number slider. That should still be able to happen, if at the same time something else triggers the script - for instance, turning on ALL the lights in the livingroom will trigger the script 3 times. If I had it set to single only the first light to trigger would set it’s brightness after turning on. If I had it set to restart - only one light would set it’s the brightness. Parallel allows the automation to run multiple times at once.

Put another way to make it easy to visualise:

Single: If the script is ALREADY running, it won’t run again.
Parallel: The script will run every time it is triggered, even if it was already running.
Restart: If the script was already running, it will be killed and restarted with the latest trigger.

1 Like

Do you have any way for HA to know if you are watching a film?

If not then the only way that I know to handle it is to create an input boolean (helper) and turn that on when you don’t want the automation to run. then you can put that being off as a condition in the automation.

Sorry for not answering… I have some new and bigger problems with my Smart Home now.
I like your ideas. I will also combine it in conditions “if Television is running”.