Simplifying automations with states?

I’d like to write sensible automations to support the following logic for our central ventilation unit. (I only need to change the intake airflow.) I’d like to have the following three states:

  • Day mode (150 m²/h)
  • Night mode (100 m²/h)
  • Vacant mode (50 m²/h)

Now I’d like to switch between these values based on the state of the house. The transition between states is shown in the following state diagram.

q

Currently, I have implemented the desired behavior using 5 automations, each corresponding to a transition in the graph above; the Turns vacant transition can be implemented only once, otherwise I’d have 6. For instance, here is the automation for setting the daily mode.

alias: 'Ventilation: Activate day mode'
description: ''
trigger:
  - platform: time
    at: '06:00:00'
condition:
  - condition: numeric_state
    entity_id: zone.home
    above: '0'
action:
  - service: input_select.select_option
    data:
      option: Daily
    target:
      entity_id: input_select.ventilation_mode
mode: single

Is there a better way than this? Can I write less automations (preferably a single one) to achieve the same result?

Can I write stateful automations? For instance, I can easily specify the conditions that define these states. Can HA then figure out which (if any) action is needed?

For example, the day mode may be active during 6AM-11PM if the house is occupied. Similarly the night mode, except the time-span is 11PM-6AM. If the house is vacant, the Vacant mode should be running.

So the question is, can I write automations by specifying states and not transitions?

yes you can.

Watch this tutorial Triggers provided by @smarthomejunkie he will tell you exactly what to do!

1 Like

Thank you. I’ve heard about the choose action, but have not taken a closer look at it yet. With this I might be able to reduce the number of automations to 1.

I still wonder whether automations can be written in a stateful way.

Not really, because automations are triggered by helpers listening for particular event types on the event bus. You can fake it by setting up a Time pattern trigger with a short interval, but continuous polling like that is not encouraged.

If you strategically set the trigger ids to match your input select options and use a couple templates you can simplify your 5 automations into something very compact:

alias: 'Ventilation: All Modes'
description: ''
trigger:
  - platform: time
    at: '06:00:00'
    id: Daily
  - platform: time
    at: '23:00:00'
    id: Nightly
  - platform: numeric_state
    entity_id: zone.home
    above: 0
    id: Occupied
  - platform: numeric_state
    entity_id: zone.home
    below: 1
    id: Vacant
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Occupied
        sequence:
          - service: input_select.select_option
            data:
              option: '{{ iif( 6 <= now().hour < 23, "Daily", "Nightly") }}'
            target:
              entity_id: input_select.ventilation_mode
    default:
      - service: input_select.select_option
        data:
          option: '{{ trigger.id }}'
        target:
          entity_id: input_select.ventilation_mode
mode: single
1 Like

Thank you. I was literally able to take your snippet and run it.

1 Like