Passing variables from IFTTT (using integrations for auth)

I use Google Assistant (GA) and IFTTT to trigger a variety of Home Assistant (HA) services/scripts. For several of the scripts, I pass variables from IFTTT. That had been working extremely well until I switched authentication methods. I moved from using api_password to service integrations. For simple scripts, this move has been an easy transition. However, I can’t figure out how to successfully continue passing variables from IFTTT to HA.

With the api_password method, my configuration looked something like this:

IFTTT
URL:

https://[DOMAIN].duckdns.org:[PORT]/api/services/script/tv_volume_up_input?api_password=[PASSWORD]

Body:

{ "vol_amnt":"{{NumberField}}" }

Home Assistant

script:
  tv_volume_up_input:
    alias: 'TV Volume Up Input Control'
    sequence:
      - service: input_number.set_value
        entity_id: input_number.tv_volume_up_variable
        data_template:
          value: >
            {% set vol_val = vol_amnt|float %}
            {% if vol_val|float <= 0.0|float %}
              {% set vol_val = 0.0|float %}
              {{vol_val|float}}
            {% elif vol_val|float > 32.0|float %}
              {% set vol_val = 31.0|float %}
              {{vol_val|float}}
            {% else %}
              {{vol_val|float - 1.0|float}}
            {% endif %}

With the new integration method, my configuration looks something like this:

IFTTT
URL:

https://[DOMAIN].duckdns.org:[PORT]/api/webhook/[TOKEN]

Body:

{ "action": "call_service", "service": "script.tv_volume_up_input" }

Home Assistant

automation:
  - hide_entity: true
    trigger:
      platform: event
      event_type: ifttt_webhook_received
      event_data:
        action: call_service
    action:
      service_template: '{{ trigger.event.data.service }}'
      data_template:
        entity_id: '{{ trigger.event.data.entity_id }}'

I still need to pass that variable to the tv_volume_up_input script (and other variables to other scripts), but I have no idea how to do it. I’ve tried multiple ways to include it in the IFTTT body and within HA, but with no success (things such as using the variable term in the body, adding a field in the data_template, etc).

If I do nothing, the HA log returns:

Error rendering data template: UndefinedError: ‘vol_amnt’ is undefined

Hopefully someone can help me with this

Thanks!

I haven’t tried this, but from what I just saw someone else do in another topic, maybe something like this might work…

IFTTT body:

{ "action": "call_service", "service": "script.tv_volume_up_input",
  "vol_amnt": "{{NumberField}}" }

HA:

automation
- hide_entity: true
  trigger:
    platform: event
    event_type: ifttt_webhook_received
    event_data:
      action: call_service
  action:
    service_template: '{{ trigger.event.data.service }}'
    data_template:
      vol_amnt: '{{ trigger.event.data.vol_amnt }}'

I tried a similar configuration yesterday with the entity_id also under the data_template. It looked like:

automation:
  - hide_entity: true
    trigger:
      platform: event
      event_type: ifttt_webhook_received
      event_data:
        action: call_service
    action:
      service_template: '{{ trigger.event.data.service }}'
      data_template:
        entity_id: '{{ trigger.event.data.entity_id }}'
        vol_amnt: '{{ trigger.event.data.vol_amnt }}'

That, unfortunately, didn’t work. However, @pnbruckner, in your example you left out the entity_id so you inspired me to try it that way. That worked like a charm! Sadly, adding both returns an error of:

voluptuous.error.MultipleInvalid: extra keys not allowed @ data[‘vol_amnt’]

The same error is generated (calling out vol_amnt) regardless of the order in which the items are entered. I still need the entity_id in there, so now I have a new problem to solve :slight_smile:

You don’t have entity_id in your IFTTT body. That’s probably the problem.

Oh no, I need it for other IFTTT automations, not this one :). For now, what I did was create two entries, one with entity_id and one with vol_amnt. So it looks like this:

automation:
  - hide_entity: true
    trigger:
      platform: event
      event_type: ifttt_webhook_received
      event_data:
        action: call_service
    action:
      service_template: '{{ trigger.event.data.service }}'
      data_template:
        entity_id: '{{ trigger.event.data.entity_id }}'
automation:
  - hide_entity: true
    trigger:
      platform: event
      event_type: ifttt_webhook_received
      event_data:
        action: call_service
    action:
      service_template: '{{ trigger.event.data.service }}'
      data_template:
        vol_amnt: '{{ trigger.event.data.vol_amnt }}'

It’s not elegant, but it works. I’ll eventually find a way to combine them. Thanks again @pnbruckner!

Both of these automations will run for each IFTTT webhook received, and probably one will always cause an error. I’d suggest maybe something like this:

automation:
  - hide_entity: true
    trigger:
      platform: event
      event_type: ifttt_webhook_received
      event_data:
        action: call_service
    condition:
      condition: template
      value_template: "{{ trigger.event.data.service != 'script.tv_volume_up_input' }}"
    action:
      service_template: '{{ trigger.event.data.service }}'
      data_template:
        entity_id: '{{ trigger.event.data.entity_id }}'

and

automation:
  - hide_entity: true
    trigger:
      platform: event
      event_type: ifttt_webhook_received
      event_data:
        action: call_service
        service: script.tv_volume_up_input
    action:
      service_template: script.tv_volume_up_input
      data_template:
        vol_amnt: '{{ trigger.event.data.vol_amnt }}'

I didnt like having to use multiple automations for each service with all the condition checking involved so this is the approach i used. Works quite well. You can also move a lot of your range checking of your volume values into the python script if you wish. As you noticed above services do not like getting called with the wrong number or the wrong attributes.

Hmm, I like this concept. However, I would need to perform such a check for any new values I add that aren’t entity_id or vol_amnt. There must be a way to combine them all into one :confused:

I looked through your solution, @Dilbert66, and it looks very promising. I’ll give this a try later today. Thanks for sharing!

For the case above, you would only need to add the line below to the automation and use the json body below. The “action”:“call service” is not needed since the value is hard coded in the automation but doesnt hurt to leave it there.

Thank you for your post,
Can you help me about API_PASSWORD?
I don’t understand how to create api_password and where…

another thing you say:

https://[DOMAIN].duckdns.org:[PORT]/api/webhook/[TOKEN]

this is another way to call service ?
Can you help me?