Automating air purifiers

100% sure. I resolved that issue by splitting it to separate automations for turning on/off and for setting speed based on aqi.
For turning on I use “for: minutes: 5” numeric_state trigger and for speed setting i use “minutes ‘/5’” time_pattern trigger. Code below.

Before that change I used only two automations – one for turning off and one for turning on & speed setting (numeric_state trigger with “for: minutes: 5”). Maybe I shouldn’t.

Automation code
# Xiaomi Air Purifier 2S – automation
- id: purifier_off
  alias: 'Purifier Off'
  initial_state: true
  hide_entity: true
  trigger:
  - platform: numeric_state
    entity_id: fan.xiaomi_miio_device
    value_template: '{{ state.attributes.aqi }}'
    below: 5
    for:
      minutes: 5
  condition:
  - condition: state
    entity_id: fan.xiaomi_miio_device
    state: 'on'
  action:
  - service: fan.turn_off
    entity_id: fan.xiaomi_miio_device
###

- id: purifier_on
  alias: 'Purifier On'
  initial_state: true
  hide_entity: true
  trigger:
    platform: numeric_state
    entity_id: fan.xiaomi_miio_device
    value_template: '{{ state.attributes.aqi }}'
    above: 5
    for:
      minutes: 3
  condition:
    - condition: state
      entity_id: fan.xiaomi_miio_device
      state: 'off'
  action:
    - service: fan.turn_on
      entity_id: fan.xiaomi_miio_device
    - service: fan.speed
      entity_id: fan.xiaomi_miio_device
      data:
        speed: Auto
###

### Purifier Auto
- id: purifier_auto
  alias: 'Purifier Auto'
  initial_state: true
  trigger:
    platform: time_pattern
    minutes: '/5'
    seconds: 00
  condition:
    - condition: state
      entity_id: 'fan.xiaomi_miio_device'
      state: 'on'
  action:
    - service: fan.set_speed
      entity_id: fan.xiaomi_miio_device
      data:
        speed: Favorite
    - service: xiaomi_miio.fan_set_favorite_level
      data_template:
        entity_id: fan.xiaomi_miio_device
        level: >
          {% if state_attr('fan.xiaomi_miio_device', 'aqi') | int > 35 %}
          14
          {% elif state_attr('fan.xiaomi_miio_device', 'aqi') | int > 30 %}
          10
          {% elif state_attr('fan.xiaomi_miio_device', 'aqi') | int > 25 %}
          7
          {% elif state_attr('fan.xiaomi_miio_device', 'aqi') | int > 20 %}
          4
          {% elif state_attr('fan.xiaomi_miio_device', 'aqi') | int > 15 %}
          2
          {% elif state_attr('fan.xiaomi_miio_device', 'aqi') | int > 10 %}
          1
          {% else %}
          0
          {% endif %}

I resolved that issue by splitting it to separate automations for turning on/off and for setting speed based on aqi.

Isn’t that exactly what I’m doing? Doesn’t look like You had a problem with the code in this thread. Just read the code in previous posts, I have these:

  1. Turn off air purifier…
  2. Turn on air purifier…
  3. Set favourite fan speed for air purifier…

Anyway, happy you got it working.

Sure, you’re right. Sorry – I have forgot to specify and yup, I pobably even copied your idea :smiley:
I was really loopy coming home after a weekend. That’s why :slight_smile:

No worries.

Revised version with triggers based on PM2.5 and PM10, also had to bump these up as some of my purifiers where on 24/h due too poor air quality in Poland :frowning:

- alias: Turn off air purifier office
    trigger:
      - platform: numeric_state
        entity_id: sensor.office_pm_2_5
        value_template: "{{ states('sensor.office_pm_2_5') | int }}"
        below: 10
        for:
          minutes: 10
      - platform: numeric_state
        entity_id: sensor.office_pm_10
        value_template: "{{ states('sensor.office_pm_10') | int }}"
        below: 15
        for:
          minutes: 10
    condition:
      - condition: state
        entity_id: 'fan.xiaomi_air_purifier_office'
        state: 'on'
    action:
      service: fan.turn_off
      entity_id: fan.xiaomi_air_purifier_office
  - alias: Turn on air purifier office
    trigger:
      - platform: numeric_state
        entity_id: sensor.office_pm_2_5
        value_template: "{{ states('sensor.office_pm_2_5') | int }}"
        above: 10
        for:
          minutes: 5
      - platform: numeric_state
        entity_id: sensor.office_pm_10
        value_template: "{{ states('sensor.office_pm_10') | int }}"
        above: 15
        for:
          minutes: 5
    condition:
      - condition: state
        entity_id: 'fan.xiaomi_air_purifier_office'
        state: 'off'
    action:
      - service: fan.turn_on
        entity_id: fan.xiaomi_air_purifier_office
      - service: fan.speed
        entity_id: fan.xiaomi_air_purifier_office
        data:
          speed: auto
  - alias: Set favorite fan speed for air purifier office
    trigger:
      platform: time_pattern
      minutes: '/5'
      seconds: 00
    condition:
      - condition: state
        entity_id: 'fan.xiaomi_air_purifier_office'
        state: 'on'
    action:
      - service: fan.set_speed
        entity_id: fan.xiaomi_air_purifier_office
        data:
          speed: favorite
      - service: xiaomi_miio.fan_set_favorite_level
        data_template:
          entity_id: fan.xiaomi_air_purifier_office
          level: >
              {% if states('sensor.office_pm_2_5') | int > 30 or states('sensor.office_pm_10') | int > 50 %}
                10
              {% elif states('sensor.office_pm_2_5') | int > 20 or states('sensor.office_pm_10') | int > 40 %}
                5
              {% elif states('sensor.office_pm_2_5') | int > 15 or states('sensor.office_pm_10') | int > 20 %}
                3
              {% elif states('sensor.office_pm_2_5') | int > 10 or states('sensor.office_pm_10') | int > 15 %}
                1
              {% else %}
                0
              {% endif %}
      - service: input_select.select_option
        data_template:
          entity_id: input_select.xiaomi_air_purifier_mode_office
          option: '{{ states.fan.xiaomi_air_purifier_office.attributes.mode }}'
      - service: input_number.set_value
        data_template:
          entity_id: input_number.xiaomi_air_purifier_favorite_level_office
          value: '{{ states.fan.xiaomi_air_purifier_office.attributes.favorite_level }}'

Is possible in th 0.105.3 , the automations not function?
Mi Air purifier 2S , are not turno OFF every is On!

No idea. I’m on 0.105.0.dev20200122. I will test before upgrading though. Thanks.

when check for all ok i appear error in this line:

  speed: {{ states('input_select.xiaomi_airpurifier_mode') }}

the error is:

Error loading /config/configuration.yaml: invalid key: “OrderedDict([(“states(‘fan.xiaomi_miio_device.attributes.mode’)”, None)])”
in “/config/automations.yaml”, line 259, column 0

It looks like your purifier isn’t available. Can you control it from developer tools?

Is avaliable.

Appear 2 errors:

Invalid option: (possible options: Auto, Favorite, Silent)

in the config are this:

input_select:
  # Xiaomi Air Purifier 2S
  xiaomi_airpurifier_mode:
    name: Mode
    options:
      - Auto
      - Silent
      - Favorite
      - Idle
    icon: "mdi:animation-outline"

and this:

Error while executing automation automation.air_purifier_favorite_level_changed. Invalid data for call_service at pos 1: expected float for dictionary value @ data[‘value’]

this is my auto

- 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_2s
      speed: "{{ states('input_select.xiaomi_airpurifier_mode') }}"
  id: 70c686c4c277473d84d61ba9e2a759e5
- alias: Air Purifier mode changed
  trigger:
    platform: state
    entity_id: fan.xiaomi_air_purifier_2s
  condition:
    condition: and
    conditions:
    - condition: template
      value_template: "{{ (state_attr('fan.xiaomi_air_purifier_2s', 'speed') | string) != (states('input_select.xiaomi_airpurifier_mode') | string) }}"
  action:
    service: input_select.select_option
    entity_id: input_select.xiaomi_airpurifier_mode
    data_template:
      option: '{{ states.fan.xiaomi_air_purifier_2s.attributes.speed }}'
  id: 7a41a74062c94ad0a6cd8f48003f1328
- alias: Air Purifier favorite level change
  trigger:
    entity_id: input_number.xiaomi_airpurifier_favorite_level
    platform: state
  action:
    service: fan.xiaomi_miio_set_favorite_level
    data_template:
      entity_id: fan.xiaomi_air_purifier_2s
      level: "{{ states('input_number.xiaomi_airpurifier_favorite_level') | int }}"
  id: 6b82ef62a375465cbf3020244cdc48b0
- alias: Air Purifier favorite level changed
  trigger:
    platform: state
    entity_id: fan.xiaomi_air_purifier_2s
  condition:
    condition: and
    conditions:
    - condition: template
      value_template: "{{ (state_attr('fan.xiaomi_air_purifier_2s', 'favorite_level') | int) != (states('input_number.xiaomi_airpurifier_favorite_level') | int) }}"
  action:
    service: input_number.set_value
    entity_id: input_number.xiaomi_airpurifier_favorite_level
    data_template:
      value: "{{ states('fan.xiaomi_miio_device.attributes.favorite_level') }}"
  id: dd3d5b9b2c414f2187108261aa22cf12
- id: '1578857709245'
  alias: 'Purificador Auto off '
  description: Apagado solo si el valor IQ es menor de 10,y en el ultimo tiempo no
    ha estado ni en silencio ni en manual
  trigger:
  - below: '10'
    entity_id: fan.xiaomi_air_purifier_2s
    for: 00:05:00
    platform: numeric_state
    value_template: "{{ state_attr('fan.xiaomi_air_purifier_2s', 'aqi') }}"
  condition:
  - condition: and
    conditions:
    - condition: state
      entity_id: fan.xiaomi_air_purifier_2s
      state: 'on'
    - condition: template
      value_template: "{{ is_state_attr('fan.xiaomi_air_purifier_2s', 'mode', 'auto') }}"
  action:
  - entity_id: fan.xiaomi_air_purifier_2s
    service: fan.turn_off
- id: '1578858063195'
  alias: Purificadro AUTO ON
  description: Solo se enciende si el valor IQ es mas de 16 y el ultimo estado has
    isdo AUTO
  trigger:
  - above: '16'
    entity_id: fan.xiaomi_air_purifier_2s
    platform: numeric_state
    value_template: "{{ state_attr('fan.xiaomi_air_purifier_2s', 'aqi') }}"
  condition:
  - condition: and
    conditions:
    - condition: state
      entity_id: fan.xiaomi_air_purifier_2s
      state: 'off'
    - condition: template
      value_template: "{{ is_state_attr('fan.xiaomi_air_purifier_2s', 'mode', 'auto') }}"
  action:
  - entity_id: fan.xiaomi_air_purifier_2s
    service: fan.turn_on
- id: '1578858381080'
  alias: Purificador ON cada 2 horas
  description: ''
  trigger:
  - hours: /2
    platform: time_pattern
  condition:
  - condition: and
    conditions:
    - condition: state
      entity_id: fan.xiaomi_air_purifier_2s
      state: 'off'
    - condition: template
      value_template: "{{ is_state_attr('fan.xiaomi_air_purifier_2s', 'mode', 'auto') }}"
  action:
  - data:
      speed: silent
    entity_id: fan.xiaomi_air_purifier_2s
    service: fan.set_speed
  - delay: 00:10:00
  - data:
      speed: Auto
    entity_id: fan.xiaomi_air_purifier_2s
    service: fan.set_speed

You sure service: fan.set_speed exists and it’s not service: fan.speed? You’re not using the code I posted so You have to troubleshoot it yourself a bit more mate.

Anotación 2020-02-12 151935

What is your code??

All the code is pasted above. And it works for me on 2s and pro purifiers.

Just bought the Xiaomi Air Purifier 2S, How can I add it to HA?
I need to extract a token somehow right? I got the iOS Mi Home App installed and set up with the purifier.
whats next?

Read documentation. It says how to get the token.

this?

yes, that’s the one.

Any body meets the following errer on startup, I am on HA 0.107.7:

2020-04-04 21:32:36 INFO (MainThread) [homeassistant.components.xiaomi_miio.fan] Initializing with host 192.168.1.102 (token 7b9f8...)
2020-04-04 21:32:36 INFO (MainThread) [homeassistant.components.xiaomi_miio.fan] zhimi.airpurifier.ma2 1.4.3_13108 MW300 detected
2020-04-04 21:32:57 ERROR (MainThread) [homeassistant.helpers.service] Error rendering data template: UndefinedError: 'None' has no attribute 'attributes'
2020-04-04 21:32:57 ERROR (MainThread) [homeassistant.helpers.service] Error rendering data template: UndefinedError: 'None' has no attribute 'attributes'

Thanks a lot

No idea, I’m using 2s and pro.

Hi, know this is old thread but i used the code (thanks for that!) mentioned in here and my purifier is doing what it should but only issue that is bugging me are following lines in logs: Zvlhčovač vzduchu - cílová vlhkost nastavena: Error executing script. Invalid data for call_service at pos 1: Invalid value for input_number.xiaomi_airhumidifier_target_humidity: 0.0 (range 20.0 - 90.0)

- alias: Zvlhčovač vzduchu - cílová vlhkost nastavena
  trigger:
    platform: state
    entity_id: fan.xiaomi_mi_smart_antibacterial_humidifier
  condition:
    condition: and
    conditions:
    - condition: template
      value_template: '{{ (state_attr(''fan.xiaomi_mi_smart_antibacterial_humidifier'', ''humidity'') | int) != (states(''input_number.xiaomi_airhumidifier_target_humidity'') | int ) }}'
  action:
    service: input_number.set_value
    entity_id: input_number.xiaomi_airhumidifier_target_humidity
    data_template:
      value: '{{ states.fan.xiaomi_mi_smart_antibacterial_humidifier.attributes.target_humidity }}'
  id: 38707ddb4a7f4315a036928299f00a78
  xiaomi_airhumidifier_target_humidity:
    name: 'Cílová vlhkost'
    initial: 60
    min: 20
    max: 90
    step: 5
    icon: "mdi:weather-rainy"

I know its only cosmetic but log is overvelmed by it and it makes it harder to search for another things :slight_smile: I tried to play with template format a bit but to no avail :confused:

Thanks!