Automating air purifiers

Not sure why are you asking in this thread since you’re setting up your humidifier not air purifier but maybe this change will help:

{{ states.fan.xiaomi_mi_smart_antibacterial_humidifier.attributes.target_humidity }}

to:

{{ states.fan.xiaomi_mi_smart_antibacterial_humidifier.attributes.target_humidity|int }}

If it does not please create your own thread.

Hi Artur, thanks that seems to solve the issue :slight_smile: I copy pasted code for humidifier as its same as for purifier, dont realised that when copying it.

Thanks.

Hi, im totally new to HA and have just found my way into this automation section.
I have an xiaomi air purifier pro and want to add these automations lines to my home assistant configuration but dont exactly know which parts of the code that i must change. entity_id: is that the name of the sensor?

Thanks in advance!

You need to change entity_id for the purifier and sensors if you use an external one. If not, just replace both of them with reading from the purifier.

Here is my updated automation after 2021.9.0

- alias: Xiaomi Air Purifier Favorite Level Change Depends On Pm2.5
  trigger:
  - platform: time_pattern
    minutes: "/5"
  condition:
  - condition: state
    entity_id: fan.xiaomi_air_purifier
    state: 'on'
  action:
  - service: fan.set_preset_mode
    target:
      entity_id: fan.xiaomi_air_purifier
    data:
      preset_mode: Favorite
  - service: number.set_value
    entity_id: number.xiaomi_air_purifier_favorite_level
    data_template:
      value: >
        {% if states('sensor.xiaomi_air_purifier_pm2_5') | int > 140 %}
          14
        {% elif states('sensor.xiaomi_air_purifier_pm2_5') | int > 120 %}
          12
        {% elif states('sensor.xiaomi_air_purifier_pm2_5') | int > 100 %}
          10
        {% elif states('sensor.xiaomi_air_purifier_pm2_5') | int > 80 %}
          8
        {% elif states('sensor.xiaomi_air_purifier_pm2_5') | int > 60 %}
          6
        {% elif states('sensor.xiaomi_air_purifier_pm2_5') | int > 40 %}
          4
        {% elif states('sensor.xiaomi_air_purifier_pm2_5') | int > 20 %}
          2
        {% elif states('sensor.xiaomi_air_purifier_pm2_5') | int > 10 %}
          1
        {% else %}
          0
        {% endif %}
4 Likes

@artkrz Hi thanks for your work. I dont have a xiaomi air purifier but I have a levoit c300 with d1 mini and4 relays to control the fan speed. I tried to change your code but I cant make it work. and I’m on a dead end. If you can, could you help me as it might help someone trying to do a more DIY solution like me in the future. bellow is the code from my air purifier. Is programed to only have one speed on at one given time:

esphome:
  name: air-purifier
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip:
    static_ip: 10.10.10.21
    gateway: 10.10.10.1
    subnet: 255.255.255.0
captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: 

ota:
  password: 

switch:
  - platform: gpio
    name: "high"
    id: high
    pin: D3
    interlock: &interlock_group [high, rest, medium, low]
  - platform: gpio
    name: "sleep"
    pin: D5
    id: rest
    interlock: *interlock_group
  - platform: gpio
    name: "medium"
    pin: D6
    id: medium
    interlock: *interlock_group
  - platform: gpio
    name: "low"
    pin: D7
    id: low
    interlock: *interlock_group

@marcos-acpc I wasn’t here for a while, did you get it working?

So here’s my smog season 2021/2022 updated config, now down to single automation:

automation:

  - id: air_purifier_bedroom_1
    alias: Air purifier automatic control bedroom 1
    trigger:
      - id: aqi_high
        platform: numeric_state
        entity_id: sensor.pm2_5_bedroom_1
        above: 10
        for:
          minutes: 10
      - id: aqi_low
        platform: numeric_state
        entity_id: sensor.pm2_5_bedroom_1
        below: 10
        for:
          minutes: 10
      - id: cron
        platform: time_pattern
        minutes: "/5"
    condition:
      condition: state
      entity_id: group.openclose_bedroom_1
      state: "off"
      for:
        minutes: 1
    action:
      - service: "fan.turn_{{ 'on' if states('sensor.pm2_5_bedroom_1')|int > 10 else 'off' }}"
        data: {}
        target:
          entity_id: fan.xiaomi_air_purifier_bedroom_1
      - delay: '00:00:10'
      - condition: state
        entity_id: fan.xiaomi_air_purifier_bedroom_1
        state: "on"
      - service: fan.set_preset_mode
        target:
          entity_id: fan.xiaomi_air_purifier_bedroom_1
        data:
          preset_mode: Favorite
      - service: number.set_value
        entity_id: number.xiaomi_air_purifier_bedroom_1_favorite_level
        data_template:
          value: >
            {% if states('sensor.pm2_5_bedroom_1') | int > 30 %}
              10
            {% elif states('sensor.pm2_5_bedroom_1') | int > 20 %}
              5
            {% elif states('sensor.pm2_5_bedroom_1') | int > 15 %}
              3
            {% elif states('sensor.pm2_5_bedroom_1') | int > 10 %}
              1
            {% else %}
              0
            {% endif %}
1 Like

Can someone please advise me how I can create an automation for selecting the “AUTO” operating mode of the Xiaomi air purifier ?

I am unable to create such an automation within the function for “Automation and scenes”, this operating mode function is not offered at all.
I don’t know the YAML code for this either. Only fan speeds and favorites are there.

I have 2 such purifiers at home, model “zhimi.airpurifier.mc1” (2S) and model “zhimi.airpurifier.mb3” (3H).

I don’t know how to handle it at all, I don’t know if the integration is loaded correctly.

The whole point is not to use built in Auto mode! My automation works by controlling the air purifier’s Favourite level. If you don’t know YAML just use GUI and after You create new automation (that’s empty) switch to YAML mode and copy paste it, then switch back to GUI.

I guess at this point my code could be transferred into blueprint but I do not have time to to this now, sorry.

But my Xiaomi air purifiers do not react to changing the “Favorite Level” in H.A., or the purifier beeps when changing, but stays in the previous mode.

There is nothing to “Favorite Level” in the app, I can’t see anything through the control panel either.

I would need to be able to set the cleaner’s mode in H.A., th. “AUTO”, or “NIGHT” or “TURBO” etc… In the integration description these modes are mentioned, but I don’t even have them in the panel in H.A…

Use service fan.set_preset_mode and set the preset_mode to Auto. Similarly you can set other modes supported by your air purifier like Night and Favorite. You can use yaml as well as GUI.

   - service: fan.set_preset_mode
        target:
          entity_id: fan.xiaomi_air_purifier
        data:
          preset_mode: Auto