How to make persistent variables for iOS and iPadOS Shortcuts | Hacking Shortcuts

Store data across Shortcut runs with Memory

Shortcuts has proven to be an invaluable source of automation to me. It is in many ways a very powerful app. But it has its limitations (in particular the “automations” that are not actually automatic but that’s another story). One limitation that has cropped up for me multiple times is around persistent variables. What I mean by that is the ability to store data across Shortcut runs.

What I’d like to be able to do is to run a Shortcut on one day, process some data. And the following day, or even later that same day, run the Shortcut again and have the previous data available for use in the new Shortcut run.

Shortcuts doesn’t natively support this functionality – any variables initialised in a run disappear once the Shortcut ends. There are some workarounds to this.

Here is a video demonstration of me using the preferred method (method 3 below):

  1. Toolbox Pro. This app supports a feature called Global Variables and these are available across Shortcut runs. The app is free to download. Global variables are included in the free set of functionality. > This is good but I personally wanted something that wouldn’t require an app that I might not have installed all the time.
  2. Notes. Shortcuts supports appending to notes. I’d judge that a high proportion of iOS users have the stock Notes app installed. So a note can serve as a data repository. Just give a note a unique title and you can reliably access it using the Find Notes Where action. Set the limit to 1 note and assign it to a variable. > The major limitation here is that Shortcuts can only append to a note. Once data is inserted into the note it can’t be deleted by Shortcuts. This means you have to build in a routine to identify the last item that was appended to get the latest data payload. Also as the Shortcut is used over time the note contents will increase. Eventually this will have an impact on the performance of the Shortcut. > The above limitation also applies to storing variables in text files directly by a Shortcut – Shortcuts can only append to text files.
  3. Reminders. Reminders might seem a strange place to store data until you consider the fact that reminder notes can be changed; not just be appended to. So variables can be stored in a master dictionary* as text. To do this we can simply use the Edit Reminder action. > This is my preferred method of storing variables across Shortcut runs because I always have the Reminders app installed and it’s more efficient than the Notes method.

Here’s how I implement variable storage for a Shortcut I use to log electricity and gas meter readings. The Shortcut is called PyLog (because the Shortcut triggers a Python script to store my readings in text files). When adding a new meter reading the Shortcut will ask if it is an electricity / gas reading, and then it will prompt me for the reading. In the prompt is the previous reading, e.g. “Input meter read (previous: 3334)”.

  • In Reminders I’ve created a dedicated list called Reference. This list will only ever have reminders that I use for variable storage so it won’t become unwieldy.
  • In that list I have a reminder called PyLog variables.
  • When I run my Shortcut it gets that reminder by name (filters to first reminder match).
  • Then it gets the content of the notes of that reminder and extracts a dictionary from the note text.
  • One of the dictionary keys is the variable name I’m interested in, so I get the value of that key. E.g. Get value for variable name from dictionary name.
  • The dictionary value itself can be anything – including another dictionary – so the scope for storing variables can extend to cover some complex scenarios.
  • Once the variable is extracted from the reminder (the above processes are very fast btw) I can then do what I need with it – show it in a prompt for input or do a calculation with it etc.
  • Updating the stored variable. To store an updated variable I add the variable back to the master dictionary using the same variable name as its key, e.g. Set variable name to updated variable in master dictionary, then set the reminder notes to the master dictionary.

Generalising this process to expand its usefulness

Repeating the above steps across multiple shortcuts would prove tedious. So I’ve harnessed a useful feature of Shortcuts app to make persistent variables more readily accessible. One Shortcut action is to Run Shortcut. I made a dedicated Shortcut whose sole purpose is to get or add persistent variables by storing them in a reminder note in a reminder list called Reference. The Shortcut is called Memory. Whenever I want to work with a variable that needs to persist across shortcut runs I use the Run Shortcut action and call Memory passing in a dictionary specifying if I am getting the last stored value of the variable or updating its value.
The dictionary has three key-value pairs:

  • “variable name” | a string value.
  • “method” | a string specifying if the variable is being added to memory “add” or being retrieved “get”.
  • “payload” | a string, number or dictionary – the data being stored for this variable.
    If getting a stored variable the payload value is returned. If adding a variable to store the variable payload is added to the master dictionary using variable name as the key and the master dictionary is stored in the reminder note.

Below are screenshots of dictionaries for passing into the Memory Shortcut. Example 1 shows how to get a variable from storage (variable is called Cycle Log). Example 2 shows how to add data to storage. In example 2 the dictionary at the top is what I want to store in the payload; you can see it referenced in the lower dictionary payload field. The lower dictionary is what gets passed into the Memory shortcut to store the data.

Example 1. A Shorcuts dictionary for passing into Memory to get a stored variable.
Example 2. A Shorcuts dictionary for passing into Memory to add a stored variable.















This Shortcut has a lot of potential for my workflows. This week I integrated Memory into my Fuel Log Shortcut to track fuel cost changes between fill ups.

If you’d like to download Memory to your Shortcuts library and start using persistent variables you can do so here. The shortcut requires a Reminder list called Reference. Create that list before use.

Notes

* A dictionary is a format for laying out data in key-value pairs. The key is the unique name used to access the value. This is an example of a dictionary: {“key_1” : “value_1”, “key_2” : “value_2”}. To get the second value from that dictionary I would use Get value for “key_2” from dictionary. Dictionaries can be stored as text and later retrieved from text. So they are a useful way of storing data in text only fields like the notes of a reminder!

Download Memory

14 thoughts on “How to make persistent variables for iOS and iPadOS Shortcuts | Hacking Shortcuts

  1. I’ve cycled through multiple implementations to create a radio seek button, but didn’t think to use reminders, that’s unique! I was using the app data jar for a bit but it can get out of sync and I’ve not figured out why. More recently I’ve used python to load/save the state and marshal it over to shortcuts, just save out your variables from Pythonista Anjou are done.

    Liked by 1 person

    • Thanks. Yes Pythonista is a top app. I have that installed everywhere I go. I use that in conjunction with shortcuts for creating a long term data store. Yes I could use it for variable storage but I find Reminders method to be a bit more lightweight.

      Like

  2. Is this no longer working?

    I have the list called reference and I verified that the memory shortcut recognizes that when I downloaded it but it doesn’t seem to create the variable storage todo for some reason…

    Like

    • I tested the shortcut by starting again with it. It works for me. Please check capitalisation of the name. I.e. “reference” does not match “Reference”. The Shortcut looks for “Reference” by default.
      I realised that my latest update to the Shortcut did not propagate out via my share link. The latest update gives a text field for the list name and prompts the user for the name on installation. I’ve updated the share link.

      Oh yeah and please check the privacy pane on the Shortcut to check it has access to Reminders and can run other Shortcuts.
      ⓘ > Privacy

      Like

  3. Thanks a lot!
    Your Memory shortcut allowed me to check if Driving is on before triggering an automation that opens my garage gate (and does other handy things) when I enter a zone close to home.
    I control the state of a global is_driving flag by calling your Memory shortcut from automations “When turning Driving off/on”.

    Liked by 1 person

  4. This is exactly what I was looking for. I implemented solution 2 but I had to clean the note too often to keep shortcut run quickly.
    I will try solution 3.
    Thanks.

    Liked by 1 person

  5. Hi. I am struggling to implement this. I understand the concept of dictionaries but it would help to see a sample reminder note containing a dictionary and a sample shortcut which implements your memory subroutine. Thanks.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s