Combine Two Automations into One

I have been using Home Assistant for about a year and haven’t gotten much into automation and some of the advance things HA can do. I want to start to make my house feel alive with some automation. My programming experience is next to nothing, but I can follow some code and change it to my liking.

What I was wondering is if I could take these two automatons and combine them into one? I would like my house to turn on lights if one of us (me and my wife) gets home and the other is not. I believe I have it working as two separate automation, but I thought if I could learn how to do it as one, it would help with my scripting on HA.

  - alias: Turn on Lights when Amanda Gets Home
    trigger:
      platform: state
      entity_id: device_tracker.wilsonama_amandasphone
      to: 'home'
    condition: 
      condition: template
      value_template: "{{ not is_state('device_tracker.wilsonbry_bryansphone', 'home')}}"
    action:
      service: homeassistant.turn_on
      entity_id: switch.cabinet_lights, switch.tplink_sp_01, switch.tplink_sp_03

  - alias: Turn on Lights when Bryan Gets Home
    trigger:
      platform: state
      entity_id: device_tracker.wilsonbry_bryansphone
      to: 'home'
    condition: 
      condition: template
      value_template: "{{ not is_state('device_tracker.wilsonama_amandasphone', 'home')}}"
    action:
      service: homeassistant.turn_on
      entity_id: switch.cabinet_lights, switch.tplink_sp_01, switch.tplink_sp_03

This may help you:

- alias: Location Change
  initial_state: True

  trigger:
    - platform: state
      entity_id:
        - device_tracker.johns_iphone
        - device_tracker.kimberleys_iphone
     
  condition:
    - condition: template
      value_template: >-
        {{ trigger.from_state.state is defined and
           trigger.to_state.state is defined and
           trigger.to_state.state != trigger.from_state.state }}
  
  action:
    - wait_template: "{{ is_state('script.notify_ios_engine' , 'off') }}"
 
    - service: script.notify_ios_engine    
      data_template:
        message: >-
          {%- set name = trigger.to_state.attributes.friendly_name -%}
          {%- set to_state = trigger.to_state.state -%}
          {%- set from_state = trigger.from_state.state -%}
          {%- if to_state == 'not_home' -%}
            {{ name }} departed {{from_state}}
          {%- elif from_state == 'not_home' -%}
            {{ name }} arrived at {{to_state}}
          {%- else -%}
            {{ name }} left {{from_state}} and arrived at {{to_state}}
          {%- endif -%}
        who: >-
          {%- if trigger.entity_id == 'device_tracker.kimberleys_iphone' -%}
            john
          {%- elif trigger.entity_id == 'device_tracker.johns_iphone' -%}
            kim
          {%- endif -%}
        notify: "{{ states('input_boolean.notify_locations') }}"

EDIT: I Just relalized the complication you have. You need to check the other device, not the active one. This is quite complicated to do. You have 2 options.

  1. Build an if statement for each device and check the ‘other’ devices. This will get complicated when adding devices.
  2. Make a group, check all other devices inside that group. This will also get complicated but adding a device simply means you just add the device to your trigger and the group. Nothing else.

Personally, I’d choose option 2. Here is option 2 all fleshed out.

group

group:
  people:
    entities:
      - device_tracker.wilsonama_amandasphone
      - device_tracker.wilsonbry_bryansphone

automation

  - alias: Turn on Lights when someone gets home
    trigger:
      platform: state
      entity_id: 
        - device_tracker.wilsonama_amandasphone
        - device_tracker.wilsonbry_bryansphone
      to: 'home'
    condition: 
      condition: template
      value_template: >
        {% set entity_ids = states_attr('group.people','entity_id') %}
        {% set state_objects_in_group = states.device_tracker | selectattr('entity_id', 'in', entity_ids) %}
        {% set state_objects_that_arent_trigger = state_objects_in_group | selectattr('entity_id', '!=', trigger.entity_id) %}
        {% set state_objects_that_are_not_home = state_objects_that_arent_trigger | selectattr('state','!=','home') %}
        {{ state_objects_that_are_not_home | length > 0 }}
    action:
      service: homeassistant.turn_on
      entity_id: switch.cabinet_lights, switch.tplink_sp_01, switch.tplink_sp_03

So what this does is it looks at the group. It gets the grouped devices as ‘state objects’. This means we can pull info out of them. Then it gets all the state objects that are in the group that aren’t the one that triggered the automation. Then it see’s which ones aren’t home. If there are any that aren’t home, it resolves True.

I recommend that you just have two triggers (the device trackers arriving home), and template condition that is value_template: {{ states("device_tracker.wilsonama_amandasphone") != states("device_tracker.wilsonbry_bryansphone") }}

This very succinctly checks if your two device trackers have the same state. If so, the automation will not trigger, because you’re both home.

1 Like

Dolores,

Thanks so much! This does what I want. Didn’t even think about states not being equal and if I did, wouldn’t have know to use != (did a little googling to understand). Thanks again.

1 Like

Thanks for your reply. Could you tell me what the % in front of the set is doing?

This is just punctuation for jinja. It {%, {{ identify the start of a template. %}, }} identify the end of a template.

{% %} is a template line that doesn’t return anything from inside the brackets. It will however contain whitespace, I.E spaces before and after (the template line) as well as carriage returns.

{{ }} is a template line that returns a string. It will contain whitespace, I.E spaces before and after (the template line) as well as carriage returns.

{%- %} is a template line that doesn’t return anything from inside the brackets. It will remove whitespace before.

{% -%} is a template line that doesn’t return anything from inside the brackets. It will remove whitespace after.

{{- }} is a template line that returns a string. It will remove whitespace before.

{{ -}} is a template line that returns a string. It will remove whitespace after.

Basically. If you are outputting a result, use {{}} if you aren’t outputing anything and just executing a line, use {%%}

2 Likes