I am a new user, and spent easily 40 hours learning the system to get this to work. I have a Pi Zero driving a SainSmart USB relay board. The Zero is running crelay that I recompiled to generate JSON formatted status. I’ll be submitting a pull request for that modification, and once I do, the URL for getting the status of the relay board will change slightly. If a person uses the native crelay web interface and turns on a relay, OH2 will get that status change and start a timer to turn the relay off at a later time, so the events are triggered on change in state. Please let me know if there’s more I need to add to this tutorial.
I’m using a web cache, and that’s set up in
http.cfg
http:sprinklerCache.url=http://192.168.10.142:8000/gpio
http:sprinklerCache.updateInterval=1000
The purpose of this is to fetch all relay states at once, every second. Then, each item uses that cached data.
sprinkler.items file
Switch sprinklerValve1 { http=">[ON:GET:http://192.168.10.142:8000/gpio?pin=1?status=1] >[OFF:GET:http://192.168.10.142:8000/gpio?pin=1?status=0] <[sprinklerCache:5000:JSONPATH($.Relay1)]" }
Switch sprinklerValve2 { http=">[ON:GET:http://192.168.10.142:8000/gpio?pin=2?status=1] >[OFF:GET:http://192.168.10.142:8000/gpio?pin=2?status=0] <[sprinklerCache:5000:JSONPATH($.Relay2)]" }
Switch sprinklerValve3 { http=">[ON:GET:http://192.168.10.142:8000/gpio?pin=3?status=1] >[OFF:GET:http://192.168.10.142:8000/gpio?pin=3?status=0] <[sprinklerCache:5000:JSONPATH($.Relay3)]" }
Switch sprinklerValve4 { http=">[ON:GET:http://192.168.10.142:8000/gpio?pin=4?status=1] >[OFF:GET:http://192.168.10.142:8000/gpio?pin=4?status=0] <[sprinklerCache:5000:JSONPATH($.Relay4)]" }
Switch sprinklerValve5 { http=">[ON:GET:http://192.168.10.142:8000/gpio?pin=5?status=1] >[OFF:GET:http://192.168.10.142:8000/gpio?pin=5?status=0] <[sprinklerCache:5000:JSONPATH($.Relay5)]" }
Switch sprinklerValve6 { http=">[ON:GET:http://192.168.10.142:8000/gpio?pin=6?status=1] >[OFF:GET:http://192.168.10.142:8000/gpio?pin=6?status=0] <[sprinklerCache:5000:JSONPATH($.Relay6)]" }
Switch sprinklerValve7 { http=">[ON:GET:http://192.168.10.142:8000/gpio?pin=7?status=1] >[OFF:GET:http://192.168.10.142:8000/gpio?pin=7?status=0] <[sprinklerCache:5000:JSONPATH($.Relay7)]" }
Switch sprinklerValve8 { http=">[ON:GET:http://192.168.10.142:8000/gpio?pin=8?status=1] >[OFF:GET:http://192.168.10.142:8000/gpio?pin=8?status=0] <[sprinklerCache:5000:JSONPATH($.Relay8)]" }
Each item has three commands, output to send when the virtual switch is turned off, output for when it is turned on, and status that comes back from the remote Pi web service.
mySitemap.sitemap
sitemap mySitemap label="My home automation" {
Frame label="Sprinkler" {
Switch item=sprinklerValve1
Switch item=sprinklerValve2
Switch item=sprinklerValve3
Switch item=sprinklerValve4
Switch item=sprinklerValve5
Switch item=sprinklerValve6
Switch item=sprinklerValve7
Switch item=sprinklerValve8
}
}
You will need to get the switches, each one representing a relay to show up in OH by adding them to your sitemap. I’m still not sure why this is a required step.
wateringSchedule.rules
import org.joda.DateTime
import org.eclipse.xtext.xbase.lib.Functions
val java.util.Map<String, Timer> sprinklerTimers = newHashMap("1"->null,"2"->null,"3"->null,"4"->null,"5"->null,"6"->null,"7"->null,"8"->null)
val org.eclipse.xtext.xbase.lib.Functions$Function4
<SwitchItem,
java.util.Map<String, Timer>,
String,
Number,
Boolean>
timerLogic = [
SwitchItem relayItem,
java.util.Map<String,Timer> timers,
String timerKey,
Number timeout |
logInfo("Logger",relayItem.toString + " changed state");
timers.get(timerKey)?.cancel
timers.put(timerKey, createTimer(now.plusMinutes(timeout)) [|
logInfo("Logger",relayItem.toString + " turning off");
relayItem.sendCommand(OFF)
timers.remove(timerKey)
])
true
]
rule "Zone 1" when Time cron "0 0 8 1/4 * ?" then sprinklerValve1.sendCommand(ON) end
rule "Zone 2" when Time cron "0 0 9 1/4 * ?" then sprinklerValve2.sendCommand(ON) end
rule "Zone 3" when Time cron "0 0 8 2/4 * ?" then sprinklerValve3.sendCommand(ON) end
rule "Zone 4" when Time cron "0 0 9 2/4 * ?" then sprinklerValve4.sendCommand(ON) end
rule "Zone 5" when Time cron "0 0 8 3/4 * ?" then sprinklerValve5.sendCommand(ON) end
rule "Zone 6" when Time cron "0 0 9 3/4 * ?" then sprinklerValve6.sendCommand(ON) end
rule "Zone 7" when Time cron "0 0 8 4/4 * ?" then sprinklerValve7.sendCommand(ON) end
rule "Zone 8" when Time cron "0 0 9 4/4 * ?" then sprinklerValve8.sendCommand(ON) end
rule "Zone 1 timer" when Item sprinklerValve1 changed then timerLogic.apply(sprinklerValve1, sprinklerTimers,"1", 30) end
rule "Zone 2 timer" when Item sprinklerValve2 changed then timerLogic.apply(sprinklerValve2, sprinklerTimers,"2", 30) end
rule "Zone 3 timer" when Item sprinklerValve3 changed then timerLogic.apply(sprinklerValve3, sprinklerTimers,"3", 30) end
rule "Zone 4 timer" when Item sprinklerValve4 changed then timerLogic.apply(sprinklerValve4, sprinklerTimers,"4", 30) end
rule "Zone 5 timer" when Item sprinklerValve5 changed then timerLogic.apply(sprinklerValve5, sprinklerTimers,"5", 30) end
rule "Zone 6 timer" when Item sprinklerValve6 changed then timerLogic.apply(sprinklerValve6, sprinklerTimers,"6", 30) end
rule "Zone 7 timer" when Item sprinklerValve7 changed then timerLogic.apply(sprinklerValve7, sprinklerTimers,"7", 30) end
rule "Zone 8 timer" when Item sprinklerValve8 changed then timerLogic.apply(sprinklerValve8, sprinklerTimers,"8", 30) end
I am testing the system now, but wanted to get this tutorial done while I have the motivation. I’ll update it if there are problems that need to be addressed.