iOS DSL guide for complex automations

The Automation DSL can be used to create automations that are more complex than those discussed in DSL guide - basic automations on iOS.

Sequential with multiple actions

Sequential with multiple actions

An automation can do more than one thing. For example, in place of the single action node, you could have multiple action nodes, which run in sequential order:

import GoogleHomeSDK
import GoogleHomeTypes

automation (
...
) {

  starter(...)
  condition {...}
  action {...}
  action {...}
  action {...}

}

Sequential with multiple parallel actions

Sequential with multiple parallel actions

If you place multiple action nodes in a parallel node, the actions execute concurrently.

import GoogleHomeSDK
import GoogleHomeTypes

automation (
...
) {

  starter(...)
  condition {...}
  parallel {
    action {...}
    action {...}
    action {...}
  }

}

If there are action nodes in the sequential node that come after the parallel node, they wait to execute until all the nodes within the parallel node have finished executing.

Delays

You can introduce pauses in your automations using the delay(for:) method, which takes a Duration argument representing how long to pause before continuing execution. The pause duration may be as short as five seconds or as long as 24 hours.

For example, to toggle a light four times with a five-second pause between each toggle:

typealias OnOffLightDevice = Matter.OnOffLightDeviceType
typealias OnOffTrait = Matter.OnOffTrait

sequential {
  action(light, OnOffLightDevice.self) { OnOffTrait.toggle() }
  delay(for:.seconds(5))
  action(light, OnOffLightDevice.self) { OnOffTrait.toggle() }
  delay(for:.seconds(5))
  action(light, OnOffLightDevice.self) { OnOffTrait.toggle() }
  delay(for:.seconds(5))
  action(light, OnOffLightDevice.self) { OnOffTrait.toggle() }
}

Trigger suppression

Trigger suppression is a capability that allows your automation to ignore a starter for a specified period of time after the initial triggering event. For example, if the automation has a starter that's triggered by motion detection, and if you specify a trigger suppression duration of five minutes, then when the starter triggers, it won't trigger again for the next five minutes. This prevents the automation from rapidly triggering over and over.

To apply trigger suppression to your automation, use the suppress(for:) keyword with a Duration argument representing how long to wait before responding to subsequent triggers. The suppression duration may be as short as five seconds or as long as 24 hours.

typealias OccupancySensorDevice = Matter.OccupancySensorDeviceType
typealias OnOffLightDevice = Matter.OnOffLightDeviceType
typealias MotionDetectionTrait = Google.MotionDetectionTrait
typealias OnOffTrait = Matter.OnOffTrait

automation {
  let starterNode = starter(device, OccupancySensorDevice.self, MotionDetectionTrait.self)
  starterNode
  suppress(for: .seconds(30 * 60)  // 30 minutes
  action(light, OnOffLightDevice.self) { OnOffTrait.toggle() }
}

Note that trigger suppression affects all starters in an automation that precede the Suppression.

Set trait attributes in an action

To set the value of a trait attribute:

  1. Create an update node within an action node, including the relevant trait as an argument to the update node:
    action(deviceReference, deviceType) {
      update(trait) {
    
      }
    }
    
  2. Within the update node, for each attribute to be modified, use a mutator function, and pass it the new value. To form the name of the mutator function:
    1. Capitalize the name of the attribute
    2. Prefix it with the word set.
    For example, to update an attribute called defaultMoveRate, you'd use a mutator function called setDefaultMoveRate.

Note that an update node can have multiple mutator functions. Here's an example where two attributes are updated:

typealias FanDeviceType = Matter.FanDeviceType
typealias FanControlTrait = Matter.FanControlTrait

action(fan, FanDeviceType.self) {
  update(FanControlTrait.self) {
    $0.setFanMode(.on)
  }
}