Lovelace: Xiaomi - Mi air purifier 3H card

Hi,

Hereā€™s some cleaned up and working config at 25/04/22 with official Xiaomi Miio integration, thatā€™s how it looks in Spanish (the code in the post has visual names translated to English):

ezgif-2-e7d7a5ecff

  1. Configure your device as normal with the official Xiaomi Miio integration using IP and token

  2. Install THIS CUSTOM CARD through HACS or manually (read instructions on the linked github)

  3. Add custom card from code (that one is for Mi Purifier 3H, edit or adapt it for your device):

type: custom:purifier-card
entity: fan.zhimi_airpurifier_mb3
aqi:
  entity_id: sensor.zhimi_airpurifier_mb3_pm2_5
  unit: Āµg/mĀ³
stats:
  - entity_id: sensor.zhimi_airpurifier_mb3_motor_speed
    unit: RPM
    subtitle: Speed
  - entity_id: sensor.zhimi_airpurifier_mb3_temperature
    unit: ĀŗC
    subtitle: Temperature
  - entity_id: sensor.zhimi_airpurifier_mb3_humidity
    unit: '%'
    subtitle: Humidity
  - entity_id: sensor.zhimi_airpurifier_mb3_filter_life_remaining
    subtitle: Filter
    unit: '%'
actions:
  - name: Silent
    icon: mdi:weather-night
    preset_mode: Silent
  - name: 25%
    icon: mdi:circle-slice-2
    preset_mode: Favorite
    number.zhimi_airpurifier_mb3_favorite_level: 3
  - name: 50%
    icon: mdi:circle-slice-4
    preset_mode: Favorite
    number.zhimi_airpurifier_mb3_favorite_level: 7
  - name: 75%
    icon: mdi:circle-slice-6
    preset_mode: Favorite
    number.zhimi_airpurifier_mb3_favorite_level: 10
  - name: 100%
    icon: mdi:circle-slice-8
    preset_mode: Favorite
    number.zhimi_airpurifier_mb3_favorite_level: 14
  - name: Auto
    icon: mdi:refresh-auto
    preset_mode: Auto
shortcuts:
  - name: Silent
    icon: mdi:weather-night
    preset_mode: Silent
  - name: 25%
    icon: mdi:circle-slice-2
    percentage: 25
  - name: 50%
    icon: mdi:circle-slice-4
    percentage: 50
  - name: 75%
    icon: mdi:circle-slice-6
    percentage: 100
  - name: Auto
    icon: mdi:refresh-auto
    preset_mode: Auto
show_name: true
show_status: true
show_state: true
show_toolbar: true
compact_view: false
  1. Hereā€™s my automations.yaml (descriptions are pretty self-explanatory):
- id: '1234567891011'
  alias: AP01 - Air Purifier turn on
  description: Automatically turns on purifier on Favorite when >15 for +30s, only
    when it's OFF
  trigger:
  - platform: time_pattern
    minutes: /1
  condition:
  - condition: device
    device_id: 62ff38a78452b62f71afd27c306bae6c
    domain: fan
    entity_id: fan.zhimi_airpurifier_mb3
    type: is_off
  - type: is_pm25
    condition: device
    device_id: 62ff38a78452b62f71afd27c306bae6c
    entity_id: sensor.zhimi_airpurifier_mb3_pm2_5
    domain: sensor
    above: 15
  action:
  - data:
      preset_mode: Favorite
    service: fan.turn_on
    target:
      entity_id: fan.zhimi_airpurifier_mb3
  mode: restart
- id: '1234567891012'
  alias: AP03 - Air Purifier set favorite level based on PM2.5
  description: Automatically changes Favorite level every 15s based on PM2.5, only
    when it's ON
  trigger:
  - platform: time_pattern
    seconds: /30
  condition:
  - condition: device
    device_id: 62ff38a78452b62f71afd27c306bae6c
    domain: fan
    entity_id: fan.zhimi_airpurifier_mb3
    type: is_on
  - condition: and
    conditions:
    - condition: state
      entity_id: automation.ap01_air_purifier_turn_on
      state: 'on'
  action:
  - service: fan.set_preset_mode
    target:
      entity_id: fan.zhimi_airpurifier_mb3
    data:
      preset_mode: Favorite
  - service: number.set_value
    entity_id: number.zhimi_airpurifier_mb3_favorite_level
    data_template:
      value: "{% if states('sensor.zhimi_airpurifier_mb3_pm2_5') | int(0) > 500 %}\n\
        \  10\n{% elif states('sensor.zhimi_airpurifier_mb3_pm2_5') | int(0) > 400\
        \ %}\n  9\n{% elif states('sensor.zhimi_airpurifier_mb3_pm2_5') | int(0) >\
        \ 300 %}\n  8\n{% elif states('sensor.zhimi_airpurifier_mb3_pm2_5') | int(0)\
        \ > 200 %}\n  7\n{% elif states('sensor.zhimi_airpurifier_mb3_pm2_5') | int(0)\
        \ > 100 %}\n  6\n{% elif states('sensor.zhimi_airpurifier_mb3_pm2_5') | int(0)\
        \ > 10 %}\n  5\n{% else %}\n  4\n{% endif %}\n"
  mode: restart
- id: '1234567891013'
  alias: AP03 - Air Purifier turn off
  description: Automatically turns off purifier when <5 for +30s, only when it's ON
  trigger:
  - platform: time_pattern
    minutes: /1
  condition:
  - condition: device
    device_id: 62ff38a78452b62f71afd27c306bae6c
    domain: fan
    entity_id: fan.zhimi_airpurifier_mb3
    type: is_on
  - type: is_pm25
    condition: device
    device_id: 62ff38a78452b62f71afd27c306bae6c
    entity_id: sensor.zhimi_airpurifier_mb3_pm2_5
    domain: sensor
    below: 5
  action:
  - data: {}
    entity_id: fan.zhimi_airpurifier_mb3
    service: fan.turn_off
  mode: restart

Hope it helps :wink:

Only problem I currently have is that I get timeouts when fetching data from the purifier every few minutes, hopefully someone can shred some light on this @schilea :

2022-04-26 00:53:00 ERROR (MainThread) [homeassistant.components.xiaomi_miio] Timeout fetching Mi Air Purifier 3/3H data
1 Like

@schilea this is your implementation of the card, do you mind posting your latest config?

Hi guys, been on holydays for few days and today back to work. There is no problem for me to post all the new relevant card implementations, mine included, and I can add an link in my first post for them. Just let me know what and when.

Hi @schilea, do you know how can I fix the fetching timeouts?

2022-04-26 00:53:00 ERROR (MainThread) [homeassistant.components.xiaomi_miio] Timeout fetching Mi Air Purifier 3/3H data

Hi, I think is a miio issue and you have to raise a ticket. It has nothing to do with the card, rather with the implementation

@ssergiio check this Timeout fetching data in Xiaomi Miio Ā· Issue #59215 Ā· home-assistant/core Ā· GitHub

@schilea yeah I saw it and installed the custom component but canā€™t figure out how to use itā€¦ The component is installed and I add the fan to my configurations.yaml but entities/device arenā€™t being showed

Minimalistic Purifier Card
My approach for the card is a bit different, as I try not to have pictures of devices in my dashboard.
So currently it looks like this:

Auto-mode enabled
image

Fan mode enabled
image

Functionality
I have removed all the details, that I do not think I need at a quick glance.
And relevant information appears or disappears when appropriate.

Custom Cards Needed

  1. custom-cards/stack-in-card: :hammer_and_wrench: group multiple cards into one card without the borders (github.com)
  2. kalkih/mini-graph-card: Minimalistic graph card for Home Assistant Lovelace UI (github.com)

Prior configuration needed

  1. Integration

I use the official integration for Miio, and suggest you do the same, as it exposes the necessary entities for this card, combined with some template sensors you define yourself, see below:

  1. Template sensors

You need some template sensors configured, which are the following, remember to change device name to the one you got:

configuration.yaml

template:
  - binary_sensor:
      - name: bedroom_purifier_mode_silent
        state: "{{ state_attr('fan.bedroom_purifier', 'preset_mode') ==  'Silent' }}"
  - binary_sensor:
      - name: bedroom_purifier_mode_favorite
        state: "{{ state_attr('fan.bedroom_purifier', 'preset_mode') ==  'Favorite' }}"
  - binary_sensor:
      - name: bedroom_purifier_mode_fan
        state: "{{ state_attr('fan.bedroom_purifier', 'preset_mode') ==  'Fan' }}"
  - binary_sensor:
      - name: bedroom_purifier_mode_fan_1'
        state: "{{ state_attr('fan.bedroom_purifier', 'preset_mode') ==  'Fan' and is_state('number.bedroomair_purifier_fan_level', '1') }}"
  - binary_sensor:
      - name: bedroom_purifier_mode_fan_2
        state: "{{ state_attr('fan.bedroom_purifier', 'preset_mode') ==  'Fan' and is_state('number.bedroomair_purifier_fan_level', '2') }}"
  - binary_sensor:
      - name: bedroom_purifier_mode_fan_3
        state: "{{ state_attr('fan.bedroom_purifier', 'preset_mode') ==  'Fan' and is_state('number.bedroomair_purifier_fan_level', '3') }}"
  - binary_sensor:
      - name: bedroom_purifier_mode_off
        state: "{{ is_state('fan.bedroom_purifier', 'off') }}"
  1. Background picture

Download the below empty image, and place it in the folder config/www/
AP-Card-Background2
Right click above here and save as picture

Automations
I have multiple automations in use, but the only one that I think is very important, is the changed automode, which only triggers when the purifier is in favorite mode and turned on. Then it will change favorite level depending on the measured 2.5PM value on a much more aggresive manner than the one built in, which is more suited for places with very bad air quality, like Chinese cities.

- id: '1234567891012'
  alias: AP02 - Air Purifier set favorite level based on PM2.5
  description: ''
  trigger:
  - platform: time_pattern
    minutes: /5
  condition:
  - condition: state
    entity_id: fan.bedroom_purifier
    state: Favorite
    attribute: preset_mode
  - condition: state
    entity_id: fan.bedroom_purifier
    state: 'on'
  action:
  - service: fan.set_preset_mode
    target:
      entity_id: fan.bedroom_purifier
    data:
      preset_mode: Favorite
  - service: number.set_value
    entity_id: number.bedroom_purifier_favorite_level
    data_template:
      value: "{% if states('sensor.bedroomair_purifier_pm2_5') | int > 140 %}\n  14\n\
        {% elif states('sensor.bedroomair_purifier_pm2_5') | int > 120 %}\n  12\n\
        {% elif states('sensor.bedroomair_purifier_pm2_5') | int > 100 %}\n  10\n\
        {% elif states('sensor.bedroomair_purifier_pm2_5') | int > 80 %}\n  8\n{%\
        \ elif states('sensor.bedroomair_purifier_pm2_5') | int > 60 %}\n  6\n{% elif\
        \ states('sensor.bedroomair_purifier_pm2_5') | int > 40 %}\n  4\n{% elif states('sensor.bedroomair_purifier_pm2_5')\
        \ | int > 20 %}\n  3\n{% elif states('sensor.bedroomair_purifier_pm2_5') |\
        \ int > 10 %}\n  2\n{% else %}\n  1\n{% endif %}\n"

Dashboard Card

Below is the yaml code for the actual card, that you place in a manual card. Make sure that the device name fits the one you have you.

type: custom:stack-in-card
cards:
  - type: vertical-stack
    cards:
      - type: picture-elements
        image: local/AP-Card-Background.png
        title: Bedroom Air Purifier
        elements:
          - type: state-label
            prefix: 'Filter Left '
            entity: sensor.bedroomair_purifier_filter_life_remaining
            style:
              top: 90%
              left: 85%
          - type: state-icon
            entity: binary_sensor.bedroom_purifier_mode_off
            title: 'Off'
            icon: mdi:power-standby
            tap_action:
              action: call-service
              service: fan.toggle
              service_data:
                entity_id: fan.bedroom_purifier
            style:
              top: 0%
              left: 90%
              min-width: 40px
              min-height: 40px
              transform: translate(-50%, -50%) scale(1.5)
              '--paper-item-icon-color': rgb(192, 192, 192)
              '--paper-item-icon-active-color': rgb(255, 128, 0)
          - type: state-icon
            entity: binary_sensor.bedroom_purifier_mode_favorite
            title: Auto
            icon: mdi:autorenew
            tap_action:
              action: call-service
              service: fan.set_preset_mode
              service_data:
                entity_id: fan.bedroom_purifier
                preset_mode: Favorite
            style:
              top: 0%
              left: 10%
              transform: translate(-50%, -50%) scale(1.5)
              '--paper-item-icon-color': rgb(192, 192, 192)
              '--paper-item-icon-active-color': rgb(255, 128, 0)
          - type: state-icon
            entity: binary_sensor.bedroom_purifier_mode_silent
            title: Silent
            icon: mdi:power-sleep
            tap_action:
              action: call-service
              service: fan.set_preset_mode
              service_data:
                entity_id: fan.bedroom_purifier
                preset_mode: Silent
            style:
              top: 0%
              left: 63.34%
              transform: translate(-50%, -50%) scale(1.5)
              '--paper-item-icon-color': rgb(192, 192, 192)
              '--paper-item-icon-active-color': rgb(255, 128, 0)
              '--iron-icon-height': 120px
              '--iron-icon-width': 120px
          - type: state-icon
            entity: binary_sensor.bedroom_purifier_mode_fan
            title: Fan
            icon: mdi:fan
            tap_action:
              action: call-service
              service: number.set_value
              service_data:
                entity_id: number.bedroomair_purifier_fan_level
                value: 1
            style:
              top: 0%
              left: 36.67%
              transform: translate(-50%, -50%) scale(1.5)
              '--paper-item-icon-color': rgb(192, 192, 192)
              '--paper-item-icon-active-color': rgb(255, 128, 0)
          - type: conditional
            conditions:
              - entity: binary_sensor.bedroom_purifier_mode_favorite
                state: 'on'
            elements:
              - type: state-label
                prefix: 'Level '
                entity: number.bedroom_purifier_favorite_level
                style:
                  top: 50%
                  left: 11.5%
                  font-size: 150%
          - type: conditional
            conditions:
              - entity: binary_sensor.bedroom_purifier_mode_fan
                state: 'on'
            elements:
              - type: state-icon
                entity: binary_sensor.bedroom_purifier_mode_fan_1
                title: Fan level 1
                icon: mdi:fan-speed-1
                tap_action:
                  action: call-service
                  service: number.set_value
                  service_data:
                    entity_id: number.bedroomair_purifier_fan_level
                    value: 1
                style:
                  top: 70%
                  left: 29.67%
                  transform: translate(-50%, -50%) scale(1.2, 1.2)
                  '--paper-item-icon-color': rgb(192, 192, 192)
                  '--paper-item-icon-active-color': rgb(255, 128, 0)
              - type: state-icon
                entity: binary_sensor.bedroom_purifier_mode_fan_2
                title: Fan level 2
                icon: mdi:fan-speed-2
                tap_action:
                  action: call-service
                  service: number.set_value
                  service_data:
                    entity_id: number.bedroomair_purifier_fan_level
                    value: 2
                style:
                  top: 70%
                  left: 36.37%
                  transform: translate(-50%, -50%) scale(1.2, 1.2)
                  '--paper-item-icon-color': rgb(192, 192, 192)
                  '--paper-item-icon-active-color': rgb(255, 128, 0)
              - type: state-icon
                entity: binary_sensor.bedroom_purifier_mode_fan_3
                title: Fan level 3
                icon: mdi:fan-speed-3
                tap_action:
                  action: call-service
                  service: number.set_value
                  service_data:
                    entity_id: number.bedroomair_purifier_fan_level
                    value: 3
                style:
                  top: 70%
                  left: 43.67%
                  transform: translate(-50%, -50%) scale(1.2, 1.2)
                  '--paper-item-icon-color': rgb(192, 192, 192)
                  '--paper-item-icon-active-color': rgb(255, 128, 0)
      - type: custom:mini-graph-card
        entities:
          - sensor.bedroomair_purifier_pm2_5
        points_per_hour: 3
        animate: true
        show:
          icon: false
          name: false
          state: true

7 Likes

You can link to 336 for my rendition

Done, check the 1st post

1 Like

Nice cardā€¦

Can someone please confirm if I could use same integration for 3C model instead of 3H?

I can confirm that

1 Like

Thank you for confirming

@bjorn.sivertsen Iā€™m trying your configuration and Iā€™m having troubles figuring out what is the number.bedroomair_purifier_fan_level entity so I can replace it with mine. I have the mi air 3H, so I should have that entity.

Thanks for making this available to the community. Iā€™ve created my own version, mostly based on the version by @bjorn.sivertsen, with a few modifications and additions.

My ā€œFavorite modeā€ (alternative auto mode) looks like this:
image

Some of the modifications I made:

  • Added the default Auto mode
  • Added a selector to adjust the Favorite fan level
  • Added color indication on the Favorite symbol according to PM 2.5 level (green, yellow, orange, red)
  • Added adjustment for LED brightness (off, dim, bright)
  • Added lock/unlock for the display button (Child-lock)
  • Added control for buzzer (on, off)
  • Included some more automations

If anyone is interested my version is in my Github repository here

I tried to include a detailed instruction that can be followed even by someone completely new to Home Assistant, so pardon me for the spoon-feeding! :slight_smile:

3 Likes

Hi Christer, Iā€™ve since moved to a less polluted area of the city, and have actually stopped using my purifier.

But a bit kudos to taking it further (and the fantastic instructions!), your additions make a lot of sense! :slight_smile:

Have you found a solution to this, I have the same question?

hi,
how can i get the token for configuration.yaml, id for automation.yaml?
i am new in home assistant things.
thank you!

If you install the addon, it should sort the token for you through the process.

Id for automation doesnā€™t matter and you can give it any you like. If you create an automation through the UI, it is automatically generated.