Skip to content

Instantly share code, notes, and snippets.

@JetForMe
Created November 15, 2020 00:55
Show Gist options
  • Save JetForMe/a635e4e5dd53e6d6ab1e8f9500fc8e60 to your computer and use it in GitHub Desktop.
Save JetForMe/a635e4e5dd53e6d6ab1e8f9500fc8e60 to your computer and use it in GitHub Desktop.
Home automation DSL in Swift
/**
ExampleAutomationScript.swift
ZWaveTest
Created by Rick Mann on 2020-11-11.
Copyright © 2020 Latency: Zero, LLC. All rights reserved.
This file was born as a scratchpad to help me think about what I wanted this
automation language to look like.
*/
// Create devices so we can set up rules for them.
// Some switches that can be turned on or off, and queried for their current on/off state…
let overheadLights = Device(serviceID: "urn:upnp-org:serviceId:SwitchPower1", number: 5)
let deskLight = Device(serviceID: "urn:upnp-org:serviceId:SwitchPower1", number: 34)
let waterHeater = Device(serviceID: "urn:upnp-org:serviceId:SwitchPower1", number: 25)
// We want a lot of our rules to depend on whether or not someone is present.
// This defines occupancy by whether or not the overhead lights are on. An
// alternative occupancy might be "Rick’s iPhone is present."
let occupied = overheadLights.on
// Schedule periods can be defined…
let summer = June...September
let winter = October...May
let onPeak = summer.weekdays(1600...2100)
let midPeak = summer.weekends(1600...2100) || winter.daily(1600...2100)
let offPeak = summer.weekdays(2100...1600) || winter.daily(2100...0800)
let superOffPeak = winter.daily(0800...1600)
// Create a rule that turns on the water heater any time the place is
// occupied, except during peak electricity rate ToU periods…
waterHeater.on = occupied && !onPeak
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment