JSON object in data_template for generic IFTTT automation

Hello,
I’m trying to create a generic automation for my IFTTT applets.
My Applet JSONs have same structure…like this:

{ 
   "action":"call_service",
   "service":"notify.telegram_myname",
   "action_data": {
		"message": {{UserName}}
	}
}

Now…this automation works fine…

- id: '1571231239000'
  alias: IFTTT Webhook
  trigger:
    platform: event
    event_type: ifttt_webhook_received
    event_data:
      action: call_service
  action:
  - service_template: '{{ trigger.event.data.service }}'
    data_template: { "message" : "MYNAME!" }

but, if I try to pass the “action_data” of my JSON object as data_template…

- id: '1571231239000'
  alias: IFTTT Webhook
  trigger:
    platform: event
    event_type: ifttt_webhook_received
    event_data:
      action: call_service
  action:
  - service_template: '{{ trigger.event.data.service }}'
    data_template: {{ trigger.event.data.action_data }}

…I’ve got this error…

2019-10-14 23:20:38 ERROR (SyncWorker_11) [homeassistant.util.yaml.loader] invalid key: "OrderedDict([('trigger.event.data.action_data', None)])"
  in "/home/homeassistant/.homeassistant/automations.yaml", line 221, column 0
2019-10-14 23:20:38 ERROR (MainThread) [homeassistant.components.automation] invalid key: "OrderedDict([('trigger.event.data.action_data', None)])"
  in "/home/homeassistant/.homeassistant/automations.yaml", line 221, column 0

if I try to pass the “action_data” of my JSON object as data_template with quotes…

- id: '1571231239000'
  alias: IFTTT Webhook
  trigger:
    platform: event
    event_type: ifttt_webhook_received
    event_data:
      action: call_service
  action:
  - service_template: '{{ trigger.event.data.service }}'
    data_template: "{{ trigger.event.data.action_data }}"

…I’ve got this error…

2019-10-15 00:15:00 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: expected a dictionary for dictionary value @ data['action'][0]['data_template']. Got None. (See /home/homeassistant/.homeassistant/configuration.yaml, line 194). Please check the docs at https://home-assistant.io/integrations/automation/

How can I solve this problem ?
Is it possible to pass an entire JSON in data_template ?

Thank you

1 Like

that won’t work because you’re sending a complex object as a string.

Something like this cannot be made generic because the shape of the object changes and home assistant transfers it as a string.

ok…but if you take a look at the other test without quotes…

{{ trigger.event.data.action_data }}

…you can see it raises another error.
I don’t understand why it doesn’t work.

in {{ trigger.event.data.action_data }} there is the exact same JSON object of the first example { "message" : "MYNAME" }

No, you aren’t understanding.

it’s not that this {{ trigger.event.data.action_data }} is in quotes. The problem is that {{ trigger.event.data.action_data }} RETURNS a string. The string will literally look like this { "message" : "MYNAME" }. But it will be a string, not a json object.

Put this into your template editor.

{% set example = '{ "message" : "MYNAME" }' %} #NOTICE QUOTES AROUND JSON
{{ example is string }} #IT IS A STRING

It will return True

{% set example = { "message" : "MYNAME" } %} #NOTICE LACK OF QUOTES AROUND JSON?
{{ example is string }} #ITS NOT A STRING

It will return False

All templates, regardless of what it does, return a string.

I understand…I converted the string in json

action:
  - service_template: '{{ trigger.event.data.service }}'
    data_template: "{{ trigger.event.data.action_data | tojson }}"

in template editor this conversion works, but the problem I think is that when I start homeassistant it founds data_template null.

I found another solution (I don’t know if it’s better solution).
I create N automations for N applets…passing in JSON also the applet name.

{ 
	"applet":"FingboxAlert",
	"action":"call_service",
	"service":"notify.telegram_myname",
	"action_data": {
		"message": "MYNAME!"
	}
}

…then in my automations…

- id: '1571231239000'
  alias: IFTTT FingboxAlert
  trigger:
    platform: event
    event_type: ifttt_webhook_received
    event_data:
      action: call_service
  condition:
    condition: template
    value_template: '{{ trigger.event.data.applet == "FingboxAlert" }}'
  action:
  - service_template: '{{ trigger.event.data.service }}'
    data_template:
      message: '{{ trigger.event.data.action_data.message }}'
      title: '{{ trigger.event.data.applet }}'

Now it works fine.

What do you think about this ?
Do you have other suggestions or better solutions ?

Thank you very much

No you don’t.

You cannot make this work. Period. The RETURN is ALWAYS a string. ALWAYS. I don’t know how else to spell this out to you. It will NEVER be an object, even if you specify it that way.

This should always work because the shape is predefined and each field is a single string. It’s the closest you can get.

Now, You could possibly get the top generic solution to work if you use a python script to handle the action. But, this hinges on the exec() method. If that method doesn’t work in scripts, then you’d have to get inventive with parsing the jsonstring into an object.

The downside of the python approach is that it’s python. You’d have to learn it.

Yes…using a python script I can solve, but I prefer my last solution using templates :slight_smile:

Thank you for your support :wink: