Execute Shortcut on Apple Device

Hi,

i have set up an automation using HA Automation to fire a Shortcut on my iPhone triggered by HA. It works fine but I want to bring it to NR now. What should the “data” part should look like? I don’t get this to work.

Thanks.

Docs

HA Automation:

alias: Wecker deaktivieren
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.hue_schalter_flur_schlusselbrett_action
    to: on_press
condition: []
action:
  - service: notify.mobile_app_dennis_iphone_12_mini
    data:
      message: "\"Trigger a Shortcut!\""
      data:
        shortcut:
          name: Alle Wecker deaktivieren
          key_for_shortcut: ignore_result
          another_key: another value
mode: single

Home Assistant configuration is in YAML (either in the config file or you can see the YAML equivalent when using the UI to set things up).

Your YAML defines action (as an array) with one service call. Under that is the data object, being message and (yet another) data object. We want the entire top level ‘data’ block.

This can be hand-translated into JSON, or you can use one of the many on-line YAML to JSON converters.

This suggests that your Data object to go into the Data UI configuration, is the following JSON object

{
  "message": "Trigger a Shortcut!",
  "data": {
    "shortcut": {
      "name": "Alle Wecker deaktivieren",
      "key_for_shortcut": "ignore_result",
      "another_key": "another_value"
    }
  }
}

As each service call has specific requirements for the data object, there is often a help list at the bottom of the node, with a ‘load example data’ button to the right. This does not always work, however it can pre-load some example data for you which may help understand the data object structure.

For notifications
{"message": "message string", "title": "title string", "target": "entity.target_id", "data": {}}

is often a good start, with the second level ‘data’ object being very dependent on the platform.

I have removed the "\" in your message string as this is just adding " to what is otherwise a string and you probably don’t need it.

Hopefully that will work for you.

1 Like

Thanks. I got it :+1:. What I still do not understand from the Docs is the following:

How do I implement this in the code? HA Automation first:

alias: Wecker deaktivieren
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.hue_schalter_flur_schlusselbrett_action
    to: on_press
condition: []
action:
  - service: notify.mobile_app_dennis_iphone_12_mini
    data:
      message: "\"Trigger a Shortcut!\""
      data:
        shortcut:
          name: Alle Wecker deaktivieren
          key_for_shortcut: ignore_result
          another_key: another value
mode: single

The documentation suggests

You can customize this behavior with the following keys in shortcut above:

Key Values Notes
ignore_result Any String, e.g. "ignore" When set, does not re-open the Home Assistant app when completed. Also prevents the below event from firing.

Hence I would add the key “ignore_result” to the data-shortcut object. The value of the key just needs to be “anything”, ie the “ignore_result” key just needs to exist.

{
  "message": "Trigger a Shortcut!",
  "data": {
    "shortcut": {
      "name": "Alle Wecker deaktivieren",
      "ignore_result": "yes please"
    }
  }
}

The way that the ‘data’ object works for service calls, is that it is an object passed to the service call (and therefore the integration). Inside ‘data’ as an object we can have zero to many key: value pairs. The integration will look for certain keys that are required (probably “name” in this case) and for optional keys that may be there (“ignore_result”) which thus act like switches. Any other keys “key_to_the_door”: 21, will hopefully be ignored also.

1 Like

Sometimes it’s easier than expected :blush:. I was confused by “Any string”. Works perfect now. Thank you for your detailed explanation. :pray: