I have this automation set up to notify me about which plane is overhead. The template to generate the ‘message’ part, which is a concatenation of elements from the trigger, works perfectly. The ‘data’ part I would like to be dependant on some properties and that template logic seems to work fine (generate YAML) as well, but the action fails and the traceback mentions a dict error. I know it has to do with syntax but as far as I can see it should work. Indentation seems correct as well (which is why the |- is in the template). What am I overlooking or doing wrong here?
alias: Notify airplane
description: ""
triggers:
- event_type: flightradar24_entry
trigger: event
conditions:
- condition: state
entity_id: input_boolean.automation
state: "on"
actions:
- action: notify.lametric
data:
message: >-
Flight{% if trigger.event.data.callsign is not in [none, '', 'Blocked']
%} {{ trigger.event.data.callsign }}{% endif %}{% if
trigger.event.data.aircraft_model is not in [none, ''] %} of type {{
trigger.event.data.aircraft_model }}{% endif %}{% if
trigger.event.data.airport_origin_city is not in [none, ''] %} from {{
trigger.event.data.airport_origin_city }} {% if
trigger.event.data.airport_origin_country_code is not in [none, '']
%}({{trigger.event.data.airport_origin_country_code }}){% endif %}{%
endif %}{% if trigger.event.data.airport_destination_city is not in
[none, ''] %} to {{ trigger.event.data.airport_destination_city }}{%
if trigger.event.data.airport_destination_country_code is not in [none,
''] %} ({{ trigger.event.data.airport_destination_country_code }}){%
endif %}{% endif %} passes{% if trigger.event.data.altitude is not
in [none, ''] %} at {{ trigger.event.data.altitude | multiply(0.3048) |
round(0)}}m height{% endif %}.
data: |-
{% if trigger.event.data.altitude | multiply(0.3048) < 5000 %}sound: 'notification3'{% endif %}
icon: '49029'
cycles: 2
priority: {% if trigger.event.airport_destination_code_iata == 'XXX' or
trigger.event.airport_destination_code_iata == 'YYY' %}'critical'{% else
%}'info'{% endif %}
mode: single
The traceback looks like this:
params:
domain: notify
service: lametric
service_data:
message: >-
Flight TEST123 of type Airbus A330-941 from Somewhere (AB) to Overthere (CD) passes at 5829m height.
data: |-
icon: '49029'
cycles: 2
priority: 'info'
target: {}
running_script: false
yet also reports “expected dict for dictionary value @ data[‘data’]”
You can’t template the structure of yaml like this. The YAML parser treats everything after the |- like a string even though it looks to your human eyes exactly like what a dictionary looks like. Instead you’ll have to have an if block in the automation based on the altitude where the if and the else both have the notify action (one with the sound key under data and one without).