Automation with trigger and condition

Dear Community,

may I ask for your help? I have a really simple automation that works with two triggers which turns on my staircase light when motion is detected (trigger 1) and turns off when motion is no longer detected (trigger 2). Does anyone have an idea why this automation stops working when I add an additional condition “after_sunset” and “before_sunrise”? In this case the light is switched on briefly (less than 1 second) and switched off again immediately. See both YAML codes below.

My System:

  • Raspberry PI 4 64bit 8GB
  • Home Assistant 2022.9.7
  • Supervisor 2022.09.1
  • Operating System 9.0
  • Frontend 20220907.2 - latest

Here is the YAML code that works:

alias: Light staircase on movement
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 1234...
    entity_id: binary_sensor.motion_sensor_staircase
    domain: binary_sensor
    id: motion-detected
  - type: no_motion
    platform: device
    device_id: 1234...
    entity_id: binary_sensor.motion_sensor_staircase
    domain: binary_sensor
    id: no-motion
condition: []
action:
  - if:
      - condition: trigger
        id: motion-detected
    then:
      - type: turn_on
        device_id: 5678...
        entity_id: light.staircase
        domain: light
        brightness_pct: 40
    else:
      - type: turn_off
        device_id: 5678...
        entity_id: light.staircase
        domain: light
mode: single

And this code turns on the light for less than a second and immediately off again:

alias: Light staircase on movement and sun position
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 1234...
    entity_id: binary_sensor.motion_sensor_staircase
    domain: binary_sensor
    id: motion-detected
  - type: no_motion
    platform: device
    device_id: 1234...
    entity_id: binary_sensor.motion_sensor_staircase
    domain: binary_sensor
    id: no-motion
condition: []
action:
  - if:
      - condition: and
        conditions:
          - condition: sun
            before: sunrise
            after: sunset
          - condition: trigger
            id: motion-detected
    then:
      - type: turn_on
        device_id: 5678...
        entity_id: light.staircase
        domain: light
        brightness_pct: 40
    else:
      - type: turn_off
        device_id: 5678...
        entity_id: light.staircase
        domain: light
mode: single

Many thanks in advance
Jürgen

Try this version:

alias: Light staircase on movement
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.motion_sensor_staircase
    from:
      - 'on'
      - 'off'
    to:
      - 'off'
      - 'on'
condition: "{{ is_state('sun.sun', 'below_horizon') }}"
action:
  - service: light.turn_on
    target:
      entity_id: light.staircase
    data:
      brightness_pct: "{{ iif(trigger.to_state.state == 'on', 40, 0) }}"
mode: single
2 Likes

In your non-working automation conditions:

Conditions are ‘and’ by default so you can simply list the conditions and don’t need the ‘and’.

Also if it’s after sunset then it can not also therefore be before sunset on any given one day. It does not span across midnight to the following day. So it’s either after sunset OR before sunrise.

  - condition: or
    conditions:
      - condition: sun
        after: sunset
      - condition: sun
        before: sunrise
  - condition: trigger
    id: motion-detected

You could use an ‘or’ for the two Sun conditions or as @123 suggested in template form you could use:

  - condition: state
    entity_id: sun.sun
    state: below_horizon
1 Like

Thank you very much @123. Your solution works very well, but unlike @rossk’s solution, I don’t quite understand it. Unfortunately switching to the visual editor doesn’t help me either, because it can’t display the following lines:

    from:
      - 'on'
      - 'off'
    to:
      - 'off'
      - 'on'

Can you give me a short explanation what these lines do?

Oh yes, there I had a thinking error, sunrise and sunset refer to different days. With OR it works fine. Thank you very much @rossk.

1 Like

A binary_sensor can have four possible states:

  1. on
  2. off
  3. unavailable
  4. unknown

We only want the State Trigger to listen for state-changes from on to off and off to on. We don’t want it to trigger for other state-changes such as unavailable to off. Therefore we specify a list of desired states in the from and to options.

The Automation Editor has several known limitations (and bugs) and you have encountered one of them. Home Assistant’s scripting language, represented in YAML, is capable of more things than the Automation Editor is able to display in Visual mode.

1 Like

Ok, the list defines the states to watch for, I understand that, but where is it defined that the light is turned off again when there is no more movement.

When the binary_sensor’s state changes to off the value of trigger.to_state.state becomes off. The Immediate If statement in the following template determines the value of the brightness_pct option.

brightness_pct: "{{ iif(trigger.to_state.state == 'on', 40, 0) }}"

When trigger.to_state.state is off the template reports 0 which effectively serves to turns off a light.

1 Like

I’ve read it a few times now but still don’t get it. I will try a little harder tomorrow, now my head is smoking. Anyway many thanks @123 for your support

Now, with a fresh head, I got it. Quite tricky and ingenious :slight_smile: Thank you very much @123

1 Like

@123: Can I ask for your help again? How to change the code so that the light is not turned off immediately when no more movement is detected, but remains on for 2 minutes?

alias: Light staircase on movement
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.motion_sensor_staircase
    from:
      - 'on'
      - 'off'
    to:
      - 'off'
      - 'on'
condition: "{{ is_state('sun.sun', 'below_horizon') }}"
action:
  - service: light.turn_on
    target:
      entity_id: light.staircase
    data:
      brightness_pct: "{{ iif(trigger.to_state.state == 'on', 40, 0) }}"
mode: single

alias: Light staircase on movement
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.motion_sensor_staircase
    from: 'on'
    to: 'off'
    for:
      minutes: 2
  - platform: state
    entity_id: binary_sensor.motion_sensor_staircase
    from: 'off'
    to: 'on'
condition: "{{ is_state('sun.sun', 'below_horizon') }}"
action:
  - service: light.turn_on
    target:
      entity_id: light.staircase
    data:
      brightness_pct: "{{ iif(trigger.to_state.state == 'on', 40, 0) }}"
mode: single
1 Like

Works like a charm :slight_smile: Thank you very much :+1: