本帖最后由 dscao 于 2023-3-5 17:10 编辑
2023年3月4日更新:
经过论坛帖子的学习,今天突然想到仅用模板就可以解决这个门锁状态的问题。新版的template有trigger可以用了,如果再有环境条件就可以更简化了。
现在的gateway3集成的小米蓝牙门锁的实体已经有 binary_sensor.xxxxxxx_contact 门的开关状态了,大部分自动化也够用了。
再增加“门内开锁,指纹开锁”等信息就可以了。
sensor.e50b9a2d2a9f_action 换成自己门锁的 action实体ID即可。重启ha不影响各模板实体状态。
代码也可以放在packages目录下的一个yaml文件中:
template:
- trigger:
- platform: state
entity_id:
- sensor.e50b9a2d2a9f_action
attribute: message
to:
- 'Unlock outside the door'
- 'Lock'
- 'Turn on anti-lock'
- 'Turn off anti-lock'
- 'Unlock inside the door'
- 'Lock inside the door'
- 'Turn on child lock'
- 'Turn off child lock'
- 'Knock on the door'
- 'Breaking the door'
- 'Door is stuck'
not_from:
- unknown
- unavailable
sensor:
name: xiaomi_smartlock_state
unique_id: xiaomi_smartlock_state
state: |
{% if is_state_attr('sensor.e50b9a2d2a9f_action','action_id',0) and is_state_attr('sensor.e50b9a2d2a9f_action','action',"lock") %}
门外开锁
{% elif is_state_attr('sensor.e50b9a2d2a9f_action','action_id',1) and is_state_attr('sensor.e50b9a2d2a9f_action','action',"lock") %}
上提把手上锁
{% elif is_state_attr('sensor.e50b9a2d2a9f_action','action_id',2) and is_state_attr('sensor.e50b9a2d2a9f_action','action',"lock") %}
反锁
{% elif is_state_attr('sensor.e50b9a2d2a9f_action','action_id',3) and is_state_attr('sensor.e50b9a2d2a9f_action','action',"lock") %}
解除反锁
{% elif is_state_attr('sensor.e50b9a2d2a9f_action','action_id',4) and is_state_attr('sensor.e50b9a2d2a9f_action','action',"lock") %}
门内开锁
{% elif is_state_attr('sensor.e50b9a2d2a9f_action','action_id',5) and is_state_attr('sensor.e50b9a2d2a9f_action','action',"lock") %}
门内上锁
{% elif is_state_attr('sensor.e50b9a2d2a9f_action','action_id',6) and is_state_attr('sensor.e50b9a2d2a9f_action','action',"lock") %}
开启童锁
{% elif is_state_attr('sensor.e50b9a2d2a9f_action','action_id',7) and is_state_attr('sensor.e50b9a2d2a9f_action','action',"lock") %}
关闭童锁
{% elif is_state_attr('sensor.e50b9a2d2a9f_action','action_id',0) and is_state_attr('sensor.e50b9a2d2a9f_action','action',"door") %}
开门
{% elif is_state_attr('sensor.e50b9a2d2a9f_action','action_id',1) and is_state_attr('sensor.e50b9a2d2a9f_action','action',"door") %}
关门
{% elif is_state_attr('sensor.e50b9a2d2a9f_action','action_id',2) and is_state_attr('sensor.e50b9a2d2a9f_action','action',"door") %}
超时未关
{% elif is_state_attr('sensor.e50b9a2d2a9f_action','action_id',3) and is_state_attr('sensor.e50b9a2d2a9f_action','action',"door") %}
敲门
{% elif is_state_attr('sensor.e50b9a2d2a9f_action','action_id',4) and is_state_attr('sensor.e50b9a2d2a9f_action','action',"door") %}
门被破坏
{% elif is_state_attr('sensor.e50b9a2d2a9f_action','action_id',5) and is_state_attr('sensor.e50b9a2d2a9f_action','action',"door") %}
门卡住了
{% else %}
action_id:{{ state_attr('sensor.e50b9a2d2a9f_action','action_id') }}
{% endif %}
- trigger:
- platform: state
entity_id:
- sensor.e50b9a2d2a9f_action
attribute: method_id
to:
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 10
- 11
not_from:
- unknown
- unavailable
- platform: state
entity_id:
- sensor.e50b9a2d2a9f_action
to: lock
not_from:
- unknown
- unavailable
sensor:
name: xiaomi_smartlock_open_method
unique_id: xiaomi_smartlock_open_method
state: |
{% if state_attr('sensor.e50b9a2d2a9f_action','method_id') == 0 %}
蓝牙
{% elif state_attr('sensor.e50b9a2d2a9f_action','method_id') == 1 %}
密码
{% elif state_attr('sensor.e50b9a2d2a9f_action','method_id') == 2 %}
指纹
{% elif state_attr('sensor.e50b9a2d2a9f_action','method_id') == 3 %}
钥匙
{% elif state_attr('sensor.e50b9a2d2a9f_action','method_id') == 4 %}
转盘
{% elif state_attr('sensor.e50b9a2d2a9f_action','method_id') == 5 %}
NFC
{% elif state_attr('sensor.e50b9a2d2a9f_action','method_id') == 6 %}
一次性密码
{% elif state_attr('sensor.e50b9a2d2a9f_action','method_id') == 7 %}
双重验证
{% elif state_attr('sensor.e50b9a2d2a9f_action','method_id') == 8 %}
强制
{% elif state_attr('sensor.e50b9a2d2a9f_action','method_id') == 10 %}
人工
{% elif state_attr('sensor.e50b9a2d2a9f_action','method_id') == 11 %}
自动
{% else %}
method_id:{{ state_attr('sensor.e50b9a2d2a9f_action','method_id') }}
{% endif %}
- trigger:
- platform: state
entity_id:
- sensor.e50b9a2d2a9f_action
attribute: key_id
to:
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 'ffffffff'
- 'DEADBEEF'
- 0xc0de0000
- 0xc0de0001
- 0xc0de0002
- 0xc0de0003
- 0xc0de0004
- 0xc0de0005
- 0xc0de0006
- 0xc0de0007
- 0xc0de0008
- 0xc0de0009
- 0xc0de000a
- 0xc0de000b
- 0xc0de000c
- 0xc0de000d
- 0xc0de000e
- 0xc0de000f
- 0xc0de0010
- 0xc0de1000
- 0xc0de1001
- 0xc0de1002
- 0xc0de1003
- 0xc0de1004
sensor:
name: xiaomi_smartlock_open_key
unique_id: xiaomi_smartlock_open_key
state: |
{% if state_attr('sensor.e50b9a2d2a9f_action','key_id') == 0 %}
管理员
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 2 %}
我的指纹
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 6 %}
我的右手指纹
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 1 %}
我的密码
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 3 %}
老爸的指纹
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 5 %}
老婆的指纹
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 7 %}
老婆的右手指纹
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 'ffffffff' %}
未知操作者
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 'DEADBEEF' %}
无效操作者
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 0xc0de0000 %}
错误的密码解锁次数过多
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 0xc0de0001 %}
错误指纹解锁次数过多
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 0xc0de0002 %}
操作超时(密码输入超时)
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 0xc0de0003 %}
开锁
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 0xc0de0004 %}
重置按钮被按下
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 0xc0de0005 %}
钥匙弄错次数过多
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 0xc0de0006 %}
钥匙孔内有异物
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 0xc0de0007 %}
钥匙未取出
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 0xc0de0008 %}
错误的NFC频繁解锁
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 0xc0de0009 %}
超时未按要求锁定
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 0xc0de000a %}
多种方式频繁解锁失败
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 0xc0de000b %}
人脸解锁失败次数过多
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 0xc0de000c %}
静脉解锁失败次数过多
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 0xc0de000d %}
劫持报警
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 0xc0de000e %}
布防后在门内解锁
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 0xc0de000f %}
掌纹解锁失败次数过多
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 0xc0de0010 %}
保险被移除
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 0xc0de1000 %}
电池电量低于 10%
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 0xc0de1001 %}
电池电量低于 5%
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 0xc0de1002 %}
指纹传感器异常
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 0xc0de1003 %}
附件电池电量低
{% elif state_attr('sensor.e50b9a2d2a9f_action','key_id') == 0xc0de1004 %}
机械故障
{% else %}
{{ state_attr('sensor.e50b9a2d2a9f_action','key_id') }}
{% endif %}
- trigger:
- platform: state
entity_id: sensor.e50b9a2d2a9f_action
sensor:
name: xiaomi_smartlock_do_time
unique_id: xiaomi_smartlock_do_time
state: >
{{ as_local(states.sensor.e50b9a2d2a9f_action.last_changed).strftime('%Y-%m-%d %H:%M:%S') }}
复制代码
大大简化之前的方法,看起来效果几乎一样。
另非常感谢 louis_lee 大佬 在 76楼的分享。
-------上面代码更新后,以下部分已弃用-(非参考思路的话可以不看了)----
-----------------2021-10-10 更新----------------------
XiaomiGateway3 v1.5.1-rc.1已经修复action的bug。
-----------------2021-9-30 更新----------------------
多模网关插件 XiaomiGateway3 1.5.0被作者标记了一处Bug,https://github.com/AlexxIT/XiaomiGateway3/issues/469。
暂时可将代码中的锁动作显示中文的触发条件的到状态值由 lock 改为 key_id。
或者手动修改插件1.5.0中的代码,sensor.py 中324行:
将 elif k in ('key_id', 'lock_control', 'lock_state'):
改为 elif k in ('lock_control', 'lock_state'):
复制代码
估计下一版作者应该会修复吧,修复后按原来的就可以了。
-----------------以下部分由 dscao 于 2021-1-31 01:48 编辑 -----------------------
看到有坛友也提出这个思路或问题,我把我现在用的方法也分享一下,不过也还有一些不足的地方,希望也能找到更好的方案。
多模网关中门锁中门状态和锁状态共用一个 sensor.XXXXXX_action ,导致用新建模板sensor,会出现有时一个为空。
所以利用自动化判断action来设定门的状态和锁的状态及其它相关的状态。
首先要安装 python_script,在ha的config目录中加入 python_scripts/set_state.py
内容为:
if 'entity_id' not in data:
logger.warning("===== entity_id is required if you want to set something.")
else:
data = data.copy()
inputEntity = data.pop('entity_id')
inputStateObject = hass.states.get(inputEntity)
if inputStateObject:
inputState = inputStateObject.state
inputAttributesObject = inputStateObject.attributes.copy()
else:
inputState = 'unknown'
inputAttributesObject = {}
if 'state' in data:
inputState = data.pop('state')
logger.debug("===== new attrs: {}".format(data))
inputAttributesObject.update(data)
hass.states.set(inputEntity, inputState, inputAttributesObject)
复制代码
在 configuration.yaml 加入一句:
python_script:
启用这个。
2021年1月31日更新,门状态通过模板,因为只有开和关两种状态,可以不需要自动化来更新了,但锁状态还是要通过自动化来更新。:
设置几个模板Sensor,可以在packages目录下的yaml文件中:
binary_sensor:
- platform: template
sensors:
smartlock_door:
friendly_name: 门状态
value_template: >-
{% if (is_state_attr('sensor.e50b9a2dxxxx_action','action_id',0) and is_state_attr('sensor.e50b9a2dxxxx_action','action',"door")) or (is_state_attr('sensor.e50b9a2dxxxx_action','action_id',2) and is_state_attr('sensor.e50b9a2dxxxx_action','action',"door"))%}
on
{% else %}
off
{% endif %}
icon_template: >-
{% if (is_state_attr('sensor.e50b9a2dxxxx_action','action_id',0) and is_state_attr('sensor.e50b9a2dxxxx_action','action',"door")) or (is_state_attr('sensor.e50b9a2dxxxx_action','action_id',2) and is_state_attr('sensor.e50b9a2dxxxx_action','action',"door"))%}
mdi:door-open
{% else %}
mdi:door-closed-lock
{% endif %}
sensor:
- platform: template
sensors:
smartlock_state:
friendly_name: 门锁状态
value_template: 未知
smartlock_method:
friendly_name: 开门方式
value_template: 未知
smartlock_user:
friendly_name: 操作者
value_template: 未知
smartlock_changetime:
friendly_name: 操作时间
value_template: >-
{% if as_timestamp(states.sensor.e50b9a2dxxxx_action.last_updated) > as_timestamp(states.sensor.xi_tong_qi_dong_shi_jian.state) %}
{{ as_local(states.sensor.e50b9a2dxxxx_action.last_changed).strftime('%Y-%m-%d %H:%M:%S') }}
{% else %}
无
{% endif %}
homeassistant:
customize:
sensor.smartlock_state:
icon: mdi:lock-smart
sensor.smartlock_method:
icon: mdi:lock-question
sensor.smartlock_user:
icon: mdi:human-handsdown
sensor.smartlock_changetime:
icon: mdi:lock-clock
复制代码
一个自动化改变锁的状态,门的状态上面通过模板可自动更新:
alias: 锁动作显示中文
description: ''
trigger:
- platform: state
entity_id: sensor.e50b9a2dxxxx_action
to: lock
condition: []
action:
- service: python_script.set_state
data:
entity_id: sensor.smartlock_method
state: |
{% if trigger.to_state.attributes['method_id'] == 0 %}
蓝牙
{% elif trigger.to_state.attributes['method_id'] == 1 %}
密码
{% elif trigger.to_state.attributes['method_id'] == 2 %}
指纹
{% elif trigger.to_state.attributes['method_id'] == 3 %}
钥匙
{% elif trigger.to_state.attributes['method_id'] == 4 %}
转盘
{% elif trigger.to_state.attributes['method_id'] == 5 %}
NFC
{% elif trigger.to_state.attributes['method_id'] == 6 %}
一次性密码
{% elif trigger.to_state.attributes['method_id'] == 7 %}
双重验证
{% elif trigger.to_state.attributes['method_id'] == 10 %}
人工
{% elif trigger.to_state.attributes['method_id'] == 11 %}
自动
{% else %}
method_id:{{ trigger.to_state.attributes['method_id'] }}
{% endif %}
- service: python_script.set_state
data:
entity_id: sensor.smartlock_state
state: |
{% if is_state_attr('sensor.e50b9a2dxxxx_action','action_id',0) %}
门外开锁
{% elif is_state_attr('sensor.e50b9a2dxxxxx_action','action_id',1) %}
上提把手上锁
{% elif is_state_attr('sensor.e50b9a2dxxxx_action','action_id',2) %}
反锁
{% elif is_state_attr('sensor.e50b9a2dxxxx_action','action_id',3) %}
解除锁
{% elif is_state_attr('sensor.e50b9a2dxxxx_action','action_id',4) %}
门内开锁
{% elif is_state_attr('sensor.e50b9a2dxxxx_action','action_id',5) %}
门内上锁
{% elif is_state_attr('sensor.e50b9a2dxxxx_action','action_id',6) %}
开启童锁
{% elif is_state_attr('sensor.e50b9a2dxxxx_action','action_id',7) %}
关闭童锁
{% else %}
action_id:{{ state_attr('sensor.e50b9a2dxxxx_action','action_id') }}
{% endif %}
- service: python_script.set_state
data:
entity_id: sensor.smartlock_user
state: |
{% if trigger.to_state.attributes['key_id'] == 0 %}
管理员
{% elif trigger.to_state.attributes['key_id'] == 2 %}
我的指纹
{% elif trigger.to_state.attributes['key_id'] == 6 %}
我的右手指纹
{% elif trigger.to_state.attributes['key_id'] == 1 %}
我的密码
{% elif trigger.to_state.attributes['key_id'] == 3 %}
老爸的指纹
{% elif trigger.to_state.attributes['key_id'] == 5 %}
老婆的指纹
{% elif trigger.to_state.attributes['key_id'] == 7 %}
老婆的右手指纹
{% elif trigger.to_state.attributes['key_id'] == 'ffffffff' %}
未知操作者
{% elif trigger.to_state.attributes['key_id'] == 'DEADBEEF' %}
无效操作者
{% else %}
{{ trigger.to_state.attributes['key_id'] }}
{% endif %}
mode: single
复制代码
效果:
评分
查看全部评分