Event_data_template not creating entity

Hi have an automation with a webhook trigger.
Here is the action that is not creating the entity with the states:

event: mf_car_webhook
  entity_id: >
    {% if trigger.json.pack_id == 1 %}
      lat: "{{ trigger.json.lat }}"
      lng: "{{ trigger.json.lng }}"
      speed: "{{ trigger.json.speed }}"
      position_gmt: '{{ as_timestamp(strptime(trigger.json.gmt, "%Y-%m-%d %H:%M:%S")) | timestamp_custom("%d/%m/%y %H:%M") }}'
    {% elif trigger.json.pack_id == 3 %}
      ignition: "{{ trigger.json.state }}"
      ignition_gmt: '{{ as_timestamp(strptime(trigger.json.gmt, "%Y-%m-%d %H:%M:%S")) | timestamp_custom("%d/%m/%y %H:%M") }}'
    {% elif trigger.json.pack_id == 22 %}
      behaviour_event: "{{ trigger.json.type }}"
      behaviour_value: "{{ trigger.json.value }}"
      behaviour_gmt: '{{ as_timestamp(strptime(trigger.json.gmt, "%Y-%m-%d %H:%M:%S")) | timestamp_custom("%d/%m/%y %H:%M") }}'
    {% endif %}

If I were to remove the if then else statements and the entity_id line I am sure if would work as some other users have this set up so I am sure this is what is causing the issue.
If I remove the entity_id line on its own, it throws an error.

A template will always result in a single string, so what you are trying to do is not possible with a template, because you are trying to create a dictionary.

Hey, I recognize this from the other thread.

You took what I had suggested (in the other thread) and applied it in an unexpected way. I was going to ask “Did that actually work?” and I guess this thread serves as the answer.

You’re using templates to generate YAML statements and that’s not how they’re meant to be used.

Ninja’d by Burningstone.

1 Like

@123, @Burningstone
Hi guys,
Yes a few posts over the last day! :slight_smile:

So do you think there will be a way around this without creating automations for each type of pack_id I have?

You’ll have to explain what you’re trying to do because, even without the templates, I don’t understand how this is a valid YAML let alone a valid entity_id:

event: mf_car_webhook
  entity_id: >
    lat: 45
    lng: 74
    speed: 50
    position_gmt: 2020-06-26 11:45
1 Like

Ok so I have an automation with a webhook trigger that my car tracking system posts to.
It posts 3 different types of json messages.
All of the messages contain one variable that is the same called pack_id.
This is the identifier of the message.
1 = a position change message
3 = an ignition status change message
22 = a driver behaviour event message

If it was all included in the same pack, the action would be as @Tinkerer showed me in another topic.
here And here.
Basically I wouldn’t need the if then else statements and my action would be the same as @Tinkerer’s.

So in a nutshell:

Incoming webhook with json body payload
read pack_id
if the pack_id = 1 then create/update entity with attributes lat, lng, speed, position_gmt.
if the pack_id = 3 then create/update entity with attributes ignition, ignition_gmt
and so on…

Hope that makes sense?

Example payloads:

    "id": 1,
    "car_id": 47039,
    "pack_id": 1,
    "lat": 56.92549,
    "lng": 24.01985,
    "speed": 6,
    "direction": 32,
    "altitude": 0,
    "gmt": "2016-07-25 10:31:08"
    "id": 1,
    "car_id": 47039,
    "pack_id": 3,
    "state": 1,
    "gmt": "2016-07-25 10:31:08"
    "id": 1,
    "car_id": 47039,
    "pack_id": 22,
    "type": "steering",
    "value": 2.4,
    "gmt": "2016-07-25 10:31:08"

Ultimately I am wanting to end up with an entity (called mf_car_webhook) with the following attributes:

  • lat
  • lng
  • speed
  • position_gmt
  • ignition
  • ignition_gmt
  • behaviour_event
  • behaviour_value
  • behaviour_gmt

You could try with a template sensor, you can define attributes there as well.

Yes I currently have it working by means of a sensor with a different API, however it is a pull API and the data only refreshed every 60 seconds. O wanted to use the push API so the data is pushed to HA in real time.
Also in addition some of the features in the push API aren’t available in the pull API so currently I am.missong some data.
I suppose I could create a blank entity using sensor then use another method in the automation to update the attributes - is this what you meant?