Variable not being treated as a string

- variables:
    coords: |-
      {%- if is_state("person.bart_simpson", "home") -%}
        {%- set latitude = state_attr('zone.home', 'latitude') -%}
        {%- set longitude = state_attr('zone.home', 'longitude') -%}
      {%- else  -%}
        {%- set latitude = state_attr(state_attr('person.bart_simpson', 'source'), 'latitude') -%}
        {%- set longitude = state_attr(state_attr('person.bart_simpson', 'source'), 'longitude') -%}
      {%- endif -%}
      {{ latitude ~ "," ~ longitude }}
- action: waze_travel_time.get_travel_times
  metadata: {}
  data:
    region: eu
    units: metric
    vehicle_type: car
    origin: "{{ coords }}"
    destination: "{{ repeat.item.location }}"
    realtime: true
  response_variable: travel

{{ repeat.item.location }} works fine and it’s coming from calendar.get_events.
If I use "{{ coords }}" as the origin, I get the following error:

Executed: 21 November 2024 at 19:38:53
Error: expected str for dictionary value @ data['origin']
Result:

params:
  domain: waze_travel_time
  service: get_travel_times
  service_data:
    region: eu
    units: metric
    vehicle_type: car
    origin:
      - 11.111
      - -1.111
    destination: Some random street
    realtime: true
  target: {}
running_script: false

How can I force it to be a string ?

I reverted your post because this will likely not get attention as a bug because it’s not a bug. The template engine is designed to convert template values into usable objects. Unfortunately, what you’re running into is a byproduct of this functionality.

What’s happening is the template resolver (runs after a template is calculated) is detecting the value as a list, so it treats it as a list when output to a field or value.

To get around this, you have to template the entire data section.

- action: waze_travel_time.get_travel_times
  metadata: {}
  data: >
    {{ dict(
      region='eu',
      units='metric',
      vehicle_type='car',
      origin=coords | join(', '),
      destination=repeat.item.location,
      realtime=True,
    ) }}
  response_variable: travel
1 Like