How to: IKEA Trådfri Motion Sensor with ZHA

I haven’t found good information on how to use the IKEA motion sensor with HA and ZHA (I have an Elelabs Zigbee USB Adapter).
When I pair the sensor to HA, it shows correctly as sensor + battery, but no movement is detected.
I suspect the motion sensor has to be paired with an IKEA light.
Can anybody help?

You have to listen to events :

- id: '1572890752657'
  alias: Licht buiten aan
  description: ''
  trigger:
  - device_id: 85362a5cfac5442da8d9fb07ce9ffdb1
    domain: binary_sensor
    entity_id: binary_sensor.0xccccccfffe2fb287_occupancy
    platform: device
    type: motion
  condition:
  - condition: state
    entity_id: light.lichtbuitenvoordeur
    state: 'off'
  - condition: state
    entity_id: sun.sun
    state: below_horizon  
  action:
  - data:
      entity_id: light.lichtbuitenvoordeur
    service: light.turn_on

I am listening to any state event (see below): can you elaborate?
Is the “platform: device” / “type: motion” doing the trick on your configuration?

- id: TestMotionSensors
  alias: Test Motion Sensors
  initial_state: true
  hide_entity: false
  trigger:
    - platform: state
      entity_id: binary_sensor.ikea_of_sweden_tradfri_motion_sensor_fe358565_1_6
    - platform: state
      entity_id: binary_sensor.ikea_of_sweden_tradfri_motion_sensor_fe463756_1_6
  action:
    - service: notify.hasstest
      data_template:
        title: "[HA] MOTION"
        message: "{{ trigger.from_state.state }}=>{{ trigger.to_state.state }}\n{{now().strftime('%Y:%m:%d - %H:%M:%S')}}"
        target: "channel/mychannel"


Please note that I see no leds turning on in front of the sensor, a part when pairing to HA

I’m using add-on zigbee2mqtt. I do not need to pair any lamp to the IKEA motion sensor. I can see the changes on the device state (occupancy) when motion occurs. In fact, I’m not using the IKEA motion sensor to turn lamps on but to detect presence.

If we are using zigbee2mqtt to trigger the sensor to have lights turn on, the default 3 minute timer built-in the sensor is no longer valid? That is the light will not turn off after 3 minutes when trigger now?

It will still send a message ‘occupancy false’ if there was no motion after 3 minutes, but if you don’t have an automation in HA to turn off the light on that message, the light will not turn off.

1 Like

True when light and sensor are not paired.
I turn off lights with an automation when no movemend is detected by the sensor anymore.

ps
seems the shortest interval I got is ~ 2 minutes

Hmmm. Ok. So, if we have HA to turn off the lights say at 2:30. If someone would walk pass the sensor right after, the light will not turn on since it still reads true ?

Did not try this one, but I think so. Although, it is possible it will send a new ‘occupance true’

No. Just tried it out. If you pass by again you get a new ‘occupancy true’

image

But pairing directly with the lamp has the benefit that it always works, even if your gateway is down. Or you don’t even have a gateway.

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?