Quick Tasker tip: Detecting when a variable based on a text file is empty

Screenshot 2013 04 25 03 42 31 - for some reason we don't have an alt tag hereMore and more of my Tasker creations are becoming complicated enough to deal with data on a long term basis, which means that storing it in text files is needed in order to allow it to survive. When dealing with text files in Tasker, there is a very simple way to check if a variable based on a text file is empty, and act accordingly.

There are essentially two ways a file can be empty: it’s actually empty, or it contains a single space. The latter typically happens when you wipe it using Tasker, since Tasker will complain about the lack of text if you try to configure a Write File that has nothing in it, and deleting the file would make Tasker complain it doesn’t exist. Furthermore, if you write a variable to a file, you should in fact be using a space as the de facto “empty” state, because otherwise you’ll be writing the variable name to the file. If you manipulate it externally, however, the file will actually be empty.

To account for these two scenarios, you need to use two If actions on the variable you Read File your data into after you do so. The first one is for the name of the variable you just read the file into, so if you did Read File into a variable %Filedata, you need to use the following If condition:

If %Filedata (doesn’t match) *filedata

This If statement will account for the scenario where the file is actually empty.  In that situation, nothing will be read into the variable, the variable will be empty, and the variable will only contain its own name. The star at the beginning is a wildcard, matching anything, and allows us to match the name of the variable without typing in the %, which would make it try to match the value of the variable against itself- which would be true all the time.

The other scenario isn’t much different, as you essentially just need to natch an empty space, but actually getting Tasker to do just that isn’t as straight forward as you might think. It won’t let you put just a single space into the If condition value field, which means that you have two options:

  1. Create a variable that contains a single space, e.g. %Blank, and compare your variable to that: If %Filedata (doesn’t match) %Blank
  2. Check for the number of characters (at least two) using the + wildcard: If %Filedata (matches) ++

The second method uses the + wildcard, which means “at least one character”. Using two of them means “at least two characters”, and since the operator here is “matches”, it would block any spaces, line shifts, or other single characters or symbols.

The first method is a bit more specific than the second, as it won’t match anything other than a space, and it also requires you to set up a variable just to use it. It does however have one major advantage, and that’s that it uses the same operator (“doesn’t match”) as the first scenario filter. This means that you only need a single If condition to cover both, which would be like this:

If %Filedata (doesn’t match) *filedata*/%Blank

The / symbol means “or”, which means that the above statement checks for both of the two scenarios, individually. It will only allow the dependent actions to run if both are untrue.

Screenshot 2013 04 25 03 43 53 - for some reason we don't have an alt tag hereThis has an added bonus: you can invert it, using “matches” as the operator instead of “doesn’t match”. This would make the dependent actions run only if the file is indeed empty.

It’s important to understand that you cannot invert your setup if you use the ++ method, as that uses a different operator than the If condition that checks to see if the variable is empty. Because of this, you would need two separate If conditions, each with separate operators. The relationship between them would then be “and”, meaning that it would both have to contain a single space and be completely empty to get through the filter, which just isn’t possible, since they’re mutually exclusive

Despite this, I tend to go with the ++ method, since that also catches any line shifts that have ninja’d their way in there. It doesn’t really matter though unless you need the advantages of one of the other, and just being aware of the logic that makes this work the way it does can be helpful.

This is a simple little trick, but also key to some projects. By checking if a file is empty, I can check to see if my todo list is empty, and control the alert actions based on it. It also allows me to account for empty files when displaying lists, but that’s a story for another day…

tasker banner - for some reason we don't have an alt tag here

Pocketables does not accept targeted advertising, phony guest posts, paid reviews, etc. Help us keep this way with support on Patreon!
Become a patron at Patreon!

Andreas Ødegård

Andreas Ødegård is more interested in aftermarket (and user created) software and hardware than chasing the latest gadgets. His day job as a teacher keeps him interested in education tech and takes up most of his time.

Avatar of Andreas Ødegård