Tag Archives: HomeKit

Using Motion Sensor as Occupancy Sensor for HomeKit with Homebridge

Requirements:

  • A HomeKit enabled motion sensor. I use Aqara Motion Sensor Hue Motion Sensor
    • The update interval of Hue Motion Sensor exposes to HomeKit is 10 seconds. Far better than Aqara Motion Sensor (120 seconds). And seems more stable after testing for months.
  • A HomeKit enabled accessory to be controlled
  • Homebridge with homebridge-delay-switch plugin installed

Goals:

  • You want to stick with local automation instead of configuring automations in vendor specific apps
  • Trigger an open/turn on/start event when motion detected
  • When no motion for specific minutes. Trigger another event to close/turn off/stop

Why? This method no longer depends on the buggy HomeKit built-in “Turn Off” and Stops Detecting Motion feature. Common HomeKit motion sensors only update the motion state every [latex]n[/latex] seconds. When Stops Detecting Motion triggers. It won’t be canceled or delayed when it detects another motion during the [latex]n[/latex] detection interval. That causes your accessory to be turned off even motion detects during the time you set.

With my method. Your accessory won’t be turned off if your motion sensor detects motion during the specific time you set. Make your motion sensor work more like an occupancy sensor.

First. Add a delay switch in Homebridge:

{
    "name": "Living Room Delay Switch",
    "delay": 300000,
    "disableSensor": false,
    "startOnReboot": false,
    "accessory": "DelaySwitch"
},

The delay time is in milliseconds so 300000 equals 6 minutes here.

In your Home app. Create the following automations:

First Automation

  • When: Living Room Motion Sensor – Detects Motion
  • Accessories:
    • Turn on your accessory
    • Turn on your Living Room Delay Switch

Second Automation

  • When: Living Room Delay Switch Trigger – Detects Motion
  • Accessories:
    • Turn off your accessory

Third Automation

This automation is important. It can ensure your accessory can still be turned off at a specific time if the accessory was opened/turned on/started manually.

  • When: Living Room Sensor – Stops Detecting Motion
  • Accessories:
    • Turn on your Living Room Delay Switch

Use IKEA TRÅDFRI Driver with HomeKit

You must use IKEA TRÅDFRI Gateway to add your drivers. Philips Hue Bridge can add TRÅDFRI drivers but they won’t appear in your Home app. That means TRÅDFRI drivers with Philips Hue Bridge won’t work with HomeKit. So dig into your pockets to buy a TRÅDFRI Gateway.

After you setting up your TRÅDFRI Gateway with IKEA Home smart app. The app will force you to add a controller (Wireless Dimmer, On-Off Switch/Dimmer, etc.) to control any smart accessory. That prevents you from adding any driver directly to the app. You have to add the driver through a controller. So again, dig deeper into your pockets to buy a switch/dimmer. I recommend TRÅDFRI Wireless dimmer. I just bought one as the device adoptor since I won’t use it to control my accessories.

You can pair the wireless dimmer with more than one drivers. When you push on/off on the dimmer. All paired drivers will respond. It’s okay if you just use it as the device adoptor like me. But if you want to use it later as a normal dimmer. Just remove it from the app and re-pair it the right driver.

Don’t buy any other IKEA smart accessories at the time of writing. Like motion sensors, bulbs, etc. Blinds are okay but reported the motors are louder than other brands. That means most of them suck.

Update Mar 8, 2021: Recently IKEA announced that they add HomeKit support for existing TRÅDFRI Shortcut Buttons and TRÅDFRI Motion Sensors. Recently I purchased these two products and did some tests:

  • Recommended: TRÅDFRI Shortcut Buttons. It responds faster than Aqara Wireless Mini Switch. It has smaller form compared to Hue switches. Zigbee connection is also stable.
  • Not recommended: TRÅDFRI Motion Sensors. The most recent revision can’t change the detection interval. It only detects motion every 180 seconds. Even worse than Aqara Motion Sensor.

Low Latency HomeKit Camera Support: Homebridge FFmpeg Plugin with Custom Codec Configuration

Stream passthrough method for low latency HomeKit camera support:

  • Device: EZVIZ (萤石) CS-C6CN-3H2CWF (星光夜视版)
  • Low latency (as low as 100ms) from the camera to homebridge-camera-ffmpeg
  • Low CPU load since all streams are passthrough from FFmpeg without re-encoding
  • No audio support due to compatibility issue from the source stream

The configuration should be look like:

"videoConfig": {
    "source": "-rtsp_transport tcp -i rtsp://admin:<device_passcode>@<device_ip>:554/h265/ch1/main/av_stream",
    "maxFPS": 30,
    "audio": false,
    "vcodec": "copy",
}

Here’s the tricky part: the EZVIZ CS-C6CN-3H2CWF will output h265 by default, which is not supported by HAP. And the default bitrate is also too high to handle for iOS devices. So you will need to update some settings for the camera:

  • Download the Chinese version of EZVIZ Studio from the official website (direct link).
  • Install and run
  • Click 本地设备 (Local Devices) on the sidebar
  • Right-click the camera you want to modify, then choose 高级设置 (Advanced Settings)
  • In the popup window, click 图像 (Images) – 视音频 (Audio/Video) on the sidebar, change the following settings:
    • 编码类型 (Encoding Type): STD_H264
    • 码率上限 (Bitrate Limit): 1024 Kbps
    • (Optional) 分辨率 (Resolution): HD720P
  • Click 保存 (Save) to save the settings

Please note this step will slightly reduce the quality of your video stream when you view the stream in EZVIZ official app, but it’s mandatory for the camera to work with HomeKit.

Bonus: audio support without compiling FFmpeg after version 1.0.0:

"videoConfig": {
    "source": "-rtsp_transport tcp -i rtsp://admin:<device_passcode>@<device_ip>:554/h265/ch1/main/av_stream",
    "maxFPS": 30,
    "audio": true
}

This will make FFmpeg re-encode your stream and hurt your CPU. The latency is about 0.6-1 second. I suggest you install Homebridge on a Mac mini for better performance.

Update for homebridge-camera-ffmpeg 3.0.0 and later: It’s finally stable enough to stream passthough with audio support. You can now use the following config:

"videoConfig": {
    "source": "-rtsp_transport tcp -i rtsp://admin:<device_passcode>@<device_ip>:554/h265/ch1/main/av_stream",
    "maxFPS": 30,
    "audio": true,
    "vcodec": "copy",
}