Trying to figure out a data_template usage

alias: 'Notification when someone arrives'
trigger:
  - platform: state
    entity_id: device_tracker.dereks_phone
    from: 'not_home'
    to: 'home'
  - platform: state
    entity_id: device_tracker.kalees_phone
    from: 'not_home'
    to: 'home'
  - platform: state
    entity_id: device_tracker.nolans_phone
    from: 'not_home'
    to: 'home'
  - platform: state
    entity_id: device_tracker.todds_phone
    from: 'not_home'
    to: 'home'
  - platform: state
    entity_id: device_tracker.kims_phone
    from: 'not_home'
    to: 'home'
  - platform: state
    entity_id: device_tracker.johns_phone
    from: 'not_home'
    to: 'home'
  - platform: state
    entity_id: device_tracker.briannas_phone
    from: 'not_home'
    to: 'home'
  - platform: state
    entity_id: device_tracker.brandons_phone
    from: 'not_home'
    to: 'home'
condition:
  condition: or
  conditions:
    - condition: state
      entity_id: device_tracker.dereks_phone
      state: 'not_home'
      for:
        minutes: 10
    - condition: state
      entity_id: device_tracker.kalees_phone
      state: 'not_home'
      for:
        minutes: 10
    - condition: state
      entity_id: device_tracker.nolans_phone
      state: 'not_home'
      for:
        minutes: 10
    - condition: state
      entity_id: device_tracker.todds_phone
      state: 'not_home'
      for:
        minutes: 10
    - condition: state
      entity_id: device_tracker.kims_phone
      state: 'not_home'
      for:
        minutes: 10
    - condition: state
      entity_id: device_tracker.johns_phone
      state: 'not_home'
      for:
        minutes: 10
    - condition: state
      entity_id: device_tracker.briannas_phone
      state: 'not_home'
      for:
        minutes: 10
    - condition: state
      entity_id: device_tracker.brandons_phone
      state: 'not_home'
      for:
        minutes: 10
action:
  - service: notify.pushbullet
    data_template: >
      {% if trigger.entity_id == 'device_tracker.kalees_phone' %}
        message: 'Kalee has arrived!'
        target: device/Galaxy S7
      {% elif trigger.entity_id == 'device_tracker.nolans_phone' %}
        message: 'Nolan has arrived!'
        target: device/Galaxy S7
      {% elif trigger.entity_id == 'device_tracker.todds_phone' %}
        message: 'Todd has arrived!'
        target: device/Galaxy S7
      {% elif trigger.entity_id == 'device_tracker.kims_phone' %}
        message: 'Kim has arrived!'
        target: device/Galaxy S7
      {% elif trigger.entity_id == 'device_tracker.johns_phone' %}
        message: 'John has arrived!'
        target: device/Galaxy S7
      {% elif trigger.entity_id == 'device_tracker.briannas_phone' %}
        message: 'Brianna has arrived!'
        target: device/Galaxy S7
      {% elif trigger.entity_id == 'device_tracker.brandons_phone' %}
        message: 'Brandon has arrived!'
        target: device/Galaxy S7
      {% endif %}

So this is kind of long haha but what I’m going for is obvious, but I run into config errors for the action section because of the data template. Not sure how else to do it…

Use if/else statement like this (with “is_state”):

      -  service: media_player.play_media
         data_template:
           entity_id: >
            {% if is_state("input_select.chromecast_radio", "Livingroom") %} media_player.ca_livingroom
            {% elif is_state("input_select.chromecast_radio", "Hall") %} media_player.ca_hall
            {% elif is_state("input_select.chromecast_radio", "Bedroom") %} media_player.bed_room
            {% elif is_state("input_select.chromecast_radio", "Bathroom") %} media_player.ca_bathroom
            {% elif is_state("input_select.chromecast_radio", "Everywhere") %} media_player.home_group
            {% endif %}

You can simplify your automation in a few ways:

  1. You can put all the entity_id in one trigger:
trigger:
  - platform: state
    entity_id: device_tracker.dereks_phone, device_tracker.kalees_phone, ...
    to: 'home'
  1. Same for condition.
  2. Finally, modify the action as below (see an example here):
  action:
    - service: notify.pushbullet
      data_template:
        message: >
          {% if trigger.entity_id == 'device_tracker.kalees_phone' %}
             message: 'Kalee has arrived!'
             target: device/Galaxy S7
          {% elif trigger.entity_id == 'device_tracker.nolans_phone' %}
             message: 'Nolan has arrived!'
             target: device/Galaxy S7
             ...
          {% endif %}
        target: device/Galaxy S7
1 Like

Awesome! That’s helped. I seem to still have some problems with my condition though (and to be honest I’m not sure if I’ll even need it).

condition:
  condition: or
  conditions:
    - condition: state
      entity_id: device_tracker.dereks_phone, device_tracker.kalees_phone, device_tracker.nolans_phone, device_tracker.todds_phone, device_tracker.kims_phone, device_tracker.johns_phone, device_tracker.briannas_phone, device_tracker.brandons_phone
      state: 'not_home'
      for:
        minutes: 10

Basically trying to make it so I’m not notified if someone goes out to their car for a minute and comes back.

You can (or should) remove the condition block (the logic is not clear). The condition block is checking for state to be not_home, but the automation is triggered when the state is `home. So, the automation will never trigger.

Gotcha, that’s what I feared. Is there another way to determine this condition that you know of? Or am I doomed to be notified of every wifi connection?

If you are using this for presence detection, see my approach that I shared here.

Thanks for your help I’ll look into that.

I currently have a couple automations setup which trigger an IFTTT action based on entering on leaving zones like this:

- alias: Arrive at Work
  trigger:
    platform: zone
    entity_id: device_tracker.bob_s8
    zone: zone.work
    event: enter
  action:
    service: ifttt.trigger
    data:
      event: work_arrive
      value1: "Bob arrived at work"      
      
- alias: Leave Work
  trigger:
    platform: zone
    entity_id: device_tracker.bob_s8
    zone: zone.work
    event: leave
  action:
    service: ifttt.trigger
    data:
      event: work_leave
      value1: "Bob left work" 

And 2 more automations for the home zone.

I’m trying to combine this into 1 automation.
How can I implement a second condition in the IF-statement?

Example: I want to add values to the action based on 2 conditions in the trigger: zone and state (enter or leave).

This is what I have now:

- alias: Presence Log
  trigger:
    platform: zone
      entity_id: device_tracker.bob_s8
      zone: zone.home, zone.work
      event: enter, leave
  action:
    service: ifttt.trigger
    data_template:
      event: >
        {% if trigger.zone == "zone.work" %}  ### This is where I have to put the second condition which checks wether the event is "enter" or "leave"
            event: work_arrive
            value1: "Bob arrived at work"
        {% elif trigger.zone == "zone.work" %} 
            event: work_leave
            value1: "Bob left work"
        {% elif trigger.zone == "zone.home" %}
            event: home_arrive
            value1: "Bob arrived at home"
        {% elif trigger.zone == "zone.home" %}
            event: home_leave
            value1: "Bob left home"
        {% endif %}      

This:

- alias: Presence Log
  trigger:
    - platform: zone
      entity_id: device_tracker.bob_s8
      zone: zone.home, zone.work
      event: enter, leave
  action:
    service: ifttt.trigger
    data_template:
      event: >
          {% if trigger.zone == "zone.work" and trigger.event == "enter" %}
            event: work_arrive
            value1: "Bob arrived at work"
          {% elif trigger.zone == "zone.work" and trigger.event == "leave" %}
            event: work_leave
            value1: "Bob left work"           
          {% elif trigger.zone == "zone.home" and trigger.event == "enter" %}
            event: home_arrive
            value1: "Bob arrived at home"
          {% elif trigger.zone == "zone.home" and trigger.event == "leave" %}
            event: home_leave
            value1: "Bob left home"
          {% endif %}

Gives me the following error:

2017-05-11 20:36:09 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: Entity ID zone.home, zone.work is an invalid entity id for dictionary value @ data['trigger'][0]['zone']. Got None