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.
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:
Sure, you’re right. Sorry – I have forgot to specify and yup, I pobably even copied your idea
I was really loopy coming home after a weekend. That’s why
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
- 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 }}'
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.
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?
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)
I know its only cosmetic but log is overvelmed by it and it makes it harder to search for another things I tried to play with template format a bit but to no avail