Xiaomi Air Purifier - Show all sensors and Switches in UI and HomeKit

Hi guys!
Given the recent “breaking changes” introduced with HA 2021.9, could some more experienced coder rewrite the automations of this thread (which have helped a lot of users) in order to use the new sensors of the xiaomi platform?
I’m trying but I can’t get them to work…

Maybe you could post or at least link to the automation(s) you want to get updated. I highly doubt, that someone will scroll through a 181 post long thread to find them. :slight_smile:

Just post the code you have problems with, someone may chime in and can help. :slight_smile: Can’t guarantee I’m the one, but I will try to help, as far as I can. :slight_smile:

1 Like

You are simply right
Here are the automations I’m talking about (practically those present at the beginning of the thread and then subsequently updated thanks to the suggestions of some users)

## Xiaomi Air Purifier 2H ##

input_select:
  zhimi_airpurifier_mode:
    name: Mode
    options:
      - Auto
      - Silent
      - Idle
      - Favorite
    icon: "mdi:animation-outline"

input_number:
  zhimi_airpurifier_favorite_level:
    name: "Favorite level"
    initial: 0
    min: 0
    max: 14
    step: 1
    icon: "mdi:weather-windy"

automation:
- id: purifier_mode_change
  alias: 'Air Purifier (mode change)'
  trigger:
    entity_id: fan.zhimi_airpurifier_v2
    platform: state
  condition:
    condition: not
    conditions:
      - condition: state
        entity_id: fan.zhimi_airpurifier_v2
        attribute: speed
        state: null
  action:
    service: input_select.select_option
    data_template:
      option: "{{ state_attr('fan.zhimi_airpurifier_v2', 'speed') }}"
    entity_id: input_select.zhimi_airpurifier_mode

- id: purifier_mode_changed
  alias: 'Air Purifier (mode changed)'
  trigger:
    platform: state
    entity_id: fan.zhimi_airpurifier_v2
  condition:
    condition: and
    conditions:
    - condition: template
      value_template: "{{ (state_attr('fan.zhimi_airpurifier_v2', 'speed') | string) != (states('input_select.xiaomi_airpurifier_mode') | string) }}"
  action:
    service: input_select.select_option
    entity_id: input_select.zhimi_airpurifier_mode
    data_template:
      option: '{{ states.fan.zhimi_airpurifier_v2.attributes.mode }}' #- speed for mode

 
- id: purifier_fav_level_change
  alias: 'Air Purifier (favorite level change)'
  trigger:
    entity_id: input_number.zhimi_airpurifier_favorite_level
    platform: state
  action:
    service: xiaomi_miio.fan_set_favorite_level
    data_template:
      entity_id: fan.zhimi_airpurifier_v2
      level: '{{ states.input_number.zhimi_airpurifier_favorite_level.state | int }}'
 
- id: purifier_fav_level_changed
  alias: 'Air Purifier (favorite level changed)'
  trigger:
    platform: state
    entity_id: fan.zhimi_airpurifier_v2
  condition:
    condition: and
    conditions:
    - condition: template
      value_template: "{{ (state_attr('fan.zhimi_airpurifier_v2', 'favorite_level') | int) != (states('input_number.zhimi_airpurifier_favorite_level') | int) }}"
  action:
    service: input_number.set_value
    entity_id: input_number.zhimi_airpurifier_favorite_level
    data_template:
      value: '{{ states.fan.zhimi_airpurifier_v2.attributes.favorite_level }}'

As you probably already know, all the attributes of the previous sensor (which were used in these automations to be able to use the air purifier through Lovelace UI) with the latest release 2021.9 have become autonomous sensors.

I am not yet sufficiently prepared on these aspects of “Home Assistant” and therefore I cannot reconfigure them

That is the normal warning message you get (even with the old) when the unit turn off instead of keeping the mode it’s displayed as null so it throw off a warning message but still should work as normal. Have to be a different reason why it dont work. Can you turn the unit on and off at the developer tool?

1 Like

Yes, I can turn the device on and off from the “developer tool” page.
I’m afraid I have not explained myself well because of my poor English.
I didn’t mean that your template doesn’t work but I just saw that error in the logs

Oh yes, that also happen with with previous too, unit report the mode as “null” when its off. some unit keep mode then it wont have that warning.

eg my humidifier keep its mode when off

you can write up an automation like old way, but as mention it not necessary anymore to have 1 input_select and 2 automation ( change mode, and monitor mode) now you can have a single “select” template in configuration.yaml

1 Like

Thanks again but as I wrote to @ paddy0174 just now, I’m not ready to write some code by myself yet.
Moreover, a few minutes ago a new version of HA came out, the 2021.9.5 and in the logs we read the following:

Fix available property for Xiaomi Miio fan platform (@bieniu - #55889) (xiaomi_miio docs)

Thats good news. hope they fix the “null” issue.

I have to be honest, i didn’t try to understand exactly, what these automations are doing, but it’s late here (01:30am) and…you know…

But nonetheless, this is the changed code. Please check, if the new sensor names (the ones newly added through the last update) I assumed are the ones really used.

So here we go:

## Xiaomi Air Purifier 2H ##

input_select:
  zhimi_airpurifier_mode:
    name: Mode
    options:
      - Auto
      - Silent
      - Idle
      - Favorite
    icon: "mdi:animation-outline"

input_number:
  zhimi_airpurifier_favorite_level:
    name: "Favorite level"
    initial: 0
    min: 0
    max: 14
    step: 1
    icon: "mdi:weather-windy"

automation:
- id: purifier_mode_change
  alias: 'Air Purifier (mode change)'
  trigger:
    entity_id: fan.zhimi_airpurifier_v2
    platform: state
  condition:
    condition: not
    conditions:
      - condition: state
        entity_id: number.zhimi_airpurifier_v2_favorite_level
        state: null
  action:
    service: input_select.select_option
    data_template:
      option: "{{ states('number.zhimi_airpurifier_v2_favorite_level') }}"
    entity_id: input_select.zhimi_airpurifier_mode

- id: purifier_mode_changed
  alias: 'Air Purifier (mode changed)'
  trigger:
    platform: state
    entity_id: fan.zhimi_airpurifier_v2
  condition:
    conditions:
    - condition: template
      value_template: "{{ (states('number.zhimi_airpurifier_v2_favorite_level') | string) != (states('input_select.xiaomi_airpurifier_mode') | string) }}"
  action:
    service: input_select.select_option
    entity_id: input_select.zhimi_airpurifier_mode
    data_template:
      option: '{{ states.fan.zhimi_airpurifier_v2.attributes.mode }}' #- speed for mode

 
- id: purifier_fav_level_change
  alias: 'Air Purifier (favorite level change)'
  trigger:
    entity_id: input_number.zhimi_airpurifier_favorite_level
    platform: state
  action:
    service: number.set_value
    data_template:
      entity_id: number.zhimi_airpurifier_v2_favorite_level
      level: '{{ states.input_number.zhimi_airpurifier_favorite_level.state | int }}'
 
- id: purifier_fav_level_changed
  alias: 'Air Purifier (favorite level changed)'
  trigger:
    platform: state
    entity_id: fan.zhimi_airpurifier_v2
  condition:
    condition: and
    conditions:
    - condition: template
      value_template: "{{ (state_attr('number.zhimi_airpurifier_v2_favorite_level') | int) != (states('input_number.zhimi_airpurifier_favorite_level') | int) }}"
  action:
    service: input_number.set_value
    entity_id: input_number.zhimi_airpurifier_favorite_level
    data_template:
      value: '{{ states(number.zhimi_airpurifier_v2_favorite_level) }}'

As I said, it’s late and I didn’t check the code for plausibility. Something tells me, with the new entities there is an easier way to achieve what these automations do. :slight_smile: But that’s for another day, just check if this works ok for you. :slight_smile:

Let me know how it worked! :slight_smile:

hmmm the script that you post purifier_mode_change & purifier_mode_changed seem to be same. Both call on to same service.

  action:
    service: input_select.select_option

there is no call to the airpurifier. When I get home i’ll look back at the old setting and see if I can edit it to your setting. From what I see the “set.speed” is change to “set.preset_mode” and the favorite level is totally changed.

tthank you @paddy0174 and @huu,
It’s very kind of you.
this night, when I’ll be back home, I will try the new code ……
I’ll let you know

Can try this one too, see which work for you.

- id: purifier_mode_change
  alias: 'Air Purifier (mode change)'
  trigger:
    entity_id: input_select.zhimi_airpurifier_mode
    platform: state
  action:
    service: fan.set_preset_mode
    data_template:
      preset_mode: "{{ states.input_select.zhimi_airpurifier_mode.state }}"
    entity_id: fan.zhimi_airpurifier_v2
    
- id: purifier_mode_changed
  alias: 'Air Purifier (mode changed)'
  trigger:
    entity_id: fan.zhimi_airpurifier_v2
    platform: state
  condition:
    condition: not
    conditions:
      - condition: state
        entity_id: fan.zhimi_airpurifier_v2
        attribute: preset_mode
        state: null
  action:
    service: input_select.select_option
    data_template:
      option: "{{ state_attr('fan.zhimi_airpurifier_v2', 'preset_mode') }}"
    entity_id: input_select.zhimi_airpurifier_mode

- id: purifier_fav_level_change
  alias: 'Air Purifier (favorite level change)'
  trigger:
    entity_id: input_number.zhimi_airpurifier_favorite_level
    platform: state
  action:
    service: number.set_value
    data_template:
      entity_id: number.zhimi_airpurifier_v2_favorite_level
      value: '{{ states.input_number.zhimi_airpurifier_favorite_level.state | int }}'
 
- id: purifier_fav_level_changed
  alias: 'Air Purifier (favorite level changed)'
  trigger:
    platform: state
    entity_id: number.zhimi_airpurifier_v2_favorite_level
  condition:
    condition: and
    conditions:
    - condition: template
      value_template: "{{ (state_attr('number.zhimi_airpurifier_v2_favorite_level', 'state') | int) != (states('input_number.zhimi_airpurifier_favorite_level') | int) }}"
  action:
    service: input_number.set_value
    entity_id: input_number.zhimi_airpurifier_favorite_level
    data_template:
      value: '{{ states.number.zhimi_airpurifier_mc1_favorite_level.state }}'

Following last update I do not see the favorite level anymore amongst the State attributes. I’m using a Xiaomi Air Purifier 2S (state attribute: model: zhimi.airpurifier.mc1). Anyone else solved this issue?

@ Bergerie
Its a separate entity call [ number.zhimi_airpurifier_mc1_favorite_level] … look in developer tools

Bingo! Thanks a lot. Is it still possible to convert the number box to an input slider for favorite level?

Yep see liuk4friends question above, have to use automation like before but for that new entity.

just edit script for the select template to ignore the “null” error.

template:
- select:
    - name: "Air Purifier Mode"
      state: >
        {% if is_state('fan.zhimi_airpurifier_mc1', 'off') %}
          Silent
        {% else %}
          {{ state_attr('fan.zhimi_airpurifier_mc1', 'preset_mode') }}
        {% endif %}
      options: "{{ state_attr('fan.zhimi_airpurifier_mc1', 'preset_modes') }}"
      select_option:
        service: fan.set_preset_mode
        target:
          entity_id: fan.zhimi_airpurifier_mc1
        data:
            preset_mode: "{{ option }}"

This goes in the configuration.yaml replace the input_select.air_purifier_mode and can remove the two associated automation.

1 Like

All done and working perfect. Thanks!

Hi everybody,
First of all, I want to thank you for taking your time to me.
Above all, I want thank @paddy0174 for sacrificing your rest for my incompetence.

I’ve tried the two modified automations (the first by @paddy0174 and the second by @huu) and, unfortunately, in the first scenario (@paddy0174 's suggestion) I’ve found some errors in my logs.

The automations written by @huu, on the other hand, do not cause errors.

Therefore I will try for some time to keep this last configuration.

I still have to try the new template suggested by @huu by removing “input_select.air_purifier_mode”.

2 Likes

For anyone who like a compact view for mobile, only show all entities when the airpurifier is on. Here is an idea for a Lovelace conditional card (Use text ‘search and replace’ to replace all instance off “zhimi_airpurifier_mc1” to your device name - assuming keeping naming consistent). Just add card → show code editor and paste this in. Two entity that also need edit is select.air_purifier_mode (your select mode) and number.zhimi_airpurifier_mc1_favorite_level (your select fav level).
example

type: vertical-stack
cards:
  - type: conditional
    conditions:
      - entity: fan.zhimi_airpurifier_mc1
        state: 'on'
    card:
      type: vertical-stack
      cards:
        - type: horizontal-stack
          cards:
            - type: entities
              entities:
                - entity: fan.zhimi_airpurifier_mc1
              state_color: true
            - type: entities
              entities:
                - entity: select.air_purifier_mode
              state_color: true
        - type: horizontal-stack
          cards:
            - type: horizontal-stack
              cards:
                - type: entities
                  entities:
                    - entity: number.zhimi_airpurifier_mc1_favorite_level
                      name: Favorite Level
                    - entity: switch.zhimi_airpurifier_mc1_child_lock
                      name: Child Lock
                    - entity: switch.zhimi_airpurifier_mc1_led
                      name: LED
                    - entity: sensor.zhimi_airpurifier_mc1_temperature
                      name: Temperature
                    - entity: sensor.zhimi_airpurifier_mc1_filter_use
                      name: Filter Use
                  state_color: true
                - type: entities
                  entities:
                    - entity: sensor.zhimi_airpurifier_mc1_motor_speed
                      name: Motor Speed
                    - entity: sensor.zhimi_airpurifier_mc1_pm2_5
                      name: Particle
                    - entity: switch.zhimi_airpurifier_mc1_buzzer
                      name: Buzzer
                    - entity: sensor.zhimi_airpurifier_mc1_humidity
                      name: Humidity
                    - entity: sensor.zhimi_airpurifier_mc1_filter_life_remaining
                      name: Filter Remaining
                  state_color: true
  - type: conditional
    conditions:
      - entity: fan.zhimi_airpurifier_mc1
        state: 'off'
    card:
      type: horizontal-stack
      cards:
        - type: entities
          entities:
            - entity: fan.zhimi_airpurifier_mc1
          state_color: true
1 Like