Hue Motion Sensor Automation does not work with ZHA

Hi, im getting some strange issues while connecting a Hue Motion Sensor to ZHA.
I had an automation where lights should go on when motion is detected. After 20 seconds the light should turn brightness to 25% and after 40 seconds the light should turn off.
Within the time any motion would trigger the light to go up to 100% again.

It worked perfect when the motion sensor was connected to the hue bridge but now the lights wont turn off after dimming to 25%. My problem with hue bridge was the delay to turn on the lights.

Now the lights are turned on instantly but the only things that work are lights on and turning brightness to 25% after this the lights wont go to 100% again and the lights don’t turn off.
It stays at 25% brightness all the time.

Whats wrong with this?

This is my code:

- id: '1630145925001'
  alias: Flur GUI
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_flur_presenz
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: binary_sensor.motion_flur_presenz
    from: 'on'
    to: 'off'
    for: 00:00:20
  - platform: state
    entity_id: binary_sensor.motion_flur_presenz
    from: 'on'
    to: 'off'
    for: 00:00:40
  condition: []
  action:
  - choose:
    - conditions:
      - condition: state
        entity_id: binary_sensor.motion_flur_presenz
        state: 'on'
      - condition: state
        entity_id: light.extended_color_light_1_2
        state: 'off'
      sequence:
      - service: light.turn_on
        target:
          entity_id: light.extended_color_light_1_2
        data:
          brightness_pct: 100
    - conditions:
      - condition: state
        entity_id: binary_sensor.motion_flur_presenz
        state: 'off'
        for: 00:00:20
      - condition: state
        entity_id: light.extended_color_light_1_2
        state: 'on'
      sequence:
      - service: light.turn_on
        target:
          entity_id: light.extended_color_light_1_2
        data:
          brightness_pct: 25
    - conditions:
      - condition: state
        entity_id: binary_sensor.motion_flur_presenz
        state: 'off'
        for: 00:00:40
      - condition: state
        entity_id: light.extended_color_light_1_2
        state: 'on'
      sequence:
      - service: light.turn_off
        target:
          entity_id: light.extended_color_light_1_2
    default: []
  mode: restart

You said this automation worked perfectly when used with the Hue Bridge but now misbehaves with ZHA. Was anything changed in the automation? For example, has it always used mode: restart?

If nothing has changed in the automation, then the behavior of the binary_sensor might now be different. Have you checked the automation’s trace to understand what happens each time it is triggered?

I changed the entities or the entity names in the automation because the sensor has a different name from ZHA.
It was restart before and worked like a charm.
But now it doesn’t trigger anything else than 25%.

How to check the trace?

Go to Configuration > Automations, find the Flur GUI automation, and click this icon:

Screenshot from 2021-08-29 11-08-35

I created a new version of your automation that reports what it does via a persistent notification. I tested it using a WiFi-based motion sensor that has a 10-second cooldown period. That means it will report off (“no motion”) after it detects no motion for 10 consecutive seconds. You should check what is the cooldown period for the Hue Motion Sensor when used with ZHA.

Here are the three notifications it produced:

  1. At 12:35:01 it detected motion and the light was turned on to 100% brightness.
  2. At 12:35:31, which represents the 10-second cooldown period plus 20 seconds (from the for: 00:00:20), it dimmed the light to 25%.
  3. At 13:35:51, which represents the 10-second cooldown period plus 40 seconds (from the for: 00:00:40), it turned off the light.

Here is the automation I used. You might want to try it and see the notifications it produces.

- id: '1630145925001'
  alias: Flur GUI
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_flur_presenz
    from: 'off'
    to: 'on'
    id: 'on'
  - platform: state
    entity_id: binary_sensor.motion_flur_presenz
    from: 'on'
    to: 'off'
    for: 00:00:20
    id: 'off_20'
  - platform: state
    entity_id: binary_sensor.motion_flur_presenz
    from: 'on'
    to: 'off'
    for: 00:00:40
    id: 'off_40'
  action:
  - variables:
      entity: "light.extended_color_light_1_2"
      light_on: "{{ is_state(entity, 'on') }}"
  - choose:
    - conditions: "{{ trigger.id == 'on' and not light_on }}"
      sequence:
      - service: light.turn_on
        target:
          entity_id: "{{ entity }}"
        data:
          brightness_pct: 100
      - service: notify.persistent_notification
        data:
          title: "Light 100"
          message: "{{ now().timestamp() | timestamp_local }}"
    - conditions: "{{ trigger.id == 'off_20' and light_on }}"
      sequence:
      - service: light.turn_on
        target:
          entity_id: "{{ entity }}"
        data:
          brightness_pct: 25
      - service: notify.persistent_notification
        data:
          title: "Light 25"
          message: "{{ now().timestamp() | timestamp_local }}"
    - conditions: "{{ trigger.id == 'off_40' and light_on }}"
      sequence:
      - service: light.turn_off
        target:
          entity_id: "{{ entity }}"
      - service: notify.persistent_notification
        data:
          title: "Light 0"
          message: "{{ now().timestamp() | timestamp_local }}"
    default: []
  mode: restart
1 Like

Thanks for your effort.
I want to try this but now i’m struggeling with the motion sensor itself.
It always shows up as on so it always detects motion even after a restart.
When i set the state to off it automatically comes back as on and stays like this forever.

I’m not sure if this is a problem for my automation (it wasn’t like this when i opened this thread) or if it has to do with the single automation.
It split the automation into 3 single automations to set it to 100% (first automation), to 25% (second automation) and turn it off (3rd automation). They also work with timers when the state of the motion sensor is 20 or 40 seconds off.

So i’m trying to find out why the state is always in and then the automation comes back to my focus.
If you have any tip to get the sensor running i will appreciate it.

I agree that it doesn’t appear to be working correctly. No automation will work as expected if the motion sensor behaves like that.

The motion sensor’s behavior you described is abnormal and independent of any automation you are using. You will need to sort out its operation because its unusual behavior appears to have been introduced by the change to the ZHA integration.

i think i fixed it by moving the sensor by a centimeter…
strange behaviour but now it goes on and off again. so now i will look into the automation.

i will reply after testing

Your code works perfect.
Just for understandig: it all depends on the trigger id to work?
Did my code not work because it had stopped after the first 20 seconds?

I don’t think your code was wrong. I think the problems you were experiencing had to do with the motion sensor’s odd behavior.

The automation I posted is essentially the same as the one you created however, each conditions is simplified because it relies on trigger.id which tells it which trigger was responsible for triggering the automation (it doesn’t have to re-check everything to determine which one triggered). It’s a bit more efficient to use trigger.id in this particular case.

Here’s the same automation without the notifications (even more compact):

- id: '1630145925001'
  alias: Flur GUI
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_flur_presenz
    from: 'off'
    to: 'on'
    id: 'on'
  - platform: state
    entity_id: binary_sensor.motion_flur_presenz
    from: 'on'
    to: 'off'
    for: 00:00:20
    id: 'off_20'
  - platform: state
    entity_id: binary_sensor.motion_flur_presenz
    from: 'on'
    to: 'off'
    for: 00:00:40
    id: 'off_40'
  action:
  - variables:
      entity: "light.extended_color_light_1_2"
      light_on: "{{ is_state(entity, 'on') }}"
  - choose:
    - conditions: "{{ trigger.id == 'on' and not light_on }}"
      sequence:
      - service: light.turn_on
        target:
          entity_id: "{{ entity }}"
        data:
          brightness_pct: 100
    - conditions: "{{ trigger.id == 'off_20' and light_on }}"
      sequence:
      - service: light.turn_on
        target:
          entity_id: "{{ entity }}"
        data:
          brightness_pct: 25
    - conditions: "{{ trigger.id == 'off_40' and light_on }}"
      sequence:
      - service: light.turn_off
        target:
          entity_id: "{{ entity }}"
    default: []
  mode: restart

Yes, i alreadys took the notifications out.
Works perfect. Thank you very much!

You’re welcome!

If you found the automation I posted was useful to resolve the issue, please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. It will also place a link below your first post that leads to the solution. All of this helps users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.

Sorry for delay but i don’t saw your last answer. I just wanted to mark your answer as a solution but there is no button for this. Do you know why?

Do you see this displayed at the bottom of each post?

Screenshot_20210910-101259~2

  • If you do, select the first one on the left (in my post containing the solution).
  • If you don’t then I don’t know why it’s not visible (and there’s nothing else to do).

No sorry. Everything is there but not the solution button.

Normally any topic categorized under Configuration will get the ability to be marked with the Solution tag. However, I noticed that none of the topics categorized under ZHA are marked with the Solution tag.

This problem wasn’t directly related to ZHA so you can edit your first post and remove the ZHA category, leaving it with just the Configuration category.

Refresh your browser page and see if that helps to reveal the Solution checkbox. If it doesn’t then there’s not much else we can do to influence the forum’s software.