I’m trying to automate my Xiaomi Air Purifiers. What I want to achieve:
Turn off air purifier if AQI is below defined value for 3 minutes and set favourite level to default value (defined in automation rule)
Turn on air purifier when aqi is above certain value and set mode to auto or favourite according to defined values
Change air purifier favourite level if:
Air purifier is on
AQI is above defined value for 10 minutes
I have it partially working but can’t get it where I want to, for instance “for” does not work with template condition (I’m most likely doing something wrong here).
I also had to use time trigger for “on” part as I can’t figure out how to use aqi attribute state changes. When I used it it always ended in triggering the auto mode and never switched to favourite. I think this is due how numeric_state works. Ideally I don’t want to use timer at all.
Here’s the code I have:
# Office
- alias: Turn off air purifier office
trigger:
platform: numeric_state
entity_id: fan.xiaomi_air_purifier_office
value_template: "{{ states.fan.xiaomi_air_purifier_office.attributes.aqi | int }}"
below: 15
for:
hours: 0
minutes: 3
seconds: 0
action:
service: fan.turn_off
entity_id: fan.xiaomi_air_purifier_office
- alias: Turn on air purifier office
trigger:
platform: time
minutes: '/1'
seconds: 00
condition:
condition: template
value_template: "{{ states.fan.xiaomi_air_purifier_office.attributes.aqi | int > 15 }}"
# for:
# hours: 0
# minutes: 3
# seconds: 0
action:
- service: fan.turn_on
entity_id: fan.xiaomi_air_purifier_office
- service: fan.set_speed
entity_id: fan.xiaomi_air_purifier_office
data_template:
speed: >
{% if states.fan.xiaomi_air_purifier_office.attributes.aqi | int > 30 %}
favorite
{% elif states.fan.xiaomi_air_purifier_office.attributes.aqi | int < 20 %}
auto
{% else %}
auto
{% 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 }}'
@pnbruckner Your solution works (did not test setting favourite level yet) but will the “on” part with numeric state trigger once? Or will evaluate AQI constantly? For example, AQI is 16, so automation set purifier to “on” and mode to “auto”, what if in the meantime AQI jumps to 35? Will the automation change the mode to “favourite”? Or will it stay at “auto”?
A numeric_state trigger will trigger when the condition (e.g., above 15) becomes true, and will not trigger again until that condition becomes false (e.g., 15 or below) and then becomes true again (back above 15.)
But you also have “for 3 minutes.” That means the “above 15” condition needs to stay true for that long before it actually triggers.
So, let’s say it raises to 16. Then let’s say it stays above 15 for 3 minutes. At that time the action will run, and depending on what the value is at that time (above 30 or not) it will set the speed accordingly. Let’s say it’s 30 or below at that time (so it sets the speed to auto) and then, without going to 15 or below, eventually goes to above 30. Since it hasn’t gone to 15 or below first it won’t trigger again, so the speed will stay at auto.
If you want it to trigger again if it goes above 30, then you need to add another trigger:
HI artkrz. I have a problem , my xiaomi air purifiers used xiaomi_miio compose setting fan. In homekit just a switch,can not change speed mode.please help me
Since I want to be able to force the purifier in ON or OFF state without touching Home Assistant, I defined the automations to work only when I leave the purifier (either on or off) in Auto mode. If I set it to Silent or Favorite the automations won’t do anything.
My goal was to have the Mi Air Purifier PRO turn on at a certain AQI value, turn off when the value drops below a threshold for at least some time, and to turn on the fan every two hours to move the air and hopefully get a more reliable AQI reading. I used Silent mode to avoid other automations to interfere. After that 10 minutes, the fan is switched back to Auto so that the other automations take over.
Checking the speed (Auto, silent, favorite) works only when the device is on, therefore I used the “mode” attribute that is retained and readable also when off.
# turn off only if below AQI value and in AUTO mode, not in silent or manual (favourite)
- alias: 'Auto off air purifier living room'
trigger:
platform: numeric_state
entity_id: 'fan.air_purifier_living_room'
value_template: "{{ state_attr('fan.air_purifier_living_room', 'aqi') }}"
below: 10
for:
minutes: 5
condition:
condition: and
conditions:
- condition: state
entity_id: 'fan.air_purifier_living_room'
state: 'on'
- condition: template
value_template: "{{ is_state_attr('fan.air_purifier_living_room', 'mode', 'auto') }}"
action:
- service: fan.turn_off
entity_id: 'fan.air_purifier_living_room'
# turn on if AQI above specified value and in AUTO mode last time
- alias: 'Auto on air purifier living room'
trigger:
platform: numeric_state
entity_id: 'fan.air_purifier_living_room'
value_template: "{{ state_attr('fan.air_purifier_living_room', 'aqi') }}"
above: 16
condition:
condition: and
conditions:
- condition: state
entity_id: 'fan.air_purifier_living_room'
state: 'off'
- condition: template
value_template: "{{ is_state_attr('fan.air_purifier_living_room', 'mode', 'auto') }}"
action:
- service: fan.turn_on
entity_id: 'fan.air_purifier_living_room'
# turn on every two hours for some minutes to move air around, keep silent for that
- alias: 'Silent on air purifier living room'
trigger:
platform: time_pattern
hours: "/2"
minutes: 0
condition:
condition: and
conditions:
- condition: state
entity_id: 'fan.air_purifier_living_room'
state: 'off'
- condition: template
value_template: "{{ is_state_attr('fan.air_purifier_living_room', 'mode', 'auto') }}"
action:
# turns on automatically when speed is changed
# - service: fan.turn_on
# entity_id: 'fan.air_purifier_living_room'
- service: fan.set_speed
entity_id: 'fan.air_purifier_living_room'
data:
speed: 'Silent'
- delay: 0:10
- service: fan.set_speed
entity_id: 'fan.air_purifier_living_room'
data:
speed: 'Auto'
Actually the Purifier-auto-off automation is not reliable, sometimes it fires, usually not.
If I disable the “for 5 minutes” then it works fine.
Any idea? Since it’s a more generic question, I’m opening a different thread to keep this one about the automations and about debugging generic features of the automation.
I have finally build new sensors based on sds011 and esp. Here’s new automation rules including 1st attempt on using pm10 as well as pm2.5. Works really nice but probably could be improved. Here’s a package for 2s in my office (requires pm2.5 and pm10 readings in mqtt sensor:
I coud not get the fan available after the 0.103 update (breaking change in xiaomi_miio). I cannot understand what I have to change… Am I wrong in something?