Conditionally execute an action or conditionally execute another action or default to else

I’m just starting to dabble in automation. I need to create an automation for the air purifier. Unfortunately, I can’t get to grips with cascading conditions. It involves a scenario like this:

  1. If PM2.5 numeric value is above 85 and below 2000, set fan preset mode to Auto. If below 85, go to 2.
  2. If the PM2.5 value is above 30 and below 85, set fan preset mode to Silent. If below 30, go to 3.
  3. If the PM2.5 value is below 30, set fan off.
    With one “conditionally execute” it works as expected. Please direct me to the right path of thinking :slight_smile: Thanks :pray:.

Something like this?

description: ""
mode: single
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.ac3829_living_room_pm2_5
    above: 85
    below: 2000
    id: auto
  - platform: numeric_state
    entity_id:
      - sensor.ac3829_living_room_pm2_5
    above: 30
    below: 84.9
    id: "2"
  - platform: numeric_state
    entity_id:
      - sensor.ac3829_living_room_pm2_5
    below: 29.9
    id: "off"
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - auto
        sequence:
          - service: fan.set_preset_mode
            metadata: {}
            data:
              preset_mode: auto
            target:
              entity_id: fan.ac3829_living_room
      - conditions:
          - condition: trigger
            id:
              - "2"
        sequence:
          - service: fan.set_preset_mode
            metadata: {}
            data:
              preset_mode: speed 2
            target:
              entity_id: fan.ac3829_living_room
      - conditions:
          - condition: trigger
            id:
              - "off"
        sequence:
          - service: fan.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: fan.ac3829_living_room

1 Like

Something like this definitely! I lost perspective as I desperately tried to achieve my goal. I did not think of using the Choose between actions option. Thank you very much, it works as expected.
I will now work on adding a condition to turn off the air purifier when the window is open and turn it back on when closed. And then there’s the night mode (silent) with the screen off to add too. Separately, both of these automations work. Can I take advantage of your kindness once again if I can’t manage?

You can just add these as triggers to the same automation
trigger → window_open → id = off (so no need to add an additional action
trigger → sunset → id = sunset --action = display off, silent mode
trigger → sunrise → id = sunrise --action = display on, normal mode

Just mark as solution

I have. Thanks again.
By the way, I’ve been looking through a lot of threads and advice on air purifier automation. Nowhere have I come across a solution as brilliantly effective as yours. Mostly the solution proposals were so confusing and complex that it was difficult to grasp.

If your fan’s preset modes have these values:

  • auto
  • 2
  • off

Then it’s possible to reduce the automation’s actions to a single service call (no choose statement required).

alias: example
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.ac3829_living_room_pm2_5
    above: 85
    below: 2000
    id: "auto"
  - platform: numeric_state
    entity_id:
      - sensor.ac3829_living_room_pm2_5
    above: 30
    below: 84.9
    id: "2"
  - platform: numeric_state
    entity_id:
      - sensor.ac3829_living_room_pm2_5
    below: 29.9
    id: "off"
condition: []
action:
  - service: fan.set_preset_mode
    data:
        preset_mode: "{{ trigger.id }}"
    target:
        entity_id: fan.ac3829_living_room

If off is not one of the preset modes, then the automation’s action requires a simple if-then statement.

action:
  - if: "{{ trigger.id != 'off' }}"
    then:
      - service: fan.set_preset_mode
        data:
          preset_mode: "{{ trigger.id }}"
        target:
          entity_id: fan.ac3829_living_room
    else:
      - service: fan.turn_off
        target:
          entity_id: fan.ac3829_living_room

@123 Yeah, I was thinking about that, but mine does not…
I have auto/speed 2, but there is no preset for ‘off’ nor ‘silent’

I didn’t want to over complicate it :stuck_out_tongue:

Thanks for your hint. Interesting idea. However, it doesn’t work even when I changed to if-then, because off is not a valid preset mode. The purifier only switches between auto and silent. It never turns off the fan. In Traces there is a comment: Error: Preset mode auto is not valid, valid preset modes are: Auto, Silent, Favorite. It refers to these lines:

service: fan.set_preset_mode
data:
  preset_mode: '{{ trigger.id }}'
target:
  entity_id: fan.air_purifier

Puzzling, as Auto and Silent are, after all, valid modes.

Based on your code, I created an automation. It works. However, I have doubts about the optimisation. Could you comment, please?

alias: aceindy Test Automation
description: ""
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.air_purifier_pm2_5
    above: 85
    below: 2000
    id: auto
  - platform: numeric_state
    entity_id:
      - sensor.air_purifier_pm2_5
    above: 30
    below: 84.9
    id: silent
  - platform: numeric_state
    entity_id:
      - sensor.air_purifier_pm2_5
    below: 29.9
    id: "off"
  - platform: state
    entity_id:
      - binary_sensor.ikea_window_sensor_opening
    from: "off"
    to: "on"
    id: "off"
  - platform: state
    entity_id:
      - binary_sensor.daytime
    id: day
  - platform: state
    entity_id:
      - binary_sensor.nighttime
    id: night
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - auto
          - condition: state
            entity_id: binary_sensor.daytime
            state: "on"
        sequence:
          - service: fan.set_preset_mode
            metadata: {}
            data:
              preset_mode: Auto
            target:
              entity_id: fan.air_purifier
          - type: turn_on
            device_id: f8c5b37af6f13b84b4ace9553cee5ab1
            entity_id: 3dec1a10a22dbc580a9a252be5d70e86
            domain: switch
          - type: turn_on
            device_id: f8c5b37af6f13b84b4ace9553cee5ab1
            entity_id: 17eb95a7723158559c4ee31a2f9ae61d
            domain: switch
      - conditions:
          - condition: trigger
            id:
              - silent
          - condition: state
            entity_id: binary_sensor.daytime
            state: "on"
        sequence:
          - service: fan.set_preset_mode
            metadata: {}
            data:
              preset_mode: Silent
            target:
              entity_id: fan.air_purifier
          - type: turn_on
            device_id: f8c5b37af6f13b84b4ace9553cee5ab1
            entity_id: 3dec1a10a22dbc580a9a252be5d70e86
            domain: switch
          - type: turn_on
            device_id: f8c5b37af6f13b84b4ace9553cee5ab1
            entity_id: 17eb95a7723158559c4ee31a2f9ae61d
            domain: switch
      - conditions:
          - condition: trigger
            id:
              - night
        sequence:
          - service: fan.set_preset_mode
            metadata: {}
            data:
              preset_mode: Silent
            target:
              device_id: f8c5b37af6f13b84b4ace9553cee5ab1
          - type: turn_off
            device_id: f8c5b37af6f13b84b4ace9553cee5ab1
            entity_id: 3dec1a10a22dbc580a9a252be5d70e86
            domain: switch
          - type: turn_off
            device_id: f8c5b37af6f13b84b4ace9553cee5ab1
            entity_id: 17eb95a7723158559c4ee31a2f9ae61d
            domain: switch
      - conditions:
          - condition: trigger
            id:
              - "off"
        sequence:
          - service: fan.turn_off
            metadata: {}
            data: {}
            target:
              device_id: f8c5b37af6f13b84b4ace9553cee5ab1
mode: single

I have doubts about the window sensor as well. Now, when the window is open and closed, the air purifier starts working after a short time. This is what happens when the PM2.5 level fluctuates. How do I make the air purifier run according to the status of the time helper when the sensor state changes and prevent it from running until the window is closed? The solutions found on forums are inconsistent and highly confusing. I’m probably abusing your kindness… :man_bowing:

Fixed by the second example I had posted above.

Capitalize the first letter of auto in the trigger’s id variable because that’s the exact value that is used in the templated service call.

Anyways, I won’t continue to promote the alternative. It’s clear you prefer to use choose.

Nothing of the sort! I just try one approach and the other. As I mentioned, I’m just learning. So I appreciate your suggestion as well. Except that I am stuck with an error that is all too obvious, but I didn’t notice it. Thanks for correcting me.

Just one tip…

Refrain from setting devices…use service instead

(makes it more readable too :wink: )

2 Likes