Why do automation blueprints require a string for action data

I’m trying to setup a blueprint for device tracking. Here’s what I put together. I haven’t decided on a trigger yet.

blueprint:
  name: Device tracking with OwnTracks
  description: Forwards sensor data from Home Assistant Companion app to OwnTracks through MQTT
  domain: automation
  input:
    mqtt_topic:
      description: Topic at which to publish sensor data
      selector:
        text:
      default: owntracks/user/device
    sensor_id:
      description: ID of the device tracker
      selector:
        text:
      default: px
    tracker:
      description: The device tracker to get sensor data from
      selector:
        entity:
          filter:
            - domain: device_tracker

mode: single
trigger:
condition: []
variables:
  tid: !input sensor_id
  tracker: !input tracker
  topic: !input mqtt_topic
action:
- service: mqtt.publish
  metadata: {}
  data:
    qos: "2"
    topic: "{{ topic }}"
    payload: |-
      {
          "_type": "location",
          "t": "p",
          "tid": "{{ tid }}",
          "tst": "{{ as_timestamp(trigger.to_state.last_reported)|int }}",
          "batt": "{{ state_attr(tracker, 'battery_level') }}",
          "alt": "{{ state_attr(tracker, 'altitude') }}",
          "cog": "{{ state_attr(tracker, 'course') }}",
          "lat": "{{ state_attr(tracker, 'latitude') }}",
          "lon": "{{ state_attr(tracker, 'longitude') }}",
          "acc": "{{ state_attr(tracker, 'gps_accuracy') }}",
          "vac": "{{ state_attr(tracker, 'vertical_accuracy') }}",
          "vel": "{% if (state_attr(tracker, 'car_speed') != 'unknown') %}{{ state_attr(tracker, 'car_speed') }}{% else %}{{ state_attr(tracker, 'speed') }}{% endif %}",
          "p": "{{ state_attr(tracker, 'pressure_sensor')|float / 10 }}",
          "poi": "{{ state_attr(tracker, 'geocoded_location') }}",
          "ssid": "{{ state_attr(tracker, 'wifi_connection') }}",
          "bssid": "{{ {{ state_attr(tracker, 'wifi_bssid') }}"
      }

With the above, I’m getting the following error
Message malformed: template value should be a string for dictionary value @ data['actions'][0]['data']
But similar YAML works fine for a live automation I already have, just not a blueprint based one.

What could I be doing wrong?

FWIW, you don’t necessarily need to “decide” on a single trigger for your blueprint, use an input with a Trigger Selector. But, a trigger is required for all automations.

Get rid of all the " around the template delimiters in the payload, otherwise you will be sending those exact strings instead of rendered values.

There is also an extra {{ after "bssid".

Duh the extra {{ was causing the error; kinda obscure, thanks for catching it!

Regarding the " around the template delimiters, wouldn’t that break the json format I’m trying to output?

Probably… Restructure the value for payload as a jinja dictionary then use the to_json filter.

payload: |-
  {% set c_speed =  state_attr(tracker, 'car_speed')
  if (state_attr(tracker, 'car_speed') != 'unknown') 
  else state_attr(tracker, 'speed') %}
  {{ {
      "_type": "location",
      "t": "p",
      "tid": tid,
      "tst": as_timestamp(trigger.to_state.last_reported)|int(0),
      "batt": state_attr(tracker, 'battery_level'),
      "alt": state_attr(tracker, 'altitude'),
      "cog": state_attr(tracker, 'course'),
      "lat": state_attr(tracker, 'latitude'),
      "lon": state_attr(tracker, 'longitude'),
      "acc": state_attr(tracker, 'gps_accuracy'),
      "vac": state_attr(tracker, 'vertical_accuracy'),
      "vel": c_speed,
      "p": state_attr(tracker, 'pressure_sensor')|float(0) / 10,
      "poi": state_attr(tracker, 'geocoded_location'),
      "ssid": state_attr(tracker, 'wifi_connection'),
      "bssid": state_attr(tracker, 'wifi_bssid')
      } | to_json }}
1 Like

I believe you were the one who also mentioned this in a different topic. Where in the documentation is this stated? Yes, an automation is required to have a triggers entry which should be a list, but as far as I can find (and in my own experience) there is nothing that says this list cannot be empty. If the list could not be empty, why would the visual editor not warn when attempting to save an automation without a trigger?

Where in the documentation is it stated that configuration keys marked Required only means the key is required and that they don’t need a valid value? Why would an empty list satisfy the requirement?

For as long as I’ve been on this forum, nearly every week there has been a user or two who assume that Required means something other than required… and then their “trigger-less automations” don’t work they way they expect.

I don’t know… nor do I know why it allows saving automations without actions. Feel free to post a frontend FR on the frontend discussion page to request that warnings be added.

The Automation Editor is more concerned with syntax than ‘fit for purpose’. In other words, it allows you to create a non-functional automation.

If you believe it ought to do more than that, follow the suggestion to post an FR.

1 Like