Using templates to translate webhook output into activity + entity_id?

Hi,

I just started with HA and wanted to give IFTTT and webhooks a try. So I’ve created an IFTTT with Google Assistant trigger that reacts to:

"Turn on radio in $"

And a webhook that posts a body of:

{ "pomieszczenie": "{{TextField}}" }

(setting pomieszczenie to whatever comes in $)

And then I thought I can mutate it like this at my HA end:

{
"entity_id": "{% if trigger.json.pomieszczenie == "bedroom" -%} remote.bedroom {%- else -%} remote.living_room_2 {%- endif %}"
"activity": "Listen to radio"
}

But I don’t think I can put it anywhere in this automation:

- id: '1569956284174'
  alias: Turn on radio
  trigger:
  - platform: webhook
    webhook_id: turn_on_radio
  condition: []
  action:
  - data:
      activity: Listen to radio
      entity_id: remote.bedroom
    service: remote.turn_on

Can I?

I don’t think that’s the typical way to use IFTTT with HA. You should probably check out the docs (even if they are fairly confusing and misleading) if you haven’t already.

But, assuming what you have works ok (i.e., the webhook trigger is firing when IFTTT sends the POST request), I think you may want to do this:

- id: '1569956284174'
  alias: Turn on radio
  trigger:
  - platform: webhook
    webhook_id: turn_on_radio
  condition: []
  action:
  - data_template:
      activity: Listen to radio
      entity_id: "remote.{{ 'bedroom' if trigger.json.pomieszczenie == 'bedroom' else 'living_room_2' }}"
    service: remote.turn_on

You mean that I should use “ifttt_webhook_received” instead of just webhook? Well, I couldn’t make that one work, but my method works flawlesly, it seems.

Yes, using the ifttt_webhook_received event trigger is the typical way, but it requires you to set up the IFTTT Integration on the Integrations page. That will give you the webhook_id to use.

But, I don’t think it really matters. As long as you can set up IFTTT to send a POST request that you can get a HA automation to trigger on, that’s the important part.

What I showed is how you’d use the room designation from the POST request to control which remote the service call in the automation uses. Does it make sense?

Sure it does! But I’m still struggling to make it work, though! :smiley:

What exactly do you have now, and how is it not working? I.e., are you seeing any errors? Can you see the call_service event in home-assistant.log? (You may have to set homeassistant.core to debug in the logger to be able to see these events first.)

I’m trying now to do this action “the right way”, with ifttt_webhook_received, but my family is sick of our audio system going on and off, so I had to stop.

Turning on debug might be a turning point for making it work, though.

You can also subscribe to the event on the Events page to see exactly what comes in from IFTTT in the UI.

You mean “listen to events” and “Start listening” to “ifttt_webhook_received”?

I’m still using a slightly old version of HA and that page has since changed a bit. But, yes, that’s what I meant. Each time the event is received it will dump out the event data, etc.

In my log when firing IFTTT I can only see this:

{'handler': 'webhook',
 'msgid': '55d93d8e-196b-4c9c-a082-6570cf8093da',
 'payload': {'body': '{ "action": "call_service", "service": "light.turn_on", '
                     '"entity_id": "light.nigdziebadz" } JSON',
             'cloudhook_id': 'c1f01b0193e14fe6b690c15fe73e846d',
             'headers': {'Content-Length': '95',
                         'Content-Type': 'application/json',
                         'Host': 'hooks.nabu.casa',
                         'X-Forwarded-For': '54.166.186.113',
                         'x-newrelic-id': 'VwAOU1RRGwAFUFZUAwQE',
                         'x-newrelic-transaction': 'PxRUAlZTXlAEXQVTAwQPXkYdUFIOFQZOEgFbUgheVwYKAQhVBFkLQEgUUQMDW1kEVQZDPw=='},
             'method': 'POST',
             'query': ''}}

So would this alone turn on light on “light.nigdziebadz” if it existed?