HomeAssistant(4) - HelloMorningStar/HomeAssistant GitHub Wiki
嘿嘿!
如果需要显示小米空气净化器的一些数据,例如空气质量,温度,湿度等。需要打开states,去entity找到小米空气净化器fan.kong_qi_jing_hua_qi 这个后面会用到。仔细研究以下例子,今后需要在sensor显示的内容都是这样调用的。
sensor:
- platform: darksky
api_key: dccedf7c6933a6776bce175a14f75ccd
# 监测内容,可选
monitored_conditions:
- summary
- temperature
- apparent_temperature
- platform: template
sensors:
airpurifier_aqi:
friendly_name: 客厅空气质量
value_template: '{{ states.fan.kong_qi_jing_hua_qi.attributes.aqi }}'
unit_of_measurement: 'idx'
airpurifier_temp:
friendly_name: 客厅温度
value_template: '{{ states.fan.kong_qi_jing_hua_qi.attributes.temperature }}'
unit_of_measurement: 'C'
airpurifier_humidity:
friendly_name: 客厅湿度
value_template: '{{ states.fan.kong_qi_jing_hua_qi.attributes.humidity }}'
unit_of_measurement: '%'
automation
安例子配置很简单,但是自动化马上有一个问题要学习,如何对设备进行分组?特别是像小米网关下面的设备,比如switch在configuration.yaml中没有(自动发现的设备),怎么设置分组呢?
按照以下实例:
group:
living_room_lights:
name: 客厅Lights
# view: yes
entities:
- light.gateway_light_286c0788b4c4 #网关灯
- switch.wall_switch_158d00013f83df #走廊灯
- switch.wall_switch_158d00013f845e #水晶灯
- switch.wall_switch_158d00013e6b4f #吊灯
- switch.plug_158d000153de1f #鱼缸灯
living_room_sockets:
name: 客厅Sockets
entities:
- switch.yin_xiang_dian_yuan #音响电源
- switch.shu_zhuo_dian_yuan #书桌电源
living_room_climates:
name: 客厅Climates
entities:
- fan.kong_qi_jing_hua_qi #空气净化器
master_bedroom:
name: 主卧Lights
entities:
- light.tai_deng #台灯
north_balcony_socket:
name: 北阳台Sockets
entities:
- switch.bei_yang_tai_qu_nuan_qi #北阳台取暖器
kitchen_sockets:
name: 厨房Sockets
entities:
- switch.chou_you_yan_ji #油烟机
automation:
alias: Turn on the lights when the sun sets
hide_entity: false
trigger:
platform: sun
event: sunset
# offset: "-01:00:00"
condition:
condition: state
entity_id: group.all_devices
state: 'home'
action:
service: homeassistant.turn_on
entity_id:
- group.living_room_lights
- group.master_bedroom
这里以小米空气净化器为例子(homekit里面只有风扇开关,其它比如模式,温度等均无法显示):
先说说homeassistant控制界面里,通过 xiaomi_miio加入的空气净化器只有开关,其它参数的显示非常不方便,当然这些参数同样也不能够自动显示在homekit里。这里需要用到几个途径来实现:
1)sensor (利用template,自定义sensors并从entity获取值)
废话不说,直接看代码:
sensor:
- platform: template
sensors:
xiaomi_ap_aqi:
friendly_name: 客厅空气质量
value_template: "{{ states.fan.kong_qi_jing_hua_qi.attributes.aqi }}"
unit_of_measurement: 'AQI'
xiaomi_ap_temp:
friendly_name: 客厅温度
value_template: "{{ states.fan.kong_qi_jing_hua_qi.attributes.temperature }}"
unit_of_measurement: '°C'
xiaomi_ap_humid:
friendly_name: 客厅湿度
value_template: "{{ states.fan.kong_qi_jing_hua_qi.attributes.humidity }}"
unit_of_measurement: '%'
这样空气净化器测量环境的空气质量、温度、湿度读数就可以显示在顶部的显示栏目里了。但是这样显示可能不如专门为小米空气净化器定制一个显示面板,集中显示所有希望看到的参数和控制开关,比如三种模式,那么就需要接下来继续。
2)定义输入选项,input_select
input_select:
xiaomi_ap_mode:
name: 模式
icon: mdi:fan
options:
- "自动"
- "睡眠"
- "最爱"
以上可以出现选择面板,但是目前还什么都不是,功能执行还需要再接下来赋予。
3)利用automation赋予具体功能
automation
#读取小米空气净化器三种工作模式的状态,并在控制面板显示当前选择的状态
- alias: xiaomi_ap_get_mode
initial_state: true
hide_entity: true
trigger:
# platform: time_pattern
# seconds: '/5'
- platform: numeric_state
entity_id: fan.kong_qi_jing_hua_qi
value_template: '{{ state.attributes.motor_speed }}'
above: 0
- platform: template
value_template: "{% if is_state('fan.kong_qi_jing_hua_qi', 'on') %}true{% endif %}"
- platform: template
value_template: "{% if is_state('fan.kong_qi_jing_hua_qi', 'off') %}true{% endif %}"
- platform: template
value_template: "{% if is_state_attr('fan.kong_qi_jing_hua_qi', 'mode', 'auto') %}true{% endif %$
- platform: template
value_template: "{% if is_state_attr('fan.kong_qi_jing_hua_qi', 'mode', 'silent') %}true{% endif$
- platform: template
value_template: "{% if is_state_attr('fan.kong_qi_jing_hua_qi', 'mode', 'favorite') %}true{% end$
action:
service: input_select.select_option
data_template:
entity_id: input_select.xiaomi_ap_mode
option: >
{% if states.fan.kong_qi_jing_hua_qi.attributes.mode == "auto" %}自动
{% elif states.fan.kong_qi_jing_hua_qi.attributes.mode == "silent" %}睡眠
{% elif states.fan.kong_qi_jing_hua_qi.attributes.mode == "favorite" %}最爱
{% endif %}
#小米空气净化器三种工作模式选择
- alias: xiaomi_ap_set_mode
initial_state: true
trigger:
platform: state
entity_id: input_select.xiaomi_ap_mode
action:
service: fan.turn_on
data_template:
entity_id: fan.kong_qi_jing_hua_qi
speed: >
{% if states.input_select.xiaomi_ap_mode.state == "自动" %}Auto
{% elif states.input_select.xiaomi_ap_mode.state == "睡眠" %}Silent
{% elif states.input_select.xiaomi_ap_mode.state == "最爱" %}Favorite
{% endif %}
4)最后采用group把以上设置的参数和功能生成一个关于小米空气净化器的面板
living_room_climates:
name: 客厅Climates
view: no
control: hidden
entities:
- fan.kong_qi_jing_hua_qi
- input_select.xiaomi_ap_mode
# - switch.xiaomi_ap_led
# - switch.xiaomi_ap_buzzer
- sensor.xiaomi_ap_aqi
- sensor.xiaomi_ap_temp
- sensor.xiaomi_ap_humid
这里我觉得led和声音的开关没有必要设置,毕竟放在客厅不会影响人,反而有显示和声音更好,如需要设置就在switch里创建这些entities

以上设置空气净化器的温度可以在homekit里显示,但是空气质量和湿度却没有显示。什么原因?我还没有来得及去查询。