Re-use condition on multiple automations

Hi all,

First I want to say I really like HomeAssistant. Great Open Source project! :+1:

I have a question regarding Automations within HomeAssistant. I would like clean up my automations by re-using conditions over multiple automations.

So for example in below automation I have this condition. I would like to re-use this, because this condition is the same for my other motion sensors (as good practice t). How should I approach this? I could not find this explained anywhere :slight_smile:

Thx!

- id: '1560195998858'
  alias: LivingRoom Motion Notify
  trigger:
  - entity_id: binary_sensor.livingroom_motion
    from: 'off'
    platform: state
    to: 'on'
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: device_tracker.paulus
        state: not_home
      - condition: state
        entity_id: device_tracker.merel
        state: not_home
      - condition: state
        entity_id: device_tracker.josh
        state: not_home
  action:
  - data:
      message: Motion detected in the Livingroom
      title: '*Alert*'
    service: telegram_bot.send_message

You could create a Template Binary Sensor and then use its value in a single condition:

binary_sensor:
  - platform: template
    sensors:
      nobody_home:
        value_template: >
          {{ !is_state('device_tracker.paulus', 'home') and
             !is_state('device_tracker.merel', 'home') and
             !is_state('device_tracker.josh', 'home') }}
automation:
  - ...
    condition:
      condition: state
      entity_id: binary_sensor.nobody_home
      state: 'on'
    ...
3 Likes

You could also put your device trackers in a group. The group’s state will be home if a single one of the entities is home and will only be not_home if all are not_home. You can even make person: entities out of each individual to take advantage of multiple trackers per person (eg: nmap + owntracks)

person:
  - name: Bob
    id: bob
    device_trackers:
      - device_tracker.bobs_phone_nmap
      - device_tracker.owntracks_bob
  - name: Jane
    id: jane
    device_trackers:
      - device_tracker.janes_iphone
      - device_tracker.janes_iphone_nmap
      - device_tracker.owntracks_jane

group:
  trackers:
    name: Trackers
    entities:
      - person.bob
      - person.jane

automation:
  - id: '1560195998858'
    alias: LivingRoom Motion Notify
    trigger:
      platform: state
      entity_id: binary_sensor.livingroom_motion
      from: 'off'
      to: 'on'
    condition:
      condition: state
      entity_id: group.trackers
      state: not_home
    action:
      service: telegram_bot.send_message
      data:
        message: Motion detected in the Livingroom
        title: '*Alert*'
1 Like