How to: IKEA Trådfri Motion Sensor with ZHA

If using zigbee2mqtt, you can use binding to pair the motion sensor with the light.

1 Like

Although the sensor maybe acting as it should, it is not working on my end. Perhaps it’s my automation that is preventing it. My code below have a delay of 3 minutes where the light will shut off. Regardless of what the sensor is detecting, HA will shut off the light at 3 minutes. The lights will not turn back on with any other motions until the icon man is at a stand still. Maybe the code needs to be rewritten where the delay timer needs to be reset with every motion detected during the time the light is on?

- alias: 'hallway light'
  trigger:
    entity_id: binary_sensor.0x588e81fffe11693e_occupancy 
    platform: state
    to: 'on'
  condition:
  - condition: or
    conditions:
      - condition: state
        entity_id: input_boolean.duc_home
        state: 'on'
      - condition: state
        entity_id: input_boolean.eri_home
        state: 'on'
      - condition: state
        entity_id: input_boolean.shion_home
        state: 'on'
  action:
  - service: light.turn_on
    entity_id: light.h_2
    data_template:
      brightness: '{% if now().hour > 20 %}50{% elif now().hour < 8 %}50{% else %}200{% endif %}'
  - delay: 00:03
  - service: light.turn_off
    data:
      entity_id: light.h_2

I noticed your code above I tried to use but I do not know where you get the device_id. I created the code straight from the yaml file and the device_id is created using the ui method?

That is a side-effect from using the binary_sensor as trigger. It will only turn off when there was no occupancy for 3 minutes. Something like this might work better :

- alias: 'hallway light'
  trigger:
  - platform: mqtt
    topic: zigbee2mqtt/0x588e81fffe11693e
  condition:
    - condition: and
      conditions:
      - condition: template
        value_template: "{{ trigger.payload_json.occupancy == true }}"
      - condition: state
        entity_id: light.light.h_2
        state: 'off'
    - condition: or
      conditions:
        - condition: state
          entity_id: input_boolean.duc_home
          state: 'on'
        - condition: state
          entity_id: input_boolean.eri_home
          state: 'on'
        - condition: state
          entity_id: input_boolean.shion_home
          state: 'on'
  action:
  - service: light.turn_on
    entity_id: light.h_2
    data_template:
      brightness: '{% if now().hour > 20 %}50{% elif now().hour < 8 %}50{% else %}200{% endif %}'
  - delay: 00:03
  - service: light.turn_off
    data:
      entity_id: light.h_2

Thank you for the code rewrite. There is a typo where you wrote entity_id: light.light.h_2. I’ve shorten the code a bit and did a fast test. Seems to be working as I want it.

- alias: 'hallway motion on'
  trigger:
  - platform: mqtt
    topic: zigbee2mqtt/0x588e81fffe11693e
  condition:
    - condition: and
      conditions:
      - condition: template
        value_template: '{{ trigger.payload_json.occupancy == true }}'
      - condition: state
        entity_id: light.h_2
        state: 'off'
    - condition: or
      conditions:
        - condition: state
          entity_id:
            - input_boolean.duc_home
            - input_boolean.eri_home
            - input_boolean.shion_home
          state: 'on'
  action:
  - service: light.turn_on
    entity_id: light.h_2
    data_template:
      brightness: '{% if now().hour > 20 %}50{% elif now().hour < 8 %}50{% else %}200{% endif %}'
  - delay: 00:03
  - service: light.turn_off
    data:
      entity_id: light.h_2

No, I don’t have an IKEA Gateway.

I meant any gateway/hub, like deCONZ, Hue bridge,etc.

My Zigbee coordinator (gateway) is a CC2531 featured with an external antenna plugged into a R-Pi 3B+ running Hassio.

After a more thorough test of the script, it isn’t performing as I want it too. When the lights shutoff, it takes awhile for the sensor to detect motion. I am assuming it scans for motion every 30 secs?

I found another script from a different post that works. When the lights shutoff and I happen to walk past it right after it shutoff, the lights turn back on almost instantly. Would be great it the automation can be combined into one instead of 2.

# Hallway Light Sensor
- alias: 'hallway motion on'
  trigger:
  - entity_id: binary_sensor.0x588e81fffe11693e_occupancy
    from: 'off'
    platform: state
    to: 'on'
  condition:
    - condition: or
      conditions:
        - condition: state
          entity_id: input_boolean.duc_home
          state: 'on'
        - condition: state
          entity_id: input_boolean.eri_home
          state: 'on'
        - condition: state
          entity_id: input_boolean.shion_home
          state: 'on'
  action:
  - service: light.turn_on
    entity_id: light.h_2
    data_template:
      brightness: '{% if now().hour > 19 %}50{% elif now().hour < 7 %}50{% else %}200{% endif %}'

- alias: 'hallway motion off'
  trigger:
  - entity_id: binary_sensor.0x588e81fffe11693e_occupancy
    for: 00:00:30
    from: 'on'
    platform: state
    to: 'off'
  action:  
  - service: light.turn_off
    data:
      entity_id: light.h_2

Hi,
I have a couple of these sensors paired with a Conbee 2 stick but am having issues with one of them.

When I restart Homeassistant, the sensor becomes unavailable.
ZHA also loses the device manufactureer and model information (i.e. unk_model by unk_manufacturer). Clicking the reconfigure button does not re-enable the device.

Has anyone else seen a similar issue with these?

You can with Homeassistant 0.113.0

I have the same problem with my xiaomi buttons. All is OK until I restart HA, and we all know how often we need to restart HA.
I found a workaround and posted it here (I use node red):

Thanks for this. I didn’t realise the device is still reporting, generating ZHA Events when they become unavailable in ZHA. I will try this the next time this happens. It is unavoidable with the amount of tweaking I do that needs HA restarts :slight_smile:
It’s great to have ZHA functionality baked into the Core HomeAssistant without needing to install additional addons. But lack of device support and issues like these make it difficult to use reliably.

After several hours of toying with the sensor and study what it is doing or can’t do, this code is the best I can find or come up with.

The lights will stay on as long as the motion sensor is in the on state. As long as you are in the area of the sensor and kinda moving around, the sensor will be in the on state. However, once the sensor detects no motion and switch to the off state, the lights will shut off after 30 sec. If you walk by when it is in the off state, the light will still shut off even though the motion sensor switch back to on.

This will lead to a long delay of turning on the lights because the sensor will start its 3 minute timer. The caveat is that you don’t want to walk by when the sensor has switched to the off state. Than you will need to avoid tripping the motion sensor for a solid 3 minutes.

Anyone found a better solution, please share.

So what you are saying : once the motion sensor has decided it sees no motion, then it will start it’s 3 minute timer ?

Well, not exactly. 3 minute timer starts once it is in the on state. However, if it detects motion between the 3 minutes, the sensor remains in the on state. How much minutes it adds I do not know. Once the sensor detects no motion, it will switch to the off state and the second automation will turn off the light after 30 secs.

I’ve physically test it by staying in the room for over 3 minutes, the light remains on for the duration of the time I was in there. Once I leave, the sensor will switch to off and lights shuts off in 30 secs.

Anyone have a solution - I have a Ikea motion sensor on ZHA - But it will not trigger any motion at all. It always stays no Motion and zha_event dosent give any info for it.

I’m having the same issue with the motion sensors in ZHA, found the device ok, named it correctly, but its not triggering. With a Tasmota flashed Sonoff bridge.

Ikea lights and Sonoff sensors seem to be working ok

Is there some other configuration that needs to be done?

Thanks

EDIT; Deleted and readded, seems to work as it should now. Oh the Local Zigbee!

Does anyone know if the new sensor also send a detection when it’s set on day and it’s night? Since the button only let’s me select day or night and not day AND night.

“Day” must be “day AND night”.
IKEA intend “day” to be used when you need to turn on an associated light also during the day.
In my case I’m using it only for detection and it works like a charm in HA both during day and night.

1 Like

how did you get it working ? I am having the same issue now. Just got the sensor last evening from ikea and it is well paired with Sonoff however doesn’t detect any motion. any help ?