OK, well, this could work great, or be a total disaster. At least it’s all via files, so if it doesn’t work just delete the files!
Things
bridge.things
Create a file called bridge.things
in the openHAB things folder. In that file, add the following and save. This will create the connection to your MQTT broker. If you use a username/password fill it in:
Bridge mqtt:broker:MQTTBroker "MQTT Broker" [
host="192.168.1.113",
secure=false,
port=1883,
clientID="OpenHAB2",
username="",
password=""
]
sonoff.things
Next, create a file called sonoff.things
in the openHAB things folder. In that file, add the following and save. Note that the things refer to the bridge setup above via (mqtt:broker:MQTTBroker)
.
Thing mqtt:topic:Rear_Floodlight "Rear floodlight" (mqtt:broker:MQTTBroker) {
Channels:
Type switch : relay1 "Relay 1" [
stateTopic="stat/Rear_Floodlight/POWER1",
commandTopic="cmnd/Rear_Floodlight/POWER1",
on="ON",
off="OFF"
]
Type switch : relay2 "Relay 2" [
stateTopic="stat/Rear_Floodlight/POWER2",
commandTopic="cmnd/Rear_Floodlight/POWER2",
on="ON",
off="OFF"
]
}
Thing mqtt:topic:Front_Floodlight "Front floodlight" (mqtt:broker:MQTTBroker) {
Channels:
Type switch : relay1 "Relay 1" [
stateTopic="stat/Front_Floodlight/POWER1",
commandTopic="cmnd/Front_Floodlight/POWER1",
on="ON",
off="OFF"
]
Type switch : relay2 "Relay 2" [
stateTopic="stat/Front_Floodlight/POWER2",
commandTopic="cmnd/Front_Floodlight/POWER2",
on="ON",
off="OFF"
]
}
Items
In whatever items file you like, add the following items. Note how the Items refer to the relevant Thing channels created above inside the { channel=" ... " }
:
// 0 = OFF, 1 = ON, 2 = AUTO
Number nRearPIRMode "Rear PIR Mode"
Number nFrontPIRMode "Rear PIR Mode"
Switch sRearFloodRelay1 "Rear Floodlight Relay 1" {channel="mqtt:topic:Rear_Floodlight:relay1"}
Switch sRearFloodRelay2 "Rear Floodlight Relay 2" {channel="mqtt:topic:Rear_Floodlight:relay2"}
Switch sFrontFloodRelay1 "Rear Floodlight Relay 1" {channel="mqtt:topic:Front_Floodlight:relay1"}
Switch sFrontFloodRelay2 "Rear Floodlight Relay 2" {channel="mqtt:topic:Front_Floodlight:relay2"}
Sitemap
Add the following two switches to your sitemap:
Switch item=nRearPIRMode label="Rear PIR Mode" icon="camera" mappings=[0="OFF", 1="ON", 2="AUTO"]
Switch item=nFrontPIRMode label="Front PIR Mode" icon="camera" mappings=[0="OFF", 1="ON", 2="AUTO"]
Rules
Add the following two rules to any rules file (or a new one)
rule "Rear PIR mode changed"
when
Item nRearPIRMode changed
then
switch(nRearPIRMode.state as Number) {
case 0: {
// Always OFF
sRearFloodRelay1.sendCommand(OFF)
sRearFloodRelay2.sendCommand(OFF)
}
case 1: {
//Always ON
sRearFloodRelay1.sendCommand(ON)
sRearFloodRelay2.sendCommand(OFF)
}
case 2: {
//ON/OFF via PIR
sRearFloodRelay1.sendCommand(OFF)
sRearFloodRelay2.sendCommand(ON)
}
}
end
rule "Front PIR mode changed"
when
Item nFrontPIRMode changed
then
switch(nFrontPIRMode.state as Number) {
case 0: {
// Always OFF
sFrontFloodRelay1.sendCommand(OFF)
sFrontFloodRelay2.sendCommand(OFF)
}
case 1: {
//Always ON
sFrontFloodRelay1.sendCommand(ON)
sFrontFloodRelay2.sendCommand(OFF)
}
case 2: {
//ON/OFF via PIR
sFrontFloodRelay1.sendCommand(OFF)
sFrontFloodRelay2.sendCommand(ON)
}
}
end
This is, however, only one way. Whilst the Thing Channels do catch the stat
topic from Tasmota, and will send it onto the relevant Item, openHAB - if configured as above - won’t do anything with it. This may not matter, however: do you have any other method of operating these relays? If not, then it’s probably OK…