Включение устройства при включении или выключении другого устройства
import com.google.home.Home
import com.google.home.HomeDevice
import com.google.home.HomeManager
import com.google.home.Id
import com.google.home.Structure
import com.google.home.automation.Action
import com.google.home.automation.Automation
import com.google.home.automation.Condition
import com.google.home.automation.Equals
import com.google.home.automation.Node
import com.google.home.automation.SequentialFlow
import com.google.home.automation.Starter
import com.google.home.matter.standard.OnOff
import com.google.home.matter.standard.OnOffLightDevice
...
automation {
sequential {
val starterNode = starter<_>(device1, OnOffLightDevice, OnOff)
val device2State = stateReader<_>(device2, OnOffLightDevice, OnOff)
condition {
// Only send the command if device2 is off
expression = device2State.onOff equals false
}
// turn on device 2
action(device2, OnOffLightDevice) { command(OnOff.on()) }
}
}
Закройте штору, если слишком ярко
import com.google.home.Home
import com.google.home.HomeDevice
import com.google.home.HomeManager
import com.google.home.Id
import com.google.home.Structure
import com.google.home.automation.Action
import com.google.home.automation.And
import com.google.home.automation.Automation
import com.google.home.automation.Condition
import com.google.home.automation.Equals
import com.google.home.automation.Node
import com.google.home.automation.SequentialFlow
import com.google.home.automation.Starter
import com.google.home.automation.StateReader
import com.google.home.matter.standard.IlluminanceMeasurement
import com.google.home.matter.standard.IlluminanceMeasurement.measuredValue
import com.google.home.matter.standard.LightSensorDevice
import com.google.home.matter.standard.WindowCovering
import com.google.home.matter.standard.WindowCovering.currentPositionLift
...
automation {
sequential {
val starterNode = starter<_>(device1,
LightSensorDevice,
IlluminanceMeasurement)
val blindsPosition = stateReader<_>(blinds, WindowCoveringDevice, WindowCovering)
condition {
// only send the command if
// the blinds are open
( blindsPosition.currentPositionLift greaterThan 0u ) and
// it's brighter than a certain threshold value
( starterNode.measuredValue greaterThan 0xAAFF )
}
// close the blind
action(blinds, WindowCoveringDevice) { command(WindowCovering.downOrClose()) }
}
}
Включите вентилятор, когда температура превышает пороговое значение.
import com.google.home.Home
import com.google.home.HomeDevice
import com.google.home.HomeManager
import com.google.home.Id
import com.google.home.Structure
import com.google.home.automation.Action
import com.google.home.automation.And
import com.google.home.automation.Automation
import com.google.home.automation.Condition
import com.google.home.automation.Equals
import com.google.home.automation.Node
import com.google.home.automation.SequentialFlow
import com.google.home.automation.Starter
import com.google.home.automation.StateReader
import com.google.home.matter.standard.FanDevice
import com.google.home.matter.standard.OnOff
import com.google.home.matter.standard.OnOffTrait.Attributes.onOff
import com.google.home.matter.standard.TemperatureMeasurement
import com.google.home.matter.standard.TemperatureMeasurement.measuredValue
import com.google.home.matter.standard.TemperatureSensorDevice
...
automation {
sequential {
val starterNode = starter<_>(thermometer,
TemperatureSensorDevice,
TemperatureMeasurement)
val fanStateReaderNode = stateReader<_>(fan, FanDevice, OnOff)
condition {
// only send the command if the temperature is above 78F
(starterNode.measuredValue greaterThan 78) and
// the fan is off
fanStateReaderNode.onOff equals false
}
// turn on the fan
action(fan, FanDevice) { command(OnOff.on()) }
}
}
Включайте освещение и подавайте звуковой сигнал при обнаружении движения.
import com.google.home.Home
import com.google.home.HomeDevice
import com.google.home.HomeManager
import com.google.home.Id
import com.google.home.Structure
import com.google.home.automation.Action
import com.google.home.automation.Automation
import com.google.home.automation.Condition
import com.google.home.automation.Equals
import com.google.home.automation.Node
import com.google.home.automation.ParallelFlow
import com.google.home.automation.SequentialFlow
import com.google.home.automation.Starter
import com.google.home.automation.StateReader
import com.google.home.google.AssistantBroadcast
import com.google.home.google.MotionDetection
import com.google.home.matter.standard.OccupancySensor
import com.google.home.matter.standard.OnOff
import com.google.home.matter.standard.OnOffTrait.Attributes.onOff
import com.google.home.matter.standard.OnOffLightDevice
import com.google.home.matter.standard.SpeakerDevice
import java.time.Duration
...
automation {
sequential {
val starterNode = starter<_>(device, OccupancySensorDevice, MotionDetection)
// only proceed if there is currently motion taking place
condition { starterNode.motionDetectionEventInProgress equals true }
// ignore the starter for one minute after it was last triggered
suppressFor(Duration.ofMinutes(1))
// make three announcements three seconds apart
action(device, SpeakerDevice) {
command(AssistantBroadcast.broadcast("Intruder detected!"))
}
delayFor(Duration.ofSeconds(3))
action(device, SpeakerDevice) {
command(AssistantBroadcast.broadcast("Intruder detected!"))
}
delayFor(Duration.ofSeconds(3))
action(device, SpeakerDevice) {
command(AssistantBroadcast.broadcast("Intruder detected!"))
}
// flash lights every two seconds
action(light, OnOffLightDevice) { command(OnOff.toggle()) }
delayFor(Duration.ofSeconds(2))
action(light, OnOffLightDevice) { command(OnOff.toggle()) }
delayFor(Duration.ofSeconds(2))
action(light, OnOffLightDevice) { command(OnOff.toggle()) }
delayFor(Duration.ofSeconds(2))
action(light, OnOffLightDevice) { command(OnOff.toggle()) }
delayFor(Duration.ofSeconds(2))
action(light, OnOffLightDevice) { command(OnOff.toggle()) }
delayFor(Duration.ofSeconds(2))
action(light, OnOffLightDevice) { command(OnOff.toggle()) }
}
}
Откройте жалюзи за 15 минут до восхода солнца.
import com.google.home.Home
import com.google.home.HomeDevice
import com.google.home.HomeManager
import com.google.home.Id
import com.google.home.Structure
import com.google.home.automation.Action
import com.google.home.automation.And
import com.google.home.automation.Automation
import com.google.home.automation.Condition
import com.google.home.automation.Equals
import com.google.home.automation.LessThanOrEquals
import com.google.home.automation.ManualStarter
import com.google.home.automation.Node
import com.google.home.automation.SequentialFlow
import com.google.home.automation.Starter
import com.google.home.automation.StateReader
import com.google.home.google.Time
import com.google.home.google.Time.sunriseTime
import com.google.home.matter.standard.WindowCovering
import java.time.Duration
import java.time.LocalTime
...
automation {
sequential {
// 15 minutes before sunrise
val unused =
starter<_>(structure, Time.ScheduledTimeEvent) {
parameter(Time.ScheduledTimeEvent.solarTime(
SolarTimeStruct(SolarTimeType.Sunrise,
java.time.Duration.ofMinutes(-15))))
}
}
sequential {
manualStarter()
}
// simultaneously open the five blinds
parallel {
action(blind1, WindowCovering) { command(WindowCovering.upOrOpen()) }
action(blind2, WindowCovering) { command(WindowCovering.upOrOpen()) }
action(blind3, WindowCovering) { command(WindowCovering.upOrOpen()) }
action(blind4, WindowCovering) { command(WindowCovering.upOrOpen()) }
action(blind5, WindowCovering) { command(WindowCovering.upOrOpen()) }
}
}
}
Откройте жалюзи в 7:00 утра, за исключением случаев, когда слишком ярко и/или слишком жарко.
Обратите внимание, что в этом примере используется Time.ScheduledTimeEvent
, что требует, чтобы структуре был назначен адрес с помощью Google Home app (GHA) .
import com.google.home.Home
import com.google.home.HomeDevice
import com.google.home.HomeManager
import com.google.home.Id
import com.google.home.Structure
import com.google.home.automation.Action
import com.google.home.automation.And
import com.google.home.automation.Automation
import com.google.home.automation.Condition
import com.google.home.automation.Equals
import com.google.home.automation.LessThanOrEquals
import com.google.home.automation.ManualStarter
import com.google.home.automation.Node
import com.google.home.automation.SequentialFlow
import com.google.home.automation.Starter
import com.google.home.automation.StateReader
import com.google.home.google.Time
import com.google.home.matter.standard.IlluminanceMeasurement
import com.google.home.matter.standard.TemperatureMeasurement
import com.google.home.matter.standard.TemperatureMeasurement.measuredValue
import com.google.home.matter.standard.TemperatureSensorDevice
import com.google.home.matter.standard.WindowCovering
import java.time.Duration
import java.time.LocalTime
...
automation {
sequential {
select {
sequential {
// run the automation daily at 7am
val earlyMorning = starter<_>(structure, Time.ScheduledTimeEvent) {
parameter(Time.ScheduledTimeEvent.clockTime(LocalTime.of(7, 0, 0, 0)))
}
val temperature = starter<_>(device, TemperatureSensorDevice, TemperatureMeasurement)
val brightness = starter<_>(device1, IlluminanceMeasurement)
// don't run more often than every 15 minutes
suppressFor(Duration.ofMinutes(15))
condition {
// temperature is below 32C/90F
(temperature.measuredValue lessThanOrEquals 32)
or
// too bright
(brightness.measuredValue greaterThan 0xAAFF)
}
}
sequential {
manualStarter()
}
}
// simultaneously open the five blinds
parallel {
action(blind1, WindowCovering) { command(WindowCovering.upOrOpen()) }
action(blind2, WindowCovering) { command(WindowCovering.upOrOpen()) }
action(blind3, WindowCovering) { command(WindowCovering.upOrOpen()) }
action(blind4, WindowCovering) { command(WindowCovering.upOrOpen()) }
action(blind5, WindowCovering) { command(WindowCovering.upOrOpen()) }
}
}
}
Если индикатор горит, выключите вентилятор и измените настройку скорости.
import com.google.home.Home
import com.google.home.HomeClient
import com.google.home.HomeDevice
import com.google.home.HomeManager
import com.google.home.Id
import com.google.home.Structure
import com.google.home.automation.Action
import com.google.home.automation.Automation
import com.google.home.automation.Command
import com.google.home.automation.Condition
import com.google.home.automation.Equals
import com.google.home.automation.Node
import com.google.home.automation.SequentialFlow
import com.google.home.automation.Starter
import com.google.home.automation.StateReader
import com.google.home.automation.Update
import com.google.home.matter.standard.DimmableLightDevice
import com.google.home.matter.standard.FanControl
import com.google.home.matter.standard.FanControlTrait.FanModeEnum
import com.google.home.matter.standard.FanDevice
import com.google.home.matter.standard.OnOff
import com.google.home.matter.standard.OnOffTrait.Attributes.onOff
...
automation {
name = "Turn off the fan when the light is turned on."
description = "If light is on, turn off the fan and change the speed setting."
isActive = true
sequential {
// When the light's OnOff state changes...
val starterNode = starter<_>(device, DimmableLightDevice, OnOff)
// ...and if the light is turned on...
condition() {
expression = starterNode.onOff equals true
}
// ...turn off the fan and change the speed.
action(device, FanDevice) {
update(FanControl) {
setFanMode(FanModeEnum.Off)
setPercentSetting(20u)
}
}
}
}
Включите свет, если помещение занято
import com.google.home.Home
import com.google.home.HomeClient
import com.google.home.HomeDevice
import com.google.home.HomeManager
import com.google.home.Id
import com.google.home.Structure
import com.google.home.automation.Action
import com.google.home.automation.Automation
import com.google.home.automation.Command
import com.google.home.automation.Condition
import com.google.home.automation.Equals
import com.google.home.automation.Node
import com.google.home.automation.SequentialFlow
import com.google.home.automation.Starter
import com.google.home.automation.StateReader
import com.google.home.google.AreaPresenceState
import com.google.home.google.AreaPresenceState.PresenceStateOccupied
import com.google.home.google.AreaPresenceStateTrait.PresenceState
import com.google.home.matter.standard.DimmableLightDevice
import com.google.home.matter.standard.OnOff
import com.google.home.matter.standard.OnOffTrait.Attributes.onOff
...
automation {
description = "If the area is occupied, turn the light on."
isActive = true
sequential {
// If the presence state changes...
val starterNode = starter<_>(structure, AreaPresenceState)
// ...and if the area is occupied...
condition() {
expression =
starterNode.presenceState equals
AreaPresenceState.PresenceStateOccupied
}
// ...turn the light on.
action(device, DimmableLightDevice) { command(OnOff.on()) }
}
}
Выключите свет, если никого нет дома
import com.google.home.Home
import com.google.home.HomeClient
import com.google.home.HomeDevice
import com.google.home.HomeManager
import com.google.home.Id
import com.google.home.Structure
import com.google.home.automation.Action
import com.google.home.automation.Automation
import com.google.home.automation.Command
import com.google.home.automation.Condition
import com.google.home.automation.Equals
import com.google.home.automation.Node
import com.google.home.automation.SequentialFlow
import com.google.home.automation.Starter
import com.google.home.automation.StateReader
import com.google.home.google.AreaAttendanceState
import com.google.home.google.AreaAttendanceStateTrait.AttendanceState
import com.google.home.matter.standard.DimmableLightDevice
import com.google.home.matter.standard.OnOff
import com.google.home.matter.standard.OnOffTrait.Attributes.onOff
...
automation {
sequential {
val starterNode = starter<_>(device, DimmableLightDevice, OnOff)
val stateReaderNode = stateReader<_>(structure, AreaAttendanceState)
condition() {
val expr1 = starterNode.onOff equals true
val expr2 =
stateReaderNode.attendanceState equals
AttendanceState.AttendanceStateNoHouseholdMembers
// If the light is turned on and no one from the household is present
expression = expr1 and expr2
}
// turn the light off
action(device, DimmableLightDevice) { command(OnOff.off()) }
}
}
Отправить уведомление в указанное время
Обратите внимание, что в этом примере используется Time.ScheduledTimeEvent
, что требует, чтобы структуре был назначен адрес с помощью Google Home app (GHA) .
import com.google.home.Home
import com.google.home.HomeClient
import com.google.home.HomeDevice
import com.google.home.HomeManager
import com.google.home.Id
import com.google.home.Structure
import com.google.home.automation.Action
import com.google.home.automation.Automation
import com.google.home.automation.Command
import com.google.home.automation.Condition
import com.google.home.automation.Equals
import com.google.home.automation.Node
import com.google.home.automation.SequentialFlow
import com.google.home.automation.Starter
import com.google.home.automation.StateReader
import com.google.home.google.Notification
import com.google.home.google.Time
import com.google.home.google.Time.ScheduledTimeEvent
import com.google.home.google.Time.ScheduledTimeEvent.clockTime
import java.time.LocalTime
...
automation {
sequential {
val email1 = "your.email@gmail.com"
// If the time is 1:01:01:001
val unused =
starter<_>(structure, Time.ScheduledTimeEvent) {
parameter(Time.ScheduledTimeEvent.clockTime(
LocalTime.of(1, 1, 1, 1)))
}
action(structure) {
// send a notification to the email list
command(
Notification.sendNotifications(
"title",
{
body = "body"
},
)
)
}
}
}
Передача сообщения в указанное время, если свет горит
Обратите внимание, что в этом примере используется Time.ScheduledTimeEvent
, что требует, чтобы структуре был назначен адрес с помощью Google Home app (GHA) .
import com.google.home.Home
import com.google.home.HomeClient
import com.google.home.HomeDevice
import com.google.home.HomeManager
import com.google.home.Id
import com.google.home.Structure
import com.google.home.automation.Action
import com.google.home.automation.Automation
import com.google.home.automation.Command
import com.google.home.automation.Condition
import com.google.home.automation.Equals
import com.google.home.automation.Node
import com.google.home.automation.SequentialFlow
import com.google.home.automation.Starter
import com.google.home.automation.StateReader
import com.google.home.google.AssistantBroadcast
import com.google.home.google.Time
import com.google.home.google.Time.ScheduledTimeEvent
import com.google.home.google.Time.ScheduledTimeEvent.clockTime
import com.google.home.matter.standard.DimmableLightDevice
import com.google.home.matter.standard.OnOff
import com.google.home.matter.standard.OnOffTrait.Attributes.onOff
import java.time.LocalTime
...
automation {
sequential {
// If the time is 12:00...
val unused =
starter<_>(structure, Time.ScheduledTimeEvent) {
parameter(Time.ScheduledTimeEvent.clockTime(LocalTime.of(12, 0, 0, 0)))
}
val stateReaderNode = stateReader<_>(device, DimmableLightDevice, OnOff)
// ...and the light is on...
condition() { expression = stateReaderNode.onOff equals true }
// ...broadcast a message over any devices that support Google Assistant.
action(structure) {
command(AssistantBroadcast.broadcast("It's midday."))
}
}
}
Воспроизведение музыки в указанное время, когда свет включен
Обратите внимание, что в этом примере используется Time.ScheduledTimeEvent
, что требует, чтобы структуре был назначен адрес с помощью Google Home app (GHA) .
import com.google.home.Home
import com.google.home.HomeClient
import com.google.home.HomeDevice
import com.google.home.HomeManager
import com.google.home.Id
import com.google.home.Structure
import com.google.home.automation.Action
import com.google.home.automation.Automation
import com.google.home.automation.Command
import com.google.home.automation.Condition
import com.google.home.automation.Equals
import com.google.home.automation.Node
import com.google.home.automation.SequentialFlow
import com.google.home.automation.Starter
import com.google.home.automation.StateReader
import com.google.home.google.AssistantFulfillment
import com.google.home.google.Time
import com.google.home.google.Time.ScheduledTimeEvent
import com.google.home.google.Time.ScheduledTimeEvent.clockTime
import com.google.home.matter.standard.DimmableLightDevice
import com.google.home.matter.standard.SpeakerDevice
import com.google.home.matter.standard.OnOff
import com.google.home.matter.standard.OnOffTrait.Attributes.onOff
import java.time.LocalTime
...
automation {
sequential {
// When the time is 3:15pm...
val unused =
starter<_>(structure, Time.ScheduledTimeEvent) {
parameter(Time.ScheduledTimeEvent.clockTime(LocalTime.of(15, 15, 0, 0)))
}
val stateReaderNode = stateReader<_>(device, DimmableLightDevice, OnOff)
// ...and the light's OnOff state is on...
condition() { expression = stateReaderNode.onOff equals true }
// ...tell Google Assistant to play music
action(device, SpeakerDevice) {
command(AssistantFulfillment.okGoogle("Play some music"))
}
}
}