Hello, I have 3 days experience in Home Assistant and this was my hardest yet device to integrate, so I want to give back to the community as there’s much confusion in this subject. I’m not a programmer I just built it by copy and paste from the forum.
Do not use the visual interface to integrate the Air Purifier! (I have done this and it managed to mess up the name of the “entity” and the configuration had to change and some other things, lost a lot of time with this)
Use this integration to get the Token directly in Home Assistant without any other android or IOS method.
- add this configuration to configuration.yaml
fan:
- platform: xiaomi_miio
host: IP OF THE Air Purifier
token: YOUR TOKEN
name: "WHAT NAME YOU WANT FOR THE DEVICE"
Check config using the interface and restart Home Assistant
2. After restart check in the “Entities” section for your Home assistant for an Fan entity, note the “Entity ID” you can check if is working by clicking on it and pushing the icon on the top-right corner.
3. add configuration to “configuration.yaml” and automations to automations.yaml REPLACE the “Entity ID” in all the code, if you have different entity ID. In my example is “fan.xiaomi_air_purifier_pro”
configuration.yaml
sensor:
# Xiaomi Air Purifier PRO
- platform: template
sensors:
xiaomi_airpurifier_temp:
friendly_name: "Air Purifier Temperature"
value_template: "{{ state_attr('fan.xiaomi_air_purifier_pro', 'temperature') }}"
unit_of_measurement: "°C"
device_class: "temperature"
icon_template: "mdiThermometerLines"
xiaomi_airpurifier_humidity:
friendly_name: "Air Purifier Humidity"
value_template: "{{ state_attr('fan.xiaomi_air_purifier_pro', 'humidity') }}"
unit_of_measurement: "%"
device_class: "humidity"
icon_template: "mdiWaterPercent"
xiaomi_airpurifier_air_quality_pm25:
friendly_name: "Air Purifier Air quality"
value_template: "{{ state_attr('fan.xiaomi_air_purifier_pro', 'aqi') }}"
unit_of_measurement: "μg/m³"
icon_template: "mdi:weather-fog"
xiaomi_airpurifier_speed:
friendly_name: "Air Purifier Fan Speed"
value_template: "{{ state_attr('fan.xiaomi_air_purifier_pro', 'motor2_speed') }}"
unit_of_measurement: "rpm"
icon_template: "mdi:speedometer"
xiaomi_airpurifier_filter_remaining:
friendly_name: "Air Purifier Filter Remaining"
value_template: "{{ state_attr('fan.xiaomi_air_purifier_pro', 'filter_life_remaining') }}"
unit_of_measurement: "%"
icon_template: "mdiAirFilter"
xiaomi_airpurifier_luminance:
friendly_name: "Air Purifier Luminance"
value_template: "{{ state_attr('fan.xiaomi_air_purifier_pro', 'illuminance') }}"
unit_of_measurement: "Lux"
icon_template: "mdiLightbulbOutline"
switch:
# Xiaomi Air Purifier PRO
- platform: template
switches:
xiaomi_airpurifier_led:
friendly_name: "Air Purifier Display"
value_template: "{{ is_state_attr('fan.xiaomi_air_purifier_pro', 'led', true) }}"
turn_on:
service: xiaomi_miio.fan_set_led_on
data:
entity_id: fan.xiaomi_air_purifier_pro
turn_off:
service: xiaomi_miio.fan_set_led_off
data:
entity_id: fan.xiaomi_air_purifier_pro
icon_template: "mdi:lightbulb-outline"
xiaomi_airpurifier_child_lock:
friendly_name: "Air Purifier Child lock"
value_template: "{{ is_state_attr('fan.xiaomi_air_purifier_pro', 'child_lock', true) }}"
turn_on:
service: xiaomi_miio.fan_set_child_lock_on
data:
entity_id: fan.xiaomi_air_purifier_pro
turn_off:
service: xiaomi_miio.fan_set_child_lock_off
data:
entity_id: fan.xiaomi_air_purifier_pro
icon_template: "mdi:lock-outline"
input_select:
xiaomi_airpurifier_mode:
name: Mode
options:
- Auto
- Silent
- Favorite
icon: "mdi:animation-outline"
input_number:
xiaomi_airpurifier_favorite_level:
name: "Favorite Level"
initial: 3
min: 0
max: 16
step: 1
icon: "mdi:weather-windy"
automations.yaml
- alias: Air Purifier mode change
trigger:
entity_id: input_select.xiaomi_airpurifier_mode
platform: state
action:
service: fan.set_speed
data_template:
entity_id: fan.xiaomi_air_purifier_pro
speed: '{{ states.input_select.xiaomi_airpurifier_mode.state }}'
- alias: Air Purifier mode changed
trigger:
platform: state
entity_id: fan.xiaomi_air_purifier_pro
action:
service: input_select.select_option
entity_id: input_select.xiaomi_airpurifier_mode
data_template:
option: '{{ states.fan.xiaomi_air_purifier_pro.attributes.speed }}'
- alias: Air Purifier favorite level change
trigger:
entity_id: input_number.xiaomi_airpurifier_favorite_level
platform: state
action:
service: xiaomi_miio.fan_set_favorite_level
data_template:
entity_id: fan.xiaomi_air_purifier_pro
level: '{{ states.input_number.xiaomi_airpurifier_favorite_level.state | int
}}'
- alias: Air Purifier favorite level changed
trigger:
platform: state
entity_id: fan.xiaomi_air_purifier_pro
action:
service: input_number.set_value
entity_id: input_number.xiaomi_airpurifier_favorite_level
data_template:
value: '{{ states.fan.xiaomi_air_purifier_pro.attributes.favorite_level }}'
Check and restart HA
You can create your Lovelace dashboard using the visual editor once all the Entities are present
What I have changed in the code is:
- added Luminance sensor (can be used for other automations)
- changed parameter, motor_speed to motor2_speed (this is now reporting main fan speed correctly)
- changed to “service: fan.set_speed” in the Air Purifier mode change automation because the original code was not working.
What I want to add:
- Sound, On/Off button (the Xiaomi aplication has it) managed to add the button but can’t figure out how to make it work.
- The icons are not shown for filter and luminance (used official mdi but no luck, not important for now)