Automation fundamentals

Hello!

I have created many automations successfully, so I understand “how” to create them.

My question is more “procedural” in general.

Example: Motion sensor to turn a light on when motion detected and turn off the light when motion no longer detected.

Should this be broken into two SEPARATE automations? What are the benefits of this method?

Should these two actions be combined into ONE automation and use UNTILs or WHILEs to control flow within the automation? This would mean the automation would continue to run “in the background” until some condition forces a STOP.

Thanks!

Making it messier and harder to find your automations.
Pulling your hair when you don’t find the automations is also a cost saving (no need to cut your hair).

Yes. I think so.

No…
You can set up several triggers in the same automation and use the choose to set an action based on the trigger.

I just picked the first automation I have with this setup:

alias: Avfuktaren stäng av vid % eller tid
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.avfuktaren_switch
    to: "on"
    for:
      hours: 11
      minutes: 0
      seconds: 0
    id: "off"
  - platform: template
    value_template: >-
      {{ states('sensor.tvattstugan_humidity') | float <=
      states('input_number.tvattstuga_fuktighet') | int +1 }}
    for:
      hours: 0
      minutes: 5
      seconds: 0
    id: "off"
  - platform: time
    at: input_datetime.avfuktaren
    id: "on"
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - "off"
          - condition: state
            entity_id: switch.avfuktaren_switch
            state: "on"
        sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.avfuktaren_switch
            data: {}
          - condition: template
            value_template: >-
              {{ as_datetime(state_attr('input_datetime.avfuktaren',
              'timestamp')) <= now() - timedelta(hours = 2) }}
          - service: input_datetime.set_datetime
            target:
              entity_id: input_datetime.avfuktaren
            data:
              timestamp: "{{ now().timestamp() + 3600 }}"

      - conditions:
          - condition: trigger
            id:
              - "on"
          - condition: template
            value_template: >-
              {{ states('sensor.tvattstugan_humidity') | float >=
              states('input_number.tvattstuga_fuktighet') | int+2 }}
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.avfuktaren_switch
            data: {}
mode: single

So each trigger has a trigger id and the actions part is split up with two choose.

1 Like

Or…

At the “best practices” level, I think the answer is “what method seems the most intuitive to you”. There is an old saying in systems development that 80% of the cost of any system is in its maintenance. So you want to make it as easy as possible to find / fix / improve things down the road. Where would you look for it first? That is where you should put it!

To better clarify my question, does a large, complex automation (> 50 lines) eat more system resources than a small < 20 line automation?

Is there a point where one would start to be constrained (read: concerned) about system resources?

Agreed…having a single automation “Driveway Motion” with all aspects reporting, lights, motion etc., makes great sense!

How many automations are too many? Are smaller automations more “agile” than larger, more complex ones?

It makes no difference - they all go in the same yaml file. A long automation may well use more resources than several short ones simply because of all the branches and conditions it requires.

As @PecosKidd says, ease of maintenance is the important thing. In six months you won’t remember how the damn thing worked - take whatever approach is going to make things easier for you. :grin:

On that subject, good organisation and annotation can make a lot of difference: