I have a trigger creating with various packets of json data being delivered.
Here are the example payloads of the three different packs of data:
{
"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"
}
I decided to try and update a sensor with some of the attributes listed in the packs using the REST API as I think this can be done in a one line template.
So this is in my config yaml:
shell_command:
curl_api: "curl -X POST -H '{{ curl_auth }}' -H '{{ header1 }}' -d '{{ data }}' {{ url }}"
And this is my automation action webhook:
data_template:
curl_auth: |
!secret curl_auth
data: >
{% if trigger.json.pack_id == 1 %}
'{"attributes": {"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 %}
'{"attributes": {"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 %}
'{"attributes": {"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 %}
header1: |
'Content-Type: application/json'
url: |
'http://10.11.12.20:8123/api/states/sensor.mf_car_webhook'
service: shell_command.curl_api
I get an error though in the log:
Error running command: `curl -X POST -H '{{ curl_auth }}' -H '{{ header1 }}' -d '{{ data }}' {{ url }}`, return code: 2
Anyone any ideas? Im sure I just have a few misplaced apostrophes/quotes!
Thanks!