本解决方案可以支持多人体重自动记录(前提是每个人有明显的体重区分或阻抗区分),以及完美显示最近一次的的人体体重,体脂信息.
参阅: HA智能硬件采购避坑指要(三) 蓝牙篇
使用的前提硬件,需要至少支持BLE蓝牙4.0(蓝牙是向下兼容的,所以BLE蓝牙5也可)的适配器,一般的HA盒子都有,如果是用PC就看带WIFI不,一般WIFI都带蓝牙,没有就买一个支持的USB WIFI适配器.
请首先安装 Passive BLE Monitor 集成
, 安装方法参见: 使用Passive BLE Monitor集成代替蓝牙网关直接接入HA
如果你已经勾选了Passive BLE Monitor 集成
的"自动发现设备及传感器",那么小米体脂秤2开机后多等一会(保持体脂秤屏幕常亮),就应该看到在该集成(Bluetooth Low Energy Monitor)下多出一个Mi Scale V2
设备,你可以重命名一个有意义的名字.
如果你希望看到详细的体脂信息,那么需要先在HACS第三方集成商场中安装 BodyMiScale 集成.
如果HACS中找不到就加库: 点击 HACS
-> 集成
-> 自定义存储库
(右上角三个点中) -> 存储库设置为 "https://github.com/dckiller51/bodymiscale" , 类别为: 集成
注意,BodyMiScale 集成的最新版本2.1 版本
需要您安装的Home Assistant
是 2022.4
以上版本才能支持,否则您只能安装bodymiscale 2.0
以下版本,切勿升级.
BodyMiScale 集成的最新版本(2.1)已经支持直接在配置界面中配置:
配置
-> 设备与服务
-> 添加集成
-> 搜索 BodyMiScale
- 开始配置
BodyMiScale
- 如果有多人重复上述操作
如果是BodyMiScale@2.0及以下的版本必须在configuration.yaml
手工配置传感器:
bodymiscale:
your_name1:
sensors:
weight: sensor.ble_stabilized_weight_XXXX
impedance: sensor.ble_impedance_XXXX
height: 176
born: "1990-04-10"
gender: "male"
model_miscale: "181B" # miscale2
your_name2:
sensors:
weight: sensor.ble_stabilized_weight_XXXX
impedance: sensor.ble_impedance_XXXX
height: 176
born: "1990-04-10"
gender: "male"
model_miscale: "181D" # miscale1
重启后就会多出来名为: bodymiscale.your_name1
和 bodymiscale.your_name2
的实体.
然后你需要在HACS第三方集成商场中安装lovelace-body-miscale-card 界面, 来使用该实体.
先加库: 点击 HACS
-> 集成
-> 自定义存储库
(右上角三个点中) -> 存储库设置为 https://github.com/dckiller51/lovelace-body-miscale-card
, 类别为: Lovelace
然后再安装该界面. 最后复制仓库中的图片文件(src/images/*
)到config/www/images
目录下.
接着就可以在 lovelace 中进行配置了:
type: custom:body-miscale-card
entity: bodymiscale.your_name1
show_name: true
image: /local/images/miscale2.jpg
show_states: true
show_attributes: true
show_body: true
show_buttons: true
show_toolbar: true
如果有多个人需要分别记录,并且可以通过体重区分的话,当然你也可以加上阻抗值(impedance)来判断,那么可以在configuration.yaml
中:
- platform: template
sensors:
# the first person's weight
weight_your_name:
friendly_name: "Weight Your Name"
value_template: >-
{% set weight = states('sensor.ble_stabilized_weight_XXXX') | float %}
{% if 66.9 <= weight <= 77 %}
{{ states("sensor.ble_stabilized_weight_XXXX") }}
{% else %}
{{ states("sensor.weight_your_name") }}
{% endif %}
unit_of_measurement: 'kg'
icon_template: mdi:weight-kilogram
# the first person's impedance
impedance_your_name:
friendly_name: "Impedance Your Name"
value_template: >-
{% set weight = states('sensor.ble_stabilized_weight_XXXX') | float %}
{% if 66.9 <= weight <= 77 %}
{{ states("sensor.ble_impedance_XXXX") }}
{% else %}
{{ states("sensor.impedance_your_name") }}
{% endif %}
unit_of_measurement: 'ohm'
icon_template: mdimega
问题又来了,如何显示最近的历史数据. 因为当电子秤离线后或者HA重启后,就没当前数据(最近一次的)了!
用SQL Sensor获取的最近一次的历史数据,以及修改前面的模板传感器如下:
- platform: sql
db_url: !secret db_url
queries:
- name: Latest Weight Your Name
query: "SELECT state FROM states WHERE entity_id = 'sensor.weight_your_name' and state <> 'unknown' and state <> 'unavailable' ORDER BY created DESC LIMIT 1;"
column: "state"
- name: Latest Impedance Your Name
query: "SELECT state FROM states WHERE entity_id = 'sensor.impedance_your_name' and state <> 'unknown' and state <> 'unavailable' ORDER BY created DESC LIMIT 1;"
column: "state"
- platform: template
sensors:
weight_your_name:
friendly_name: "Weight Your Name"
value_template: >-
{% set weight = states('sensor.ble_stabilized_weight_XXXX') | float(0) %}
{% set id = 'weight_your_name' %}
{% if 66.9 <= weight <= 77 %}
{{ weight }}
{% elif weight <= 0 %}
{{ state_attr("sensor.latest_"+id, 'state') }}
{% else %}
{{ states("sensor."+id) }}
{% endif %}
unit_of_measurement: 'kg'
icon_template: mdi:weight-kilogram
impedance_your_name:
friendly_name: "Impedance Your Name"
value_template: >-
{% set weight = states('sensor.ble_stabilized_weight_XXXX') | float(0) %}
{% set id = 'impedance_your_name' %}
{% if 66 <= weight <= 77 %}
{{ states("sensor.ble_impedance_XXXX") }}
{% elif weight <= 0 %}
{{ state_attr("sensor.latest_"+id, 'state') }}
{% else %}
{{ states("sensor."+id) }}
{% endif %}
unit_of_measurement: 'ohm'
icon_template: mdimega
注意:
db_url: !secret db_url
要填写自己recorder
数据库的url. 详细请参阅 : SQL Sensor
- 如果recoder 没有添加过
db_url
用的原来的默认配置, 就把这个删掉不要
- YAML 脚本中的 大于(>) 小于(<) 符号 被错误转义了,