IFTTT -> Home Assistant with templating

Hi all,

I’m looking to send a Google Assistant request to vacuum a certain room in my house, via IFTTT.
Everything seems to be working so far, except for the conditional if statements.
(The end goal will be to send a service request with the room’s ID number)

My automations file

- id: this_is_the_automation_id
  trigger:
  - platform: event
    event_type: ifttt_webhook_received
    event_data:
      action: vacuum_room
  action:
  - service_template: '{{ trigger.event.data.service }}'
    data_template:
      message: >
        {% if '{{ trigger.event.data.message }}' == 'the laundry' %}
          L
        {% elif '{{trigger.event.data.message}}' == "the kitchen" %}
          K  
        {% else %}
          '{{ trigger.event.data.message }}'
        {% endif %}

I’ve only just started looking into Templating, and don’t fully understand the syntax, but can’t seem to get any of the data messages to match, but can spit out the data message in the else statement.
I’ve looked around for different templating examples and tried different combinations of quotation levels etc., but I’m sure there’s something fundamental I’m missing in the syntax or misunderstanding about templating.

Would be grateful if someone could steer me in the right direction - cheers!

Like this:

  action:
  - service: '{{ trigger.event.data.service }}'
    data:
      message: >
        {% if trigger.event.data.message == 'the laundry' %}
          L
        {% elif trigger.event.data.message == 'the kitchen' %}
          K  
        {% else %}
          {{ trigger.event.data.message }}
        {% endif %}

Or this:

  action:
  - service: '{{ trigger.event.data.service }}'
    data:
      message: >
        {% set msg = trigger.event.data.message %}
        {{ 'L' if 'laundry' in msg else 'K' if 'kitchen' in msg else msg }}

What is the service call received from IFTTT? I assume it’s a notification because of the message option?

1 Like

Thanks for the guidance!

The trigger is actually for a vacuum, but I was just using persistent notifications message for testing.
Here’s my final(ly) working code

- id: clean_room
  alias: Clean room
  trigger:
  - platform: event
    event_type: ifttt_webhook_received
    event_data:
      action: clean_room
  condition: []
  action:
  - service_template: xiaomi_miio.vacuum_clean_segment
    data_template:
      entity_id: vacuum.xiaomi_vacuum_cleaner
      segments: >
        {% set msg = trigger.event.data.message %}
        {% if 'living room' in msg %}
          [17]
        {% elif 'entrance' in msg or 'landing' in msg %}
          [18]
        {% elif 'study' in msg %}
          [19]
        {% elif 'dining room' in msg or 'nursery' in msg %}
          [20]
        {% elif 'laundry' in msg or 'bathroom' in msg %}
          [21]
        {% elif 'ensuite' in msg %}
          [22]
        {% elif 'kitchen' in msg or 'toilet' in msg %}
          [23]
        {% endif %}

I’m sure there’s a better way to do the value mapping, but I’m just glad it’s working.
Thanks again for your help!

1 Like

It’s possible to use a dictionary but you already have working code so there’s no pressing need to change it. What I would suggest is that you append a final else to the long if-elif chain to handle the case where there is no match found (i.e. it needs a default value). Currently, it produces no value in that situation which is not likely to be acceptable to the service call.

Please consider marking my post (above) with the Solution tag (only you, the author of this topic, can do that). It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions about webhooks and templating the trigger variable.

1 Like