I have a very basic Python script using PyScript which takes an input string of comma separated values and then calls an external API.
I am trying to put a string of comma separated values from an Automation, however the values are being input as a List (shown below), what am i missing to force the input to stay as a string?
service: pyscript.send_disruption_details
data:
disruptions: >-
{% set disruptions = state_attr('sensor.home_to_work_route_0','disruption_ids') | string | trim('[]') %}
{% set disruptions = disruptions.split(', ') %}
{% set ignored_disruptions = states('input_text.ptv_ignored_disruptions') | string | trim('[]') %}
{% set ignored_disruptions = ignored_disruptions.split(', ') %}
{% set ns = namespace(output = '' | string) %}
{%- for a in disruptions -%}
{%- if a not in ignored_disruptions -%}
{%- set ns.output = ns.output | string + a | string + "," | string -%}
{%- endif -%}
{%- endfor -%}
{{ ns.output | string }}
sendToRichard: true
However when looking in Traces this is inputting as a list:
Result:
params:
domain: pyscript
service: send_disruption_details
service_data:
disruptions:
- 200000
sendToRichard: true
target: {}
running_script: false
I would like this input as: disruptions: "200000,"
I have got this working by using a space separated list, however is there a proper approach to this? I did try quoting, "{{ output }}"
, other delimiters (e.g. | ) but space seemed to work.
Although i have worked around this i thought i would still post to see what the proper solution is and hopefully help anyone else that is coming across a similar issue.
Note: I did have other options like just sending the entity_id or attribute value to the Python as another input but i was hoping to use templates in the automation as that may give me a little bit more UI flexibility rather than having to update the python script on the HA server.