Simple light/motion sensor automation randomly doesn't work

Hello,
I’m quite new with HA and just having trouble with some simple flow where I just don’t find my error.
The problem is, that my flow sometimes works - sometimes not. It’s a light group that turns on with motion sensor and off in the same flow, when no motion was detected for a while. In the log I can see the motion sensor publishes the “not motion action” but the lights just don’t turn off. And sometimes the lights also don’t turn on. It seems to be totally randomly.

alias: Flur Bewegung (7-24) (1min 15lx)
description: ''
trigger:
  - type: motion
    platform: device
    device_id: 227be23ed3ac0629b3cdb9c00ca07ca4
    entity_id: binary_sensor.bewegungsmelder_flur_occupancy
    domain: binary_sensor
    id: flur-an
  - type: no_motion
    platform: device
    device_id: 227be23ed3ac0629b3cdb9c00ca07ca4
    entity_id: binary_sensor.bewegungsmelder_flur_occupancy
    domain: binary_sensor
    for:
      hours: 0
      minutes: 1
      seconds: 0
    id: flur-aus
condition:
  - condition: time
    after: '07:00:00'
    before: '23:59:59'
  - condition: numeric_state
    entity_id: sensor.bewegungsmelder_flur_illuminance_lux
    below: '15'
action:
  - choose:
      - conditions:
          - condition: trigger
            id: flur-an
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.flur_schiene_komplett
      - conditions:
          - condition: trigger
            id: flur-aus
        sequence:
          - service: light.turn_off
            data:
              transition: 5
            target:
              entity_id: light.flur_schiene_komplett
    default: []
mode: single

Does anyone have an idea what went wrong here?

Regards
Sebastian

I try not to use devices for triggers. I have always had problems when I do. I do something similar for my bar.

- id: 'xxxxxxxxxxxxxxxxx'
  alias: Bar Lights - Turn on and off based on stairwell light and motion sensors.
  description: Turn on and off all the lights in the bar area
  trigger:
    - id: 'bar_on'
      platform: state
      entity_id: binary_sensor.bar_stairwell_motion_sensor_motion
      from: 'off'
      to: 'on'
    - id: 'bar_on'
      platform: state
      entity_id: binary_sensor.bar_main_motion_sensor_motion
      from: 'off'
      to: 'on'
    - id: 'bar_on'
      platform: state
      entity_id: switch.stairwell_light
      from: 'off'
      to: 'on'
    - id: 'bar_off'
      platform: state
      entity_id: binary_sensor.bar_main_motion_sensor_motion
      from: 'on'
      to: 'off'
      for: '00:01:30'
    - id: 'bar_off'
      platform: state
      entity_id: switch.stairwell_light
      from: 'on'
      to: 'off'
    - id: 'bar_off1'
      platform: state
      entity_id: binary_sensor.bar_stairwell_motion_sensor_motion
      from: 'on'
      to: 'off'
      for: '00:01:30'
  condition: []
  action:
  - choose:
    - conditions:
        - "{{ trigger.id == 'bar_on' }}"
      sequence:
        - service: switch.turn_on
          target:
            entity_id:
            - switch.area_cans
            - switch.bar_pool_table_light
            - switch.rope_lights
            - switch.bar_hall_to_media
            - switch.bar_landing
            - switch.stairwell_light
        - service: light.turn_on
          target:
            entity_id:
            - light.bar_pendant_light
            - light.bar_back_bar_cans
          data:
            brightness_pct: 50
    - conditions:
        - "{{ trigger.id == 'bar_off' }}"
      sequence:
        - service: switch.turn_off
          target:
            entity_id:
            - switch.area_cans
            - switch.bar_pool_table_light
            - switch.rope_lights
            - switch.bar_hall_to_media
            - switch.bar_landing
            - switch.stairwell_light
        - service: light.turn_off
          target:
            entity_id:
            - light.bar_back_bar_cans
            - light.bar_pendant_light
    - conditions:
        - "{{ trigger.id == 'bar_off1' }}"
        - "{{ states('binary_sensor.bar_main_motion_sensor_motion') == 'off' }}"
      sequence:
        - service: switch.turn_off
          target:
            entity_id:
            - switch.area_cans
            - switch.bar_pool_table_light
            - switch.rope_lights
            - switch.bar_hall_to_media
            - switch.bar_landing
            - switch.stairwell_light
        - service: light.turn_off
          target:
            entity_id:
            - light.bar_back_bar_cans
            - light.bar_pendant_light
    default: []
  mode: single

I don’t use transition as my devices allow that to be configured in the parameters (z-wave)

There could be any number of reasons including your condition, the fact that its in ‘single’ mode with multiple triggers, you could have reloaded (saved) an automation during the 1 minute ‘duration’ in the trigger causing it not to fire, etc…

You really need to look at the diagnostic data to see what happened.

I try not to use devices at all… but it still should work fine with devices…

Thanks a lot AllHailJ, by coding skills are still to little to understand all of your code yet. I need to learn a little bit more about that before. :slight_smile: But still thank you. I will try to rebuild your code with my settings at a point.

Thanks for your answer! Should I set the automation on “restart”?
What exactly does that mean, Rob - you try not to use devices?

No. No one knows your use case. You should look at the diagnostics to see what is happening first.

I prefer to use entitities. I don’t use devices unless I actually need to for some reason.

So I will check the Zigbee2MQTT log in debug level?

Im just saying to look at the automation debug (trace) to start. See what happened or what didnt.

Found my problem: Im using the condition with the lux value with the idea the light only turns on when it’s dark. But that condition aborts the turn off moment, because it’s too bright at that moment by the light itself.

  - condition: numeric_state
    entity_id: sensor.bewegungsmelder_flur_illuminance_lux
    below: '15'

Does anybody know how I could work around that in a smart way?

What is your reason behind that?

It is simplier to address the entities directly. It makes for cleaner yaml code and I have had odd behavior when addressing devices.

You could also use a condition that checks that the light is off in the conditions for the action.

1 Like

You really need a dedicated lux sensor sadly. I tried using the lux in my movement sensors and quite frankly it’s pointless.

They are, at best, not accurate and as you have found don’t always report correctly on movement.

Lux is only reported on movement and not when nothing is going on. So the last time it detected movement it was “bright” and therefore that’s where the lux level will stay. It’s a bit hit and miss. As I say I got around it by having dedicated lux sensors in the rooms where I use this sort of automation.

Personally I always make 2 or more automations on the lights. Triggering on both on and off can lead to strange behavior as in the case, and the code gets much more complicated leading to manual errors.

It looks crowdy in the automation overview, however the overview is not used for anything besides creating the automations.

1 Like

I was checking my trace and there is everything all right. The automation is turning off the light but in reality sometimes the lights don’t turn off. How can I check where is the problem occuring? In the ZigBee2MQTT protocol?

How would you start an error analysis in this situation when automation seems to work right?