I have a Xiaomi air purifier which I run for few minutes every two hours to move the air around.
I do this in Silent mode to ensure that no other automation will touch it in the meanwhile.
At the end of the cycle, if the pollution is below a certain limit, the purifier is switched to automatic AND THEN turned off. This because changing mode of the purifier while off makes it turn on.
However, I find in the logbook that sometimes the fan if first turned off and then changed to Auto.
This results in the fan staying on for a long time, because no other automation will touch it until the pollution threshold is crossed. Also, this periodic Silent mode is not executed if the fan is already on.
To solve the problem I introduced a delay of 5 seconds between changing mode to Auto and turning off, hoping that within 5 seconds the fan will have applied the change of state (or is it maybe that the Home Assistant components subsystem somehow caches the values? I have no way to test).
This proves insufficient.
How could I ensure that the fan is turned off only after the change of mode is applied?
This is the code:
- id: xxx
alias: 'Air Purifier: silent on 10m every 2h'
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:
- service: fan.set_speed
entity_id: fan.air_purifier_living_room
data_template:
speed: Silent
- delay: 0:10
- service: fan.set_speed
entity_id: fan.air_purifier_living_room
data_template:
speed: Auto
- delay: 00:00:05
- condition: template
value_template: '{{ state_attr(''fan.air_purifier_living_room'', ''aqi'') | int < 10 }}'
- service: fan.turn_off
entity_id: fan.air_purifier_living_room