運算子可讓你根據特定值檢查特徵屬性的值、相互比較,以及合併 condition 節點中使用的運算式。
運算子可透過下列 import 陳述式提供:
import com.google.home.automation.and
import com.google.home.automation.between
import com.google.home.automation.contains
import com.google.home.automation.equals
import com.google.home.automation.greaterThan
import com.google.home.automation.greaterThanOrEquals
import com.google.home.automation.lessThan
import com.google.home.automation.not
import com.google.home.automation.notEquals
import com.google.home.automation.or
比較運算子
介於
如果運算式 1 的值介於運算式 2 和運算式 3 之間 (含頭尾),則會評估為 true。運算式的排名會因資料類型而異。數字和字串等簡單資料類型的排序方式與 Kotlin 相同。
| 運算式 1 | 運算式 2 | 運算式 3 | 結果 |
|---|---|---|---|
6 |
1 |
3 |
false |
2 |
1 |
3 |
true |
DSL 範例
val time = stateReader<_>(structure, Time)
condition() {
expression = time.currentTime
.between(
time.sunsetTime,
time.sunriseTime)
}
搭配 between() 使用日期
使用 between() 運算子時,您可以指定日期範圍:
val exp2 =
time.currentDate.between(
LocalDate.of(2025, Month.OCTOBER, 1),
LocalDate.of(2025, Month.DECEMBER, 15),
)
不過,您不一定要輸入完整日期。自動化動作 DSL 也可讓您以多種方式表示日期範圍:
- 僅使用年和月:
val exp2 =
time.currentDate.yearMonth.between(
YearMonth.of(2024, Month.OCTOBER),
YearMonth.of(2026, Month.JANUARY),
)
- 僅使用月和日:
val exp2 =
time.currentDate.monthDay.between(
MonthDay.of(Month.OCTOBER, 1),
MonthDay.of(Month.DECEMBER, 15),
)
- 僅使用每月第幾天:
val exp2 = time.currentDate.day.between(1, 15)
等於
如果運算式 1 等於運算式 2,則會評估為 true。
| 運算式 1 | 運算式 2 | 結果 |
|---|---|---|
6 |
1 |
false |
2 |
2 |
true |
DSL 範例
washer.operationalState equals STOPPED
greaterThan
如果運算式 1 大於運算式 2,則評估為 true。
| 運算式 1 | 運算式 2 | 結果 |
|---|---|---|
6 |
1 |
true |
1 |
6 |
false |
DSL 範例
( blindsPosition.currentPositionLift greaterThan 0u )
greaterThanOrEquals
如果運算式 1 大於或等於運算式 2,則計算結果為 true。
| 運算式 1 | 運算式 2 | 結果 |
|---|---|---|
8 |
6 |
true |
6 |
6 |
true |
1 |
6 |
false |
DSL 範例
( starterNode.measuredValue greaterThanOrEquals 50 )
lessThan
如果運算式 1 小於運算式 2,則會評估為 true。
| 運算式 1 | 運算式 2 | 結果 |
|---|---|---|
6 |
1 |
false |
1 |
6 |
true |
DSL 範例
time.currentTime lessThan LocalTime.of(22,0,0,0)
lessThanOrEquals
如果運算式 1 小於或等於運算式 2,則會評估為 true。
| 運算式 1 | 運算式 2 | 結果 |
|---|---|---|
8 |
6 |
false |
6 |
6 |
true |
1 |
6 |
true |
DSL 範例
( starterNode.measuredValue lessThanOrEquals 25 )
notEquals
如果運算式 1 不等於運算式 2,則求值結果為 true。
| 運算式 1 | 運算式 2 | 結果 |
|---|---|---|
6 |
1 |
true |
1 |
6 |
true |
2 |
2 |
false |
DSL 範例
occupancyStateChange.occupied notEquals 0
算術運算子
新增
加法運算子 ( +)。
DSL 範例
var totalCount = 0
...
totalCount = totalCount + 1
減號
減法運算子 ( -)。
DSL 範例
var countdown = 10
...
countdown = countdown - 1
相乘
乘法運算子 ( *)。
DSL 範例
val millis = seconds * 1000
除號
除法運算子 ( /)。
DSL 範例
val rpm = revolutions / minutes
邏輯運算子
和
在邏輯 AND 運算式中結合兩個運算式,當兩個運算式都是 true 時,評估結果為 true。
| 運算式 1 | 運算式 2 | 結果 |
|---|---|---|
false |
false |
false |
true |
false |
false |
false |
true |
false |
true |
true |
true |
DSL 範例
((device.occupied notEquals 0) and
time.currentTime.between(time.sunriseTime, time.sunsetTime))
not
反轉運算式的邏輯值。
| 運算式 | 結果 |
|---|---|
true |
false |
false |
true |
DSL 範例
time.currentTime not (between(time.sunriseTime, time.sunsetTime))
或
將兩個運算式合併為邏輯 OR 運算式。
| 運算式 1 | 運算式 2 | 結果 |
|---|---|---|
false |
false |
false |
true |
false |
true |
false |
true |
true |
DSL 範例
(time.currentTime equals LocalTime.of(10,0,0,0)) or
(time.currentTime equals LocalTime.of(22,0,0,0))