CURL Shell Command Action with Template

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!

Hi Everyone,
After many days on and off trying to solve this one I finally got it (well…so far I think it works!)

Part of the problem was a few apostrophes and quotes, and I also ended up using rest_command instead of shell_command.

Here is how I did it:
In my config yaml:

rest_command:
  curl_api:
    url: http://localhost:8123/api/states/{{ entity_id }}
    method: POST
    headers:
      Authorization: !secret rest_auth
    payload: '{{ payload }}'
    content_type:  'application/json'

And in my automation:

data_template:
  entity_id: 'sensor.mf_car_webhook'
  payload: |
    {% if trigger.json.pack_id == 1 %}
      {"state": "{{ as_timestamp(now()) | timestamp_custom("%d/%m/%y %H:%M") }}", "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 %}
      {"state": "{{ as_timestamp(now()) | timestamp_custom("%d/%m/%y %H:%M") }}", "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 %}
      {"state": "{{ as_timestamp(now()) | timestamp_custom("%d/%m/%y %H:%M") }}", "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 %}
service: rest_command.curl_api

I had some of these '' bad boys around the json payloads that was causing bad requests to the rest integration. I only spotted it because when I swapped to rest_command from shell_command it actually threw the 400 bad request error, so I knew there was a problem with the payload!

Well, hope this might aid someone else one day!

2 Likes