Automation with motion sensor not triggering with sun/lux condition

Hi, I have a motion sensor setup at my entrance (Philips Hue Outdoor sensor, I have one at my gate as well). As soon as I add the condition to run the automation between sunset and sunrise the automation doesn’t work.

Any suggestions highly appreciated!

alias: deCONZ motion Entré
description: >-
  Turns on Entré lights for 2 min (restarting), when motion is detected between
  sunset/sunrise and below 10 lux
trigger:
  - type: motion
    platform: device
    device_id: b070c094d874d0524a6f394f00417fe7
    entity_id: binary_sensor.entre_motion
    domain: binary_sensor
condition:
  - condition: numeric_state
    entity_id: sensor.infart_motion_light_level
    below: '10'
  - condition: and
    conditions:
      - condition: sun
        before: sunrise
        after: sunset
action:
  - service: light.turn_on
    target:
      entity_id: light.entre
    data:
      brightness_pct: 40
  - wait_for_trigger:
      - type: no_motion
        platform: device
        device_id: b070c094d874d0524a6f394f00417fe7
        entity_id: binary_sensor.entre_motion
        domain: binary_sensor
        for:
          hours: 0
          minutes: 2
          seconds: 0
          milliseconds: 0
  - service: light.turn_off
    target:
      entity_id: light.entre
    data:
      transition: 3
mode: restart

It’s never before sunrise and after sunset at the same time. You need two conditions inside an OR condition, one before sunrise and one after sunset.

1 Like

If you want to the condition to be between sunset and sunrise, use a State Condition like this:

condition:
  - condition: numeric_state
    entity_id: sensor.infart_motion_light_level
    below: '10'
  - condition: state
    entity_id: sun.sun
    state: below_horizon
1 Like

From the documentation Sunset/Sunrise Condition:
Therefore, to cover time between sunset and sunrise one need to use after: sunset and before: sunrise as 2 separate conditions and combine them using or .

condition:
  condition: or
  conditions:
    - condition: sun
      after: sunset
    - condition: sun
      before: sunrise
alias: deCONZ motion Entré
description: >-
  Turns on Entré lights for 2 min (restarting), when motion is detected between
  sunset/sunrise and below 10 lux
trigger:
  - platform: state
    entity_id: binary_sensor.entre_motion
    to: 'on'
condition:
  - "{{ states('sensor.infart_motion_light_level') | int < 10 }}"
  - condition: or
    conditions:
      - condition: sun
        after: sunset
      - condition: sun
        before: sunrise
action:
  - service: light.turn_on
    target:
      entity_id: light.entre
    data:
      brightness_pct: 40
  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.entre_motion
        to: 'off'
        for: '00:02'
  - service: light.turn_off
    target:
      entity_id: light.entre
    data:
      transition: 3
mode: restart

The above code contains wait_for_trigger that isn’t advisable if it spans for a very long time. You can specify the trigger separately like below-

alias: deCONZ motion Entré
description: >-
  Turns on Entré lights for 2 min (restarting), when motion is detected between
  sunset/sunrise and below 10 lux
trigger:
  - platform: state
    entity_id: binary_sensor.entre_motion
    to: 'on'
  - platform: state
    entity_id: binary_sensor.entre_motion
    to: 'off'
    for: '00:02'
condition:
  - "{{ states('sensor.infart_motion_light_level') | int < 10 }}"
  - condition: or
    conditions:
      - condition: sun
        after: sunset
      - condition: sun
        before: sunrise
action:
  - choose:
      - conditions:
          - "{{ trigger.to_state.state == 'on' }}"
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.entre
            data:
              brightness_pct: 40
    default:
      - service: light.turn_off
        target:
          entity_id: light.entre
        data:
          transition: 3
1 Like

Many thanks! I get the “sun” part now when reading the documentation after your example (and that I can use below.horizon as well).

But I don’t get that “wait_for_trigger” isn’t a good choice…? Will your example restart the timer for 2 minutes if motion is detected?
This was a little more complicated than my knowledge, so sorry for asking. But still learning and enjoying HA :slight_smile: !

The wait_for_trigger is not a good choice if the area is crowded. The reason simply because the automation will be waiting for the OFF trigger - and if the server is down/restarted while waiting, the automation will no longer work.

However, if the area is not crowded, the use of wait_for_trigger is OK.

1 Like

Thanks,
having the same issue.
Found the solution as well in the help, but it is not accepted

“Message malformed: Unexpected value for condition: ‘{‘condition’: ‘or’, ‘conditions’: [{‘condition’: ‘sun’, ‘before’: ‘sunrise’}, {‘condition’: ‘sun’, ‘after’: ‘sunset’}]}’. Expected and, device, not, numeric_state, or, state, sun, template, time, trigger, zone @ data[‘condition’][0]”

Was there any change meanwhile in the format?