Tag: Homebridge

  • 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
  • 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",
    }