I have a service posting JSON data to an automation with a webhook trigger.
The automation triggers fine and I can see the JSON data that the service is posting. But how do I parse the JSON data and create sensors from that?
Thanks
I have a service posting JSON data to an automation with a webhook trigger.
The automation triggers fine and I can see the JSON data that the service is posting. But how do I parse the JSON data and create sensors from that?
Thanks
With templating. The templating docs cover this in detail, but here’s one of mine, and here’s another.
@Tinkerer - amazing, just what I was looking for!
Now to throw a spanner in the works…
I have an index in the json that determines the type of message. Its either 1, 2 or 3.
How do I add the curly brackets inside an if statement?
Its pulling an error whichever way I try and do it.
This is what I have:
data_template:
message: >
{% if ({{ trigger.json.pack_id }}) == 1 %}
Pack_ID is 1
{% else %}
Pack_ID is something else
{% endif %}
title: '{{ trigger.json.car_id }}'
service: notify.mobile_app_sm_n970f
data_template:
message: >
{% if trigger.json.pack_id == 1 %}
First message
{% elif trigger.json.pack_id == 2 %}
Second message
{% elif trigger.json.pack_id == 3 %}
Third message
{% else %}
Uh-oh! Something's wrong!
{% endif %}
title: '{{ trigger.json.car_id }}'
service: notify.mobile_app_sm_n970f
Thats done it! Thanks!
This is my end code for creating a webhook and allowing a service to post json data to it containing sensor information:
event: mf_car_webhook
event_data_template:
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 }}"
last_update: '{{ as_timestamp(strptime(trigger.json.gmt, "%Y-%m-%d %H:%M:%S")) | timestamp_custom("%d/%m/%y %H:%M") }}'
{% endif %}