Activate scene when PIR detected motion in the past half hour

I created an automation that should activate an evening scene to turn on some lights. This should only happen when motion was detected in the past half hour.

The duration parameter in the conditions can be used to see if the state is active for the configured duration. But I want the opposite: if the PIR is at state ‘clear’ for 30 mins, apparently nobody has moved in the living room for 30 mins, so I consider the room empty. To my surprise, the lights were on though when we arrived home yesterday. So, what is my mistake? Why isn’t the NOT condition working as expected?

My automation looks like this:

- id: '1602490760898'
  alias: Beneden avond verlichting aan door avondstand
  description: ''
  trigger:
  - platform: state
    entity_id: input_select.part_of_day
    to: evening
  condition:
  - type: is_illuminance
    condition: device
    device_id: e6f4ed75a50a44cb8af444989fce5dd6
    entity_id: sensor.0x00158d0004667d0f_illuminance
    domain: sensor
    below: 100
  - condition: not
    conditions:
    - type: is_motion
      condition: device
      device_id: e6f4ed75a50a44cb8af444989fce5dd6
      entity_id: binary_sensor.0x00158d0004667d0f_occupancy
      domain: binary_sensor
      for:
        hours: 0
        minutes: 30
        seconds: 0
  action:
  - scene: scene.woonkamer_avond
  mode: single

Device conditions are a little murky for me, but it looks the automation will only fire if there is not motion for 30 minutes. That is, the lights will only turn on when you don’t want them to!

I don’t know if what you want is available in “canned” automation functionality. One thing you could do is create a template binary sensor that goes “off” once there is no motion for 30 minutes, and you could use that as a condition in your automation. Here is a snippet from where I am doing something similar:

binary_sensor:
  - platform: template
    sensors:
      motion_last_hour:
        friendly_name: "Motion in last hour"
        delay_off:
          #56 minutes plus 4 minutes sensor update = 60 minutes
          minutes: 56
        value_template: "{{ is_state('sensor.living_room_multisensor_burglar', '8') }}"

If your PIR sensor has a last_updated attribute, you could also use the PIR being off and if it was last updated more than 30 minutes ago as a condition instead.

Hmm, I think I accidently changed my automation when writing this post, so I am confused myself now.

Anyways, I would prefer a ‘canned’ automation as you aptly call it. I am trying not to switch to evening mode (turning on a few lights) if nobody is home.

So actually, what the condition should be, is

 condition:
  - type: is_illuminance
    condition: device
    device_id: e6f4ed75a50a44cb8af444989fce5dd6
    entity_id: sensor.0x00158d0004667d0f_illuminance
    domain: sensor
    below: 100
  - condition: not
    conditions:
    - type: is_no_motion
      condition: device
      device_id: e6f4ed75a50a44cb8af444989fce5dd6
      entity_id: binary_sensor.0x00158d0004667d0f_occupancy
      domain: binary_sensor
      for:
        hours: 0
        minutes: 30
        seconds: 0

I would think it would execute now when the PIR is not cleared for the last 30 minutes right?