Template expanding...not just yet

HI,

I have 2 scripts, for making an announcement:

  announce_daughters:
    sequence:
      service: notify.ios_phone
      data_template:
        title: >
          {{presence_person}} arrived home speech
        message: >
          {{-as_timestamp(now()) | timestamp_custom("%X") }} :
          {{presence_person}} arrived {{presence_location}}.
        data:
          push:
            sound: >
              US-EN-Morgan-Freeman-Daughter-Is-Arriving.wav

and

announce_wife:
  etc

with the same content only the wav file is different.

this automation calls the scripts:

  - service_template: >
      script.announce_{{ 'daughters' if trigger.entity_id in ['group.daughter1', 'group.daughter2', 'group.daughter3',
                                  'group.daughter4'] else 'wife'}}
    data_template: 
      presence_person: "{{trigger.to_state.attributes.friendly_name}}"
      presence_location: "{{trigger.to_state.state}}"

works fine.

Now I want to expand that with a 3d option, but I can’t get it right. Using the above syntax

{{ 'daughters' if trigger.entity_id in ['group.daughter1', 'group.daughter2', 'group.daughter3',
                                  'group.daughter4'] 
 'wife' if  trigger.entity_id == 'device_tracker.wife'
else 'husband' }}

or like this:

  - service_template: >
      script.announce_{% if trigger.entity_id in 
                          ['group.daughter1', 'group.daughter2', 'group.daughter3','group.daughter4']%}daughters
                      {%elif trigger.entity_id == 'device_tracker.wife_presence' %}wife
                      {% else%}husband
                      {%endif%}

What am I doing wrong here? If I test it in the dev-template it shows the correct script…

Maybe I could try:

  {% if trigger.entity_id in ['group.daughter1', 'group.daughter2', 'group.daughter3','group.daughter4']%} script.announce_daughters
  {%elif trigger.entity_id == 'device_tracker.wife_presence' %} script.announce_wife
  {% else%} script.announce_husband
  {%endif%} 

But I like to keep it as compact as possible, and dont like the script.announce_ in each option if I can avoid that

please have a look would you?
would be even cooler if I could make it into 1 script and place the wav file automatically too,

‘daughter’: ‘US-EN-Morgan-Freeman-Daughter-Is-Arriving.wav’
‘wife’: ‘US-EN-Morgan-Freeman-Wife-Is-Arriving.wav’
‘husband’: ‘US-EN-Alexa-Husband-Is-Arriving.wav’

thx!

solved so far using:

  - condition: template
    value_template: >
      {{trigger.entity_id in ['group.daughter1', 'group.daughter2', 'group.daughter3',
                            'group.daughter4', 'device_tracker.wife_presence',
                            'device_tracker.me_presence'] and 
                            trigger.to_state.state == 'home'}}
  - service_template: >
      script.announce_{% if trigger.entity_id in 
                          ['group.daughter1','group.daughter2','group.daughter3','group.daughter4'] %}daughters
                      {% elif trigger.entity_id == 'device_tracker.wife_presence' %}wife
                      {% else %}husband
                      {% endif %}
    data_template: 
      trigger: Presence tracking
      presence_person: '{{trigger.to_state.attributes.friendly_name}}'
      presence_location: '{{trigger.to_state.state}}'

and the 3 separate scripts for daughters, wife and husband:

script:

  announce_daughters:
    sequence:
      service: notify.ios_iphone
      data_template:
        title: >
          {{trigger}}: {{presence_person}} arrived home speech
        message: >
          {{-as_timestamp(now()) | timestamp_custom("%X") }} :
          {{presence_person}} arrived {{presence_location}}.
        data:
          push:
            sound: >
              US-EN-Morgan-Freeman-Daughter-Is-Arriving.wav

    announce_wife:
      etc
    announce_husband:
      etc

as you can see, this is for the situation where trigger.to_state.state == 'home'.

Since this works now, i’d love to expand it some more to cater for state= ‘not_home’ and state= ‘zone’…

please help me getting the correct structure for doing this, because Id hope not to have to use 6 or more separate scripts for that.

I’ve tried to first check a value_template for the state home or not_home and go from there, but Id need passing these variables from automation to script to script and am not sure if that is the best way to do it.(understatement here)

maybe I could start but selecting the correct speech in the script (and cut it out of the automation), of which I would only need 1 then per state:

automation:

action:
  service_template: >
    script.announce_{{trigger.to_state.state}}
  data_template: 
    trigger: Presence tracking test 2
    presence_person: '{{trigger.to_state.attributes.friendly_name}}'
    presence_location: '{{trigger.to_state.state}}'

script:

  announce_home:
    sequence:
      service: notify.ios_iphone
      data_template:
        title: >
          {{trigger}}: {{presence_person}} speech
        message: >
          {{-as_timestamp(now()) | timestamp_custom("%X") }} :
          {{presence_person}} is {{presence_location}}.
        data_template:
          push:
            sound: >
              {% if presence_person in 
                              ['group.daughter1','group.daughter2','group.daughter3','group.daughter4'] %}US-EN-Morgan-Freeman-Daughter-Is-Arriving.wav
                          {% elif trigger.entity_id == 'device_tracker.wife_presence' %}US-EN-Morgan-Freeman-Wife-Is-Arriving.wav
                          {% else %}US-EN-Alexa-Husband-Is-Arriving.wav
                          {% endif %}

tried the mapper technique but the daughters list made it impossible yet to get a correct outcome.

Please @petro and @pnbruckner, allow me to tag you directly here and aks for your further assistance?
thx!

What object is triggering the action?

ultimately the state change of my device_trackers:

ive finally reached like this:

  announce_presence:
    sequence:
      - condition: template
        value_template: >
          {{tostate in ['home','not_home'] or
            fromstate in ['home','not_home'] }}
      - service: notify.ios_ipone
        data_template:
          title: >
            {{trigger}}: {{ states[entityid.split('.')[0]][entityid.split('.')[1]].name }}  - {{ 'Home' if tostate == 'home' else 'Away'}} speech
          message: >
            {{-as_timestamp(now()) | timestamp_custom("%X") }} :
              {{ states[entityid.split('.')[0]][entityid.split('.')[1]].name }} {{ ' arrived home' if is_state(entityid, 'home') else ' left home' }}.
          data:
            push:
              sound: >
                {% if entityid in 
                    ['group.daughter1','group.daughter2','group.daughter3','group.daughter4'] %}
                  {% if is_state(entityid, 'home') %}US-EN-Morgan-Freeman-Daughter-Is-Arriving.wav
                  {% else %}US-EN-Morgan-Freeman-Front-Door-Closed.wav
                  {% endif %}
                {% elif entityid == 'device_tracker.wife_presence' %}
                  {% if is_state(entityid, 'home') %}US-EN-Morgan-Freeman-Wife-Is-Arriving.wav
                  {% else %}US-EN-Morgan-Freeman-Good-Night.wav
                  {% endif %}
                {% else%}
                  {% if is_state(entityid, 'home') %}US-EN-Alexa-Husband-Is-Arriving.wav
                  {% else %}US-EN-Alexa-Good-Night.wav
                  {% endif %}
                {% endif %}

  notify_presence:
    sequence:
      service: notify.m
      data_template:
        title: >
          {{trigger}}: {{ states[entityid.split('.')[0]][entityid.split('.')[1]].name }}  - {{ 'Home' if tostate == 'home' else 'Away'}} notification
        message: >
          {% set name = states[entityid.split('.')[0]][entityid.split('.')[1]].name %}
          {{as_timestamp(now()) | timestamp_custom("%X") }} :
          {% if tostate == 'not_home' %}
            {{-name }} left {{fromstate}}
          {% elif fromstate == 'not_home' %}
            {{-name }} arrived at {{tostate}}
          {% else %}
            {{-name }} left {{fromstate}} and arrived at {{tostate}}
          {% endif %}

and automation:

  - alias: Presence Tracking 2
    id: 'Presence Tracking 2'
    trigger:
      platform: state
      entity_id:
        - group.daughter1
        - group.daughter2
        - group.daughter3
        - group.daughter4
        - device_tracker.me_presence
        - device_tracker.wife_presence
    condition:
      - condition: template
        value_template: >
          {{ trigger.to_state.state is not none and
             trigger.from_state.state is not none and
             trigger.to_state.state != trigger.from_state.state }}
#      - condition: template
#        value_template: >
#          {{trigger.to_state.attributes.longitude != 
#              trigger.from_state.attributes.longitude and 
#            trigger.to_state.attributes.latitude != 
#              trigger.from_state.attributes.latitude }}
      - condition: template
        value_template: >
          {% set zones = states.zone | map(attribute='name')|list %}
          {{trigger.to_state.state in ['home','not_home'] or
            trigger.from_state.state in ['home','not_home'] or
            trigger.to_state.state in zones or 
            trigger.from_state.state in zones}}
      - condition: template
        value_template: >
          {{ (now() - trigger.from_state.last_changed | default(0)).total_seconds() > 
                                            states('input_number.presence_timer')|int }}
    action:
      - condition: template
        value_template: >
          {{ is_state('input_boolean.notify_announcement', 'on') or 
             is_state('input_boolean.notify_presence', 'on') }}
      - service_template: >
          script.{% if is_state('input_boolean.notify_announcement', 'on') %}announce_presence
                 {% elif is_state('input_boolean.notify_presence', 'on') %}notify_presence
                 {% else %}dummy
                 {% endif %}
        data_template: 
          trigger: Presence tracking 2
          entityid: '{{trigger.entity_id}}'
          tostate: '{{trigger.to_state.state}}'
          fromstate: '{{trigger.from_state.state}}'

You just need to get the state from the state object of the device_tracker. If it’s in a zone, it should return the zone name. Example {{ states(“device_tracker.daughter1”) }}