Webhook automation if then else

I have an automation that triggers from a webhook but with a json trigger condition, that works without problems:

- alias: Fullfillment Basic UPS
  trigger:
  - platform: webhook
    webhook_id: Fullfilment_Order_Shopify
  condition:
    - condition: template
      value_template: "{{ trigger.json.tracking_company == 'UPS' }}"
  action:
  - service: homeassistant.update_entity
    entity_id: sensor.shopify_unshipped_orders
  - service: notify.mylogg
    data_template:
      message: '{{ now().strftime(''%d.%m.%y %a %X'') }}  SHOPIFY Ready! EXPRESS {{ trigger.json.name }} ({{ trigger.json.destination.city }}, {{ trigger.json.destination.country }})'
  - service: var.set
    data_template:
      entity_id: var.fullfilment
      value: "EXPRESS Order {{ trigger.json.name }} - ({{ trigger.json.destination.city }}, {{ trigger.json.destination.country }})"

but now I need to add two other conditions that will trigger other things with the content of the trigger.json. So I made this here:

 - alias: Fullfillment Trigger
   trigger:
   - platform: webhook
     webhook_id: Fullfilment_Order_Shopify
   action:
     service_template: "{% if trigger.json.tracking_company == 'None' %} script.ups_fullfillment
                                   {% elif trigger.json.tracking_company == 'Deutsche Post' %} script.deutsche_post_fullfillment
                                   {% else %} script.rest_fullfillment {% endif %}"

But the problem is the three scrips cant read trigger.json (Error rendering data template: UndefinedError: 'trigger' is undefined)

ups_fullfillment:
  sequence:
  - service: homeassistant.update_entity
    entity_id: sensor.shopify_unshipped_orders
  - service: notify.mylogg
    data_template:
      message: '{{ now().strftime(''%d.%m.%y %a %X'') }}  SHOPIFY Ready! EXPRESS {{ trigger.json.name }} ({{ trigger.json.destination.city }}, {{ trigger.json.destination.country }})'
  - service: var.set
    data_template:
      entity_id: var.fullfilment
      value: "EXPRESS Order {{ trigger.json.name }} - ({{ trigger.json.destination.city }}, {{ trigger.json.destination.country }})"

I tried to have three different automation with separate conditions, but it seems a webhook can only trigger one automation. How can I get this automation to work?

Your UPS fullfilment script uses trigger in the data template, but it doesn’t have a trigger, the trigger is only available in the original automation that calls the script.
You either need to create a choose action in your automatiom and put your scripts there or pass the trigger object to the script.

thanks for your reply! How can I pass the trigger.json objects to the three scripts? is the json payload somewhere saved after the webhook is triggered?

Take a look at the docs on how to pass variables to scripts.
However, I’m not sure whether the trigger object can be passed to the script or only specific elements. You need to try.

ah ok, since I already use the custom component variable, I just use it from now to save the trigger.json into variable attributes, and use the conditions based on the variable state

- alias: Fullfillment Trigger
  trigger:
  - platform: webhook
    webhook_id: Fullfilment_Order_Shopify
  action:
    - service: variable.set_variable
      data_template:
        variable: shopify_fulfillment
        value: '{{ trigger.json.name }}'
        attributes_template: >
          {
            "city": "{{ trigger.json.destination.city }}",
            "country": "{{ trigger.json.destination.country }}",
            "tracking_company": "{{ trigger.json.tracking_company }}"
          }

- alias: Fullfillment Trigger Variable
  trigger:
     platform: state
     entity_id: variable.shopify_fulfillment
  action:
     service_template: "{% if states.variable.shopify_fulfillment.attributes.tracking_company == 'None' %} script.ups_fullfillment
                        {% elif states.variable.shopify_fulfillment.attributes.tracking_company == 'Deutsche Post' %} script.deutsche_post_fullfillment
                        {% else %} script.rest_fullfillment {% endif %}"
1 Like