Filter code lets you add custom JavaScript logic to your Applets. You can use it to control when Actions run, change the values sent to Actions, or build more advanced automations based on Trigger and Query data.
Filter code runs after an Applet's Trigger and Queries, but before any Delay or Actions.
Some of the things you can do with filter code include:
- Skip an Action or multiple Actions when certain conditions are met.
- Change the values of Action fields before an Action runs.
- Use data from your Trigger or Queries to create more advanced logic.
- Use JavaScript to build conditions that aren't available through the standard Applet configuration.
Here are some examples of what you can do with filter code.
Running Applets only during certain times
If you have Applets that notify you about new events or updates from a service, but these notifications are not important enough to wake you up at night, you can use filter code to skip notifications outside your waking hours. This is one of the most common use cases and fortunately, a simple one to set up.
let currentHour = Meta.currentUserTime.hour();
if (currentHour < 9 || currentHour >= 23) {
Email.sendMeEmail.skip(`It's ${currentHour}, only running between 9am and 11pm`);
// You can pass a message to skip() and it will be recorded in the Applet's
// activity feed when an action is skipped. This is useful for debugging.
}In the filter code, you have access to the time object Meta.currentUserTime, which is technically a momentjs object. It has a lot of built-in methods for getting and manipulating the different parts that make up the current time and date (taking into account the time zone the user set in their IFTTT account setting).
For this example, you need to get the integer value, the number that represents the current hour (in 24h format). The hour() method provides that, so we assign the value to the currentHour variable on the first line.
We then build a simple IF statement that checks two conditions: either the value of currentHour is less than 9 OR it’s greater than or equal to 23 (11pm). The || symbols represent the OR logic we need in this example.
Lastly, in the { } we have just one command - skip the email Action. If the current hour is outside the 9am - 11pm interval, this code will stop the Applet. Any Actions afterward will not run.
Running Actions every other week
Another interesting Applet idea that is now possible with filter code is running an Action on a bi-weekly schedule. If you want your Applet to run on certain days of the week, then the Date & Time service can help, but it’s powerless if you have a bi-weekly Action(s). It’s also not useful when there is another Trigger that needs to fire, and the schedule is just another layer of logic on top of it.
// Filter code to check the week number and if the week number is odd,
// the actions will be skipped.
// This code makes use of the moment.js function moment().week()
// which returns the current week number in the number format.
let thisWeek = Meta.currentUserTime.week();
//Change 0 to 1 to make it run on odd weeks instead
if (thisWeek % 2 != 0) {
IfNotifications.sendNotification.skip(`Only running on even weeks. Week number ${thisWeek}`);
}Running Actions based on Trigger data
If you have a few Applets that take alerts (from a monitoring, security, or weather service, for example) and use different Actions to notify you, you can combine them in one Applet and build the filter code logic to skip some notification methods if the alert is not important enough.
In this example, we have an Applet that takes the current temperature from the Weather Underground Trigger and three Actions - sendMeEmail, sendMeText and sendNotification. The logic is then built to skip the text message if the temperature does not rise above 90, and to skip both a text and a push notification if it’s still under 75. All three Actions will fire if the temperature rises above 90.
let currentTemp = Number(Weather.currentTemperatureRisesAbove.TempFahrenheit);
if (currentTemp < 90 && currentTemp > 75) {
Sms.sendMeText.skip(`Temperature is ${currentTemp}`);
}
else if (currentTemp <= 75) {
IfNotifications.sendNotification.skip(`Temperature is ${currentTemp}`);
Sms.sendMeText.skip(`Temperature is ${currentTemp}`);
}Searching for a keyword and skipping an Action
This is a simple starter filter code that searches for a keyword in a Google Calendar event description and skips the notification Action if the keyword is not found.
let str = GoogleCalendar.anyEventStarts.Description;
let searchTerm = 'IFTTT';
if (!str.includes(searchTerm)) {
IfNotifications.sendNotification.skip("Not found!")
}The code works by checking whether the search term appears anywhere in the calendar event description using .includes(). If the term isn't found, the IF statement skips the notification accordingly.
Updating Action fields
In this example, there is an array of colors and a way of picking a random color from the list. This color is then used with a Lifx.color Action to change the color of your lights.
let colors = [
"#FF8400",
"#FF0000",
"#15FF00",
"#FF00D4",
"#00D4FF",
"#003CFF"
]
let index = Math.floor((Math.random() * colors.length))
Lifx.color.setAdvancedOptions('color: ' + colors[index] + '; brightness: 1; duration: 12')
Adding filter code to an Applet
You can add filter code when creating or editing an Applet. For instructions, see How to add filter code.
If you're not comfortable writing JavaScript, the filter code editor includes tools that can help you generate code. You can choose from generators for common use cases or describe what you want to accomplish and use the AI filter code generator to create the code for you.