Airpurifier Automation

Hey guys,

I’m pretty new and actually have no clue what I’m doing here. Sound like a good start at least to me.

So I found a automation Script here and “edited” it to my liking (and use).

It looks like that now:

- alias: Set state for air purifier mode input
  trigger:
    - platform: homeassistant
      event: start
    - platform: state
      entity_id: sensor.xiaomi_miio_device
  action:
    - service: input_select.select_option
      data_template:
        entity_id: input_select.xiaomi_miio_device
        option: '{{ states.fan.xiaomi_miio_device.attributes.mode }}'

- alias: Set state for air purifier favorite level input
  trigger:
    - platform: homeassistant
      event: start
    - platform: state
      entity_id: sensor.xiaomi_miio_device
  action:
    - service: input_number.set_value
      data_template:
        entity_id: input_number.xiaomi_miio_device
        value: '{{ states.fan.xiaomi_miio_device.favorite_level | int }}'

################################################################################
# Inputs
################################################################################

- alias: Air purifier Mode
  trigger:
    platform: state
    entity_id: input_select.xiaomi_miio_device
  action:
    - service: fan.set_speed
      data_template:
        entity_id: fan.xiaomi_miio_device
        speed: '{{ states.input_select.xiaomi_miio_device.state }}'

- alias: Air purifier User Mode
  trigger:
    platform: state
    entity_id: input_number.xiaomi_miio_device
  action:
    - service: fan.xiaomi_miio_device_set_favorite_level
      data_template:
        entity_id: fan.xiaomi_miio_device
        level: '{{ states.input_number.xiaomi_miio_device.state | int }}'


################################################################################
# Automatic control
################################################################################

- alias: Air purifier ausschalten
  trigger:
    platform: numeric_state
    entity_id: fan.xiaomi_miio_device
    value_template: "{{ states.fan.xiaomi_miio_device.attributes.aqi | int }}"
    below: 20
    for: '00:03:00'
  condition:
    - condition: state
      entity_id: 'fan.xiaomi_miio_device'
      state: 'on'
  action:
    - service: fan.turn_off
      entity_id: fan.xiaomi_miio_device

- alias: Air purifier Nacht
  trigger:
    platform: numeric_state
    entity_id: fan.xiaomi_miio_device
    value_template: "{{ states.fan.xiaomi_miio_device.attributes.aqi | int }}"
    above: 100
    for: '00:03:00'
  condition:
    - condition: state
      entity_id: 'fan.xiaomi_miio_device'
      state: 'on'
  action:
    - service: fan.turn_on
      entity_id: fan.xiaomi_miio_device
    - service: fan.speed
      entity_id: fan.xiaomi_miio_device
      data:
        speed: favorite

- alias: Air purifier User
  trigger:
    platform: numeric_state
    entity_id: fan.xiaomi_miio_device
    value_template: "{{ states.fan.xiaomi_miio_device.attributes.aqi | int }}"
    above: 20
    for: '00:03:00'
  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: silent

#- alias: Set favorite fan speed for air purifier
#  trigger:
#    platform: time_pattern
#    minutes: '/5'
#  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: fan.xiaomi_miio_device_set_favorite_level
#      data_template:
#        entity_id: fan.xiaomi_miio_device
#        level: >
#            {% if states.fan.xiaomi_miio_device.attributes.aqi | int > 30 %}
#              15
#            {% elif states.fan.xiaomi_miio_device.attributes.aqi | int > 25 %}
#              10
#            {% elif states.fan.xiaomi_miio_device.attributes.aqi | int > 20 %}
#              5
#            {% elif states.fan.xiaomi_miio_device.attributes.aqi | int > 15 %}
#              3
#            {% elif states.fan.xiaomi_miio_device.attributes.aqi | int > 10 %}
#              1
#            {% else %}
#              0
#            {% endif %}
#    - service: input_select.select_option
#      data_template:
#        entity_id: input_select.xiaomi_miio_device_mode_airpurifier
#        option: '{{ states.fan.xiaomi_miio_device.attributes.mode }}'
#    - service: input_number.set_value
#      data_template:
#        entity_id: input_number.xiaomi_miio_device_favorite_level_airpurifier
#        value: '{{ states.fan.xiaomi_miio_device.attributes.favorite_level }}'

But I get this error on startup (altough it looks like it is working anyway):
2019-05-27 18:52:47 ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.set_state_for_air_purifier_mode_input. Service not found for call_service at pos 1: (ServiceNotFound(...), 'Service input_select.select_option not found')

I just wanna make sure I actually understand kinda what the definitions there are since I will need some more Automations… So is there anything wrong in my part?

My guess is that when the automation is triggered at startup, the input_select component hasn’t loaded yet. Therefore when the automation attempts to call the input_select.select_option service, it’s unavailable.

Examine the log to see the sequence of startup events. The order of the startup sequence will reveal if my guess has any merit.

Or he doesn’t have it defined at all, just add:

  xiaomi_miio_device:
    name: Mode
    icon: mdi:flattr
    options:
     - auto
     - favorite
     - silent
     - idle

That would definitely cause the problem! :slight_smile:

Thanks for the answers.

I can check the log as soon the automation gets loaded again… I just destroyed everything. Seeing my other problem regarding “groups”.

I’m missing this definition at all I guess? But it is working though. I just don’t want to use the auto-mode at all, that’s why I commented the last part out completely.

I added it now and can test as soon as it works again. :slight_smile:

Btw. I got it fixed and it works just fine as of now. It looks like that now:

################################################################################
# Set input states when HA starts or states change
################################################################################

- alias: Set state for air purifier input
  trigger:
    - platform: homeassistant
      event: start
    - platform: state
      entity_id: sensor.xiaomi_miio_device_mode
  action:
    service: input_select.select_option
    data_template:
      entity_id: input_select.xiaomi_miio_device_mode
      option: '{{ states.fan.xiaomi_miio_device.attributes.mode }}'

- alias: Set state for air purifier favorite level input
  trigger:
    - platform: homeassistant
      event: start
    - platform: state
      entity_id: sensor.xiaomi_miio_device_favorite_level
  action:
    service: input_number.set_value
    data_template:
      entity_id: input_number.air_purifier_favorite_level
      value: '{{ states.fan.xiaomi_miio_device.attributes.favorite_level | int }}'

################################################################################
# Inputs
################################################################################

- alias: Air purifier mode
  trigger:
    platform: state
    entity_id: input_select.xiaomi_air_purifier_mode
  action:
    - service: fan.set_speed
      data_template:
        entity_id: fan.xiaomi_miio_device
        speed: '{{ states.input_select.xiaomi_air_purifier_mode.state }}'

- alias: Air purifier favorite level
  trigger:
    platform: state
    entity_id: input_number.xiaomi_air_purifier_favorite_level
  action:
    - service: fan.xiaomi_miio_set_favorite_level
      data_template:
        entity_id: fan.xiaomi_miio_device
        level: '{{ states.input_number.xiaomi_air_purifier_favorite_level.state | int }}'


################################################################################
# Automatic control
################################################################################

- alias: Turn off airpurifier
  trigger:
    platform: numeric_state
    entity_id: fan.xiaomi_miio_device
    value_template: "{{ states.fan.xiaomi_miio_device.attributes.aqi | int }}"
    below: 30
    for: '00:01:00'
  condition:
    - condition: state
      entity_id: fan.xiaomi_miio_device
      state: 'on'
  action:
    service: fan.turn_off
    entity_id: fan.xiaomi_miio_device

- alias: Set favorite fan speed for airpurifier
  trigger:
    platform: time_pattern
    minutes: '/1'
#  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: fan.xiaomi_miio_set_favorite_level
      data_template:
        entity_id: fan.xiaomi_miio_device
        level: >
            {% if states.fan.xiaomi_miio_device.attributes.aqi | int == 600 %}
              12
            {% elif states.fan.xiaomi_miio_device.attributes.aqi | int > 400 %}
              10
            {% elif states.fan.xiaomi_miio_device.attributes.aqi | int > 300 %}
              8
            {% elif states.fan.xiaomi_miio_device.attributes.aqi | int > 150 %}
              6
            {% elif states.fan.xiaomi_miio_device.attributes.aqi | int > 100 %}
              4
            {% elif states.fan.xiaomi_miio_device.attributes.aqi | int > 30 %}
              2
            {% else %}
              0
            {% endif %}
    - service: input_select.select_option
      data_template:
        entity_id: input_select.xiaomi_air_purifier_mode
        option: '{{ states.fan.xiaomi_miio_device.attributes.mode }}'
    - service: input_number.set_value
      data_template:
        entity_id: input_number.xiaomi_air_purifier_favorite_level
        value: '{{ states.fan.xiaomi_miio_device.attributes.favorite_level }}'

cat input_select/airpurifier_mode.yaml

name: Airpurifier Mode
icon: mdi:flattr
options:
 - auto
 - favorite
 - silent
 - idle

Where exactly? :slight_smile: