How to combine two automations for air purifier

I have prepared two automations to operate the air purifier. I have tested and both work as expected. My knowledge is unfortunately too small and, despite my efforts, it was not possible to create one from the two. Perhaps this is not possible at all. Please suggest if this can be done and how. Thank you. I will learn something from the best :slight_smile:

Main automation:

alias: Air purifier automation
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.daytime
    id: day
  - platform: state
    entity_id:
      - binary_sensor.nighttime
    id: night
condition: []
action:
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: trigger
                id:
                  - day
                enabled: true
              - condition: time
                after: "07:00:10"
                before: "22:00:00"
                weekday:
                  - mon
                  - tue
                  - wed
                  - thu
                  - fri
                  - sat
                  - sun
        sequence:
          - service: switch.turn_on
            target:
              entity_id:
                - switch.air_purifier_led
                - switch.air_purifier_buzzer
            data: {}
          - choose:
              - conditions:
                  - condition: numeric_state
                    entity_id: sensor.air_purifier_pm2_5
                    above: 85
                    below: 1999.9
                sequence:
                  - service: fan.turn_on
                    metadata: {}
                    data:
                      preset_mode: Auto
                    target:
                      entity_id: fan.air_purifier
              - conditions:
                  - condition: numeric_state
                    entity_id: sensor.air_purifier_pm2_5
                    above: 30
                    below: 84.9
                sequence:
                  - service: fan.turn_on
                    metadata: {}
                    data:
                      preset_mode: Silent
                    target:
                      entity_id: fan.air_purifier
              - conditions:
                  - condition: numeric_state
                    entity_id: sensor.air_purifier_pm2_5
                    below: 29.9
                sequence:
                  - service: fan.turn_off
                    metadata: {}
                    data: {}
                    target:
                      entity_id: fan.air_purifier
      - conditions:
          - condition: or
            conditions:
              - condition: trigger
                id:
                  - night
                enabled: true
              - condition: time
                after: "22:00:10"
                before: "07:00:00"
                weekday:
                  - mon
                  - tue
                  - wed
                  - thu
                  - fri
                  - sat
                  - sun
        sequence:
          - service: switch.turn_off
            target:
              entity_id:
                - switch.air_purifier_led
                - switch.air_purifier_buzzer
            data: {}
          - service: fan.set_preset_mode
            metadata: {}
            data:
              preset_mode: Silent
            target:
              entity_id: fan.air_purifier
mode: single

Window open/close control:


alias: Air purifier on/off
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.ikea_window_sensor_opening
    from: "off"
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 0
condition: []
action:
  - service: fan.turn_off
    target:
      entity_id:
        - fan.air_purifier
    data: {}
  - service: automation.turn_off
    target:
      entity_id:
        - automation.air_purifier
    data:
      stop_actions: false
  - wait_for_trigger:
      - platform: state
        entity_id:
          - binary_sensor.ikea_window_sensor_opening
        from: "on"
        to: "off"
        for:
          hours: 0
          minutes: 0
          seconds: 10
    continue_on_timeout: false
  - service: automation.turn_on
    target:
      entity_id:
        - automation.air_purifier
    data: {}
  - condition: numeric_state
    entity_id: sensor.air_purifier_pm2_5
    above: 1
  - service: fan.turn_on
    target:
      entity_id:
        - fan.air_purifier
    data: {}
mode: restart

Stop there, then. Why are you trying to combine them?

Looks like some messy logic though. Your first one:

  • triggers when your daytime or nighttime binaries change in either direction
  • then, if it’s between 0700 and 2200 or your daytime binary changed:
    • turns on the led
    • turns on the buzzer
    • if the particulates are:
      • high, turns the fan to auto
      • medium, turns the fan to silent
      • low, turns the fan off
  • if it’s between 2200 and 0700 or your nighttime binary changed
    • turns off the led and buzzer
    • sets silent mode

What’s the point of the two binary sensors in combination with the times? Wouldn’t a single binary on for day, off for night work? A Schedule helper could do that and can be set up with the UI.

1 Like

To have all-in-one, nice and tidy. And to learn something new. It’s just an idea and I’m not going to insist on it :tipping_hand_man:

@Troon they’re turning the first automation on and off from the second automation, which isn’t great. Otherwise, I’d agree that multiple, smaller automations are better.

@3jam3 you can put the action of the first in a script and rather call it that way if your goal is to minimise duplication.

As Troon touches on, you just need the one trigger in the first automation. Instead of testing for the trigger IDs, just test the binary sensor’s state.

1 Like

I believe it would. I thought about this but was not sure if the time helper would trigger the needed option at, say, 7:00 a.m. Am I to understand that my fears are wrong and that it will trigger?

Yes. Create a Schedule helper in the UI, then:

trigger:
  - platform: state
    entity_id: schedule.daytime

Then instead of testing the trigger ID in your condition, just test the state of the schedule, on for daytime, off for night-time.

@Troon I did what you suggested. Indeed, it is simpler and more compact. It works. By the way, I used the schedule helper for other day/night automations and learned something new. Thanks.
@parautenbach @Troon Now give me a hint whether I should integrate both automations or leave them as two separate ones. Because you have different opinions, guys :thinking:

I don’t think there’s a hard line. :wink: I definitely have some large, combined automations, but that’s usually where I have very complex conditions I don’t want to duplicate, all just to decide (e.g.) to automatically open some door and turn on a light.

The problem often comes in when automations intersect. For example, if another automation will affect the same said light at the same time. You can get strange behaviours.

My general guidance towards smaller but multiple automations is that it tends to be easier to maintain, just like how one should write smaller, purposeful functions when programming.

For automations that either turn something on or off, I’d have it as one and use some templating.

In your case, roughly speaking, to combine the two, you need all the triggers together, as well as all the conditions. When trigger x fires, the conditions for x will already be true, but you want to then check conditions y are protecting you. Vise versa for when trigger y fires. Because, if I read your automations correctly, it’s all just about turning on or off the purifier, so a single automation might be OK. You’ll need to try it an see what you fancy.

1 Like

Thanks for the clarification. I will rethink the matter.
I will ask one more thing. How do you make the selection conditions periodically checked? I have the impression that switching does not always happen according to the set ranges.

          - choose:
              - conditions:
                  - condition: numeric_state
                    entity_id: sensor.air_purifier_pm2_5
                    above: 85
                    below: 1999.9
                sequence:
                  - service: fan.turn_on
                    metadata: {}
                    data:
                      preset_mode: Auto
                    target:
                      entity_id: fan.air_purifier
              - conditions:
                  - condition: numeric_state
                    entity_id: sensor.air_purifier_pm2_5
                    above: 30
                    below: 84.9
                sequence:
                  - service: fan.turn_on
                    metadata: {}
                    data:
                      preset_mode: Silent
                    target:
                      entity_id: fan.air_purifier
              - conditions:
                  - condition: numeric_state
                    entity_id: sensor.air_purifier_pm2_5
                    below: 29.9
                sequence:
                  - service: fan.turn_off
                    metadata: {}
                    data: {}
                    target:
                      entity_id: fan.air_purifier

You can add a numeric state trigger too. Just be aware that it only triggers once the threshold is crossed. If, for example, the condition would be already true (the state is already within your parameters) at HA startup, the trigger won’t fire. For that case, also add a home assistant start trigger.

That’s generally bad practice. Trigger on something rather than time, even if it’s a simple state change of the PM2.5 sensor.

Can I add a helper with a sensor template and switch modes according to the measured values within the set ranges? Can this be done?

Yes. So a template sensor with a state template like this:

{% set p = states('sensor.air_purifier_pm2_5')|float(0) %}
{{ { p >= 85: "high",
     30 <= p < 85: "medium",
     p < 30: "low" }.get(true, "unknown") }}

(which is just a shorter way to write a load of if statements: it’s a dictionary where only one key will be true at a time).

That will check and adjust its state every time the PM2.5 sensor publishes a new state.

I’ve made a helper but I’m stuck. The helper does not have an entity ID and in Preview there is this message: Sensor None has device class 'pm25', state class 'measurement' unit 'µg/m³' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'medium' (<class 'str'>). What do I do now?

Did you chose Template{}

You leave a lot of the boxes blank. Should look like this:

and that will give you sensor.particulate_level.

(edit: ninja’d by @LiQuid_cOOled and they even have dark mode on!)

1 Like

I see now. Thanks. I did it this way which was obviously incorrect:

Always Dark Mode :sunglasses:

Dark mode rules and it’s cooler than cool :cool:

1 Like

This is what the final and working version of my automation looks like. It switches modes as expected. Please comment, as there are probably some errors or inconsistencies in it.

alias: Air purifier automation
description: ""
trigger:
  - platform: state
    entity_id:
      - schedule.nighttime
  - platform: state
    entity_id:
      - sensor.particulate_level
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: schedule.nighttime
            state: "off"
        sequence:
          - service: switch.turn_on
            target:
              entity_id:
                - switch.air_purifier_led
                - switch.air_purifier_buzzer
            data: {}
          - choose:
              - conditions:
                  - condition: state
                    entity_id: sensor.particulate_level
                    state: high
                sequence:
                  - service: fan.turn_on
                    metadata: {}
                    data:
                      preset_mode: Auto
                    target:
                      entity_id: fan.air_purifier
              - conditions:
                  - condition: state
                    entity_id: sensor.particulate_level
                    state: medium
                sequence:
                  - service: fan.turn_on
                    metadata: {}
                    data:
                      preset_mode: Silent
                    target:
                      entity_id: fan.air_purifier
              - conditions:
                  - condition: state
                    entity_id: sensor.particulate_level
                    state: low
                sequence:
                  - service: fan.turn_off
                    metadata: {}
                    data: {}
                    target:
                      entity_id: fan.air_purifier
      - conditions:
          - condition: state
            entity_id: schedule.nighttime
            state: "on"
        sequence:
          - service: switch.turn_off
            target:
              entity_id:
                - switch.air_purifier_led
                - switch.air_purifier_buzzer
            data: {}
          - service: fan.set_preset_mode
            metadata: {}
            data:
              preset_mode: Silent
            target:
              entity_id: fan.air_purifier
mode: single

Question: Is it possible to use When a template triggers instead of the helper and insert the state template there? What is (will be) the difference?

1 Like