Trouble with Variables in Automation

I’m trying to make use of variables in an automation, but they don’t appear to be getting set. What am I doing wrong? The following yaml was generated using the visual editor.

alias: Announce Car
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.2018_honda_odyssey_car_info
    id: 2018_honda_odyssey
    from: null
    to: null
  - platform: state
    entity_id:
      - sensor.2013_audi_s4_car_info
    id: 2013_audi_s4
    from: null
    to: null
  - platform: state
    entity_id:
      - sensor.2012_mini_cooper_clubman_car_info
    id: 2012_mini_cooper
    from: null
    to: null
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - 2018_honda_odyssey
        sequence:
          - service: homeassistant.update_entity
            data: {}
            target:
              entity_id:
                - sensor.google_travel_time_honda_odyssey
          - wait_for_trigger:
              - platform: state
                entity_id:
                  - sensor.google_travel_time_honda_odyssey
                attribute: origin_addresses
            timeout:
              hours: 0
              minutes: 0
              seconds: 10
              milliseconds: 0
          - variables:
              car_name: minivan
              car_info: "{{ states( 'sensor.2018_honda_odyssey_car_info' ) }}"
              car_location_address: >-
                {% if states( 'device_tracker.2018_honda_odyssey' ) !=
                "not_home" %}
                    "{{ states( 'device_tracker.2018_honda_odyssey' ) }}"
                    {% else %}
                    "{{ state_attr('sensor.google_travel_time_honda_odyssey',
                        'origin_addresses')[0].split(",")[0] }}"
                    {% endif %}
      - conditions:
          - condition: trigger
            id:
              - 2013_audi_s4
        sequence:
          - service: homeassistant.update_entity
            data: {}
            target:
              entity_id: sensor.google_travel_time_audi
          - wait_for_trigger:
              - platform: state
                entity_id:
                  - sensor.google_travel_time_audi
                attribute: origin_addresses
            timeout:
              hours: 0
              minutes: 0
              seconds: 10
              milliseconds: 0
          - variables:
              car_name: audi
              car_info: "{{ states( 'sensor.2013_audi_s4_car_info' ) }}"
              car_location_address: |-
                {% if states( 'device_tracker.2013_audi_s4' ) != "not_home" %}
                    {{ states( 'device_tracker.2013_audi_s4' ) }}
                    {% else %}
                    {{ state_attr('sensor.google_travel_time_audi',
                        'origin_addresses')[0].split(",")[0] }}
                    {% endif %}
      - conditions:
          - condition: trigger
            id:
              - 2012_mini_cooper
        sequence:
          - service: homeassistant.update_entity
            data: {}
            target:
              entity_id: sensor.google_travel_time_mini
          - wait_for_trigger:
              - platform: state
                entity_id:
                  - sensor.google_travel_time_mini
                attribute: origin_addresses
            timeout:
              hours: 0
              minutes: 0
              seconds: 10
              milliseconds: 0
          - variables:
              car_name: mini cooper
              car_info: "{{ states( 'sensor.2012_mini_cooper_clubman_car_info' ) }}"
              car_location_address: >-
                {% if states( 'device_tracker.2012_mini_cooper_clubman' ) !=
                "not_home" %}
                    {{ states( 'device_tracker.2012_mini_cooper_clubman' ) }}
                    {% else %}
                    {{ state_attr('sensor.google_travel_time_mini',
                        'origin_addresses')[0].split(",")[0] }}
                    {% endif %}
  - parallel:
      - service: notify.alexa_media_alexa_man_cave
        data:
          message: The {{ car_name }} is {{ car_info }} at {{ car_location_address }}
      - service: notify.alexa_media_living_room
        data:
          message: The {{ car_name }} is {{ car_info }} at {{ car_location_address }}
      - service: notify.alexa_media_master_bedroom
        data:
          message: The {{ car_name }} is {{ car_info }} at {{ car_location_address }}
mode: single

I had read that, and feared that might be the case. I thought I had tried moving things around, but I must have messed something up in the process. This adjustment seems to work, but I don’t like how I have to duplicate the code. Is there any other option to keep the code cleaner?

alias: Announce Car
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.2018_honda_odyssey_car_info
    id: 2018_honda_odyssey
    from: null
    to: null
  - platform: state
    entity_id:
      - sensor.2013_audi_s4_car_info
    id: 2013_audi_s4
    from: null
    to: null
  - platform: state
    entity_id:
      - sensor.2012_mini_cooper_clubman_car_info
    id: 2012_mini_cooper
    from: null
    to: null
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - 2018_honda_odyssey
        sequence:
          - service: homeassistant.update_entity
            data: {}
            target:
              entity_id:
                - sensor.google_travel_time_honda_odyssey
          - wait_for_trigger:
              - platform: state
                entity_id:
                  - sensor.google_travel_time_honda_odyssey
                attribute: origin_addresses
            timeout:
              hours: 0
              minutes: 0
              seconds: 10
              milliseconds: 0
          - variables:
              car_name: minivan
              car_info: "{{ states( 'sensor.2018_honda_odyssey_car_info' ) }}"
              car_location_address: >-
                {% if states( 'device_tracker.2018_honda_odyssey' ) !=
                "not_home" %}
                    "{{ states( 'device_tracker.2018_honda_odyssey' ) }}"
                    {% else %}
                    "{{ state_attr('sensor.google_travel_time_honda_odyssey',
                        'origin_addresses')[0].split(",")[0] }}"
                    {% endif %}
          - parallel:
              - service: notify.alexa_media_alexa_man_cave
                data:
                  message: The {{ car_name }} is {{ car_info }} at {{ car_location_address }}
              - service: notify.alexa_media_living_room
                data:
                  message: The {{ car_name }} is {{ car_info }} at {{ car_location_address }}
              - service: notify.alexa_media_master_bedroom
                data:
                  message: The {{ car_name }} is {{ car_info }} at {{ car_location_address }}
      - conditions:
          - condition: trigger
            id:
              - 2013_audi_s4
        sequence:
          - service: homeassistant.update_entity
            data: {}
            target:
              entity_id: sensor.google_travel_time_audi
          - wait_for_trigger:
              - platform: state
                entity_id:
                  - sensor.google_travel_time_audi
                attribute: origin_addresses
            timeout:
              hours: 0
              minutes: 0
              seconds: 10
              milliseconds: 0
          - variables:
              car_name: audi
              car_info: "{{ states( 'sensor.2013_audi_s4_car_info' ) }}"
              car_location_address: |-
                {% if states( 'device_tracker.2013_audi_s4' ) != "not_home" %}
                    {{ states( 'device_tracker.2013_audi_s4' ) }}
                    {% else %}
                    {{ state_attr('sensor.google_travel_time_audi',
                        'origin_addresses')[0].split(",")[0] }}
                    {% endif %}
          - parallel:
              - service: notify.alexa_media_alexa_man_cave
                data:
                  message: The {{ car_name }} is {{ car_info }} at {{ car_location_address }}
              - service: notify.alexa_media_living_room
                data:
                  message: The {{ car_name }} is {{ car_info }} at {{ car_location_address }}
              - service: notify.alexa_media_master_bedroom
                data:
                  message: The {{ car_name }} is {{ car_info }} at {{ car_location_address }}
      - conditions:
          - condition: trigger
            id:
              - 2012_mini_cooper
        sequence:
          - service: homeassistant.update_entity
            data: {}
            target:
              entity_id: sensor.google_travel_time_mini
          - wait_for_trigger:
              - platform: state
                entity_id:
                  - sensor.google_travel_time_mini
                attribute: origin_addresses
            timeout:
              hours: 0
              minutes: 0
              seconds: 10
              milliseconds: 0
          - variables:
              car_name: mini cooper
              car_info: "{{ states( 'sensor.2012_mini_cooper_clubman_car_info' ) }}"
              car_location_address: >-
                {% if states( 'device_tracker.2012_mini_cooper_clubman' ) !=
                "not_home" %}
                    {{ states( 'device_tracker.2012_mini_cooper_clubman' ) }}
                    {% else %}
                    {{ state_attr('sensor.google_travel_time_mini',
                        'origin_addresses')[0].split(",")[0] }}
                    {% endif %}
          - parallel:
              - service: notify.alexa_media_alexa_man_cave
                data:
                  message: The {{ car_name }} is {{ car_info }} at {{ car_location_address }}
              - service: notify.alexa_media_living_room
                data:
                  message: The {{ car_name }} is {{ car_info }} at {{ car_location_address }}
              - service: notify.alexa_media_master_bedroom
                data:
                  message: The {{ car_name }} is {{ car_info }} at {{ car_location_address }}
mode: single

Since all your actions are the same, just with different values; attach the appropriate value to variables for each trigger. Then you can collapse all the actions into a single sequence with a single scope instead of using a Choose.

The only major difference will be the Wait for trigger since templates can’t be used in a State trigger. If you think the waiting is actually necessary, there are a couple options:

  1. Use a static delay as I have below.
  2. If it absolutely has to be that specific attribute you would need to set up a template sensor for each one.

There’s no point in parallelizing the Alexa Media calls, and it can actually cause messages to not be announced. If you really need the announcements to be as synchronous as possible the best bet is to create a speaker group in the Alexa app and call its notify service.

alias: Announce Car
description: ""
trigger:
  - platform: state
    entity_id: sensor.2018_honda_odyssey_car_info
    id: 2018_honda_odyssey
    to: null
    variables:
      car_name: minivan
      sensor: sensor.google_travel_time_honda_odyssey
      tracker: device_tracker.2018_honda_odyssey
  - platform: state
    entity_id: sensor.2013_audi_s4_car_info
    id: 2013_audi_s4
    to: null
    variables:
      car_name: audi
      sensor: sensor.google_travel_time_audi
      tracker: device_tracker.2013_audi_s4
  - platform: state
    entity_id: sensor.2012_mini_cooper_clubman_car_info
    id: 2012_mini_cooper
    to: null
    variables:
      car_name: mini cooper
      sensor: sensor.2012_mini_cooper_clubman_car_info
      tracker: device_tracker.2012_mini_cooper_clubman
condition: []
action:
  - service: homeassistant.update_entity
    data: {}
    target:
      entity_id: "{{ sensor }}"
  - delay: 10
  - variables: 
      car_info: "{{ states(trigger.entity_id) }}"
      car_location_address: >-
        {% set state = states( tracker) %}
        {{ state if state != "not_home" else state_attr(sensor, 'origin_addresses')[0].split(",")[0] }}
  - service: notify.alexa_media
    data:   
      message: "The {{ car_name }} is {{ car_info }} at {{ car_location_address }}"
      target:
        - media_player.man_cave
        - media_player.living_room
        - media_player.master_bedroom
mode: single
1 Like

The only reason I had the delay in there the way I had it was because I wanted to make sure the update-entity call completed before I pulled the location of the car. Do you know if I need to wait, or will the update finish before the next step in the action?