『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 1412|回复: 3

[新奇玩法] 如何让HACS MIOT 集成的小米人体传感器2代更好用。

[复制链接]

9

主题

97

帖子

699

积分

高级会员

Rank: 4

积分
699
金钱
597
HASS币
30
发表于 2022-8-18 15:27:58 | 显示全部楼层 |阅读模式
本帖最后由 neighbour 于 2022-8-18 15:22 编辑

MIOT 可以方便的集成小米的各种设备。 但是二代蓝牙小米人体传感器二代并不怎么好用。因为他的数据格式是介样的
state:
2022-08-18 08:45:00
attr_
model: lumi.motion.bmgl01
2022-08-18 08:45:00: ["000100"]
2022-08-18 08:44:31: ["000100"]
2022-08-18 08:43:39: ["000100"]
2022-08-18 08:42:14: ["000100"]
2022-08-18 08:41:23: ["000100"]
friendly_name: Mi Motion Sensor 2 motion
device_class: motion
所以给他转换成标准的Motion device_class 才会更加好用。
当然我们可以方便的用nr拉一个简单流程建立一个MQTT bin_sensor.来实现这个功能。 这里说的是利用Hass自身组件插件来实现这个功能
1.第一步建立一个空的模版二进制传感器。(YAML格式自理),
template:
  - binary_sensor:
      - name: "cwmotion"
        delay_off:
          seconds: 5
        state: >
        device_class: motion
2.我们需要建立一个自动化脚本监测原人体传感器的状态改变来改变这个模版二进制传感器的状态。但是HASS本身的服务不包含传感器状态改变的功能(我是没找到)。后来在网上搜索终于找到大神写的python脚本。 具体开启方法如下。
Set State
Python Script to set the state or other attributes for the specified entity.
Excellent documentation and HACS installation available from (https://github.com/xannor/hass_py_set_state) readme.
Manual Installation Hints
Create <config>/python_scripts folder if you haven't already.
Copy set_state.py into the <config>/python_scripts folder.
Add python_script: to <config>/configuration.yaml if you haven't already.
Restart Home Assistant.
Set the state or other attributes for the specified entity."""

# ========================================================================================
# python_scripts/set_state.py
# modified from -
# [url]https://community.home-assistant.io/t/how-to-manually-set-state-value-of-sensor/43975/37[/url]
# ========================================================================================

# ----------------------------------------------------------------------------------------
# Set the state or other attributes for the specified entity.
# Updates from @xannor so that a new entity can be created if it does not exist.
# ----------------------------------------------------------------------------------------

inputEntity = data.get("entity_id")
if inputEntity is None:
    logger.warning("===== entity_id is required if you want to set something.")
else:
    inputStateObject = hass.states.get(inputEntity)
    if inputStateObject is None and not data.get("allow_create"):
        logger.warning("===== unknown entity_id: %s", inputEntity)
    else:
        if not inputStateObject is None:
            inputState = inputStateObject.state
            inputAttributesObject = inputStateObject.attributes.copy()
        else:
            inputAttributesObject = {}

        for item in data:
            newAttribute = data.get(item)
            logger.debug("===== item = {0}; value = {1}".format(item, newAttribute))
            if item == "entity_id":
                continue  # already handled
            elif item == "allow_create":
                continue  # already handled
            elif item == "state":
                inputState = newAttribute
            else:
                inputAttributesObject[item] = newAttribute

        hass.states.set(inputEntity, inputState, inputAttributesObject)[/size]
https://github.com/rodpayne/home-assistant/blob/main/.homeassistant/python_scripts/set_state.py

3.最后就是建立一个自动化脚本监测人体传感状态的改变而更改那个模版传感器的状态,并延时复位。因为自定义脚本不支持图形化配置,所以需要用yaml方式进行编辑
alias: 移动传感器状态改变
description: ''
trigger:
  - platform: state
    entity_id:
      - sensor.bmgl01_event_rge000_motion
condition: []
action:
  - service: python_script.set_state
    data_template:
      entity_id: binary_sensor.cwmotion
      state: 'on'
  - delay:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - service: python_script.set_state
    data_template:
      entity_id: binary_sensor.cwmotion
      state: 'off'
mode: single

经过这番配置,我们就得到了标准的motion传感器信息了。


回复

使用道具 举报

6

主题

81

帖子

492

积分

中级会员

Rank: 3Rank: 3

积分
492
金钱
411
HASS币
10
发表于 2022-8-19 00:20:03 | 显示全部楼层
小米人体感应器,可以脱离网关接入吗
回复

使用道具 举报

9

主题

97

帖子

699

积分

高级会员

Rank: 4

积分
699
金钱
597
HASS币
30
 楼主| 发表于 2022-8-19 10:10:11 | 显示全部楼层
wmhack 发表于 2022-8-19 00:20
小米人体感应器,可以脱离网关接入吗

前阵子看到一个蓝牙插件。 但是hassos 虚机 蓝牙驱动不灵。 就没试过。
回复

使用道具 举报

6

主题

81

帖子

492

积分

中级会员

Rank: 3Rank: 3

积分
492
金钱
411
HASS币
10
发表于 2022-8-19 21:49:24 | 显示全部楼层
neighbour 发表于 2022-8-19 10:10
前阵子看到一个蓝牙插件。 但是hassos 虚机 蓝牙驱动不灵。 就没试过。

我最近就是买了一个USB蓝牙4.0,等回来了就试试额
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-5-7 23:04 , Processed in 0.233978 second(s), 25 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表