Smart Flood Light with PIR, Sonoff Dual

Good afternoon everyone.

In the days before smart devices, if you wanted to manually control a floodlight with a separate pir sensor this could be achieved with a 3 way switch.
ON-Auto-OFF

I’m planning to use a sonoff dual in a similar manner.

When channel 1 is on this will bypass the pir sensor and power the floodlight untill it’s turned off.

When channel 2 is on it will send the power to the pir. Letting the pir decide when to send switch live to the floodlight.

When channel 1 and 2 are both off. The flood light will not turn on in any circumstance.

How can I make this possible regarding sitemap and statuses?

Im planning on using 1 sitemap item with the labels ON-AUTO-OFF and thought mappings might be able to help me here but not 100% on how that would affect the status of the item.

I.e I set channel 1 ON as ON in mappings and channel 2 and OFF. This might make getting the actually status of the channels hard because they both conflict.

Any suggestions would be great :+1:

Thanks guys

You might use a Number type Item, with mappings=[] for pushbuttons as you suggest. A simple MAP transform in the label would give you a plain language state display if needed.
Or use a String type Item in much the same way.
A rule can listen for commands to this Item, and issue commands to Items for real devices as required.

1 Like

How do you (plan to?) communicate to the Sonoff from openHAB? MQTT?

I was interested in this, so mocked something up in a similar vein to @rossko57 suggestion:

Items

// 0 = OFF, 1 = ON, 2 = AUTO
Number nPIRMode "PIR Mode"

Switch sSonoffDualChannel1 "Sonoff Dual Channel 1"

Switch sSonoffDualChannel2 "Sonoff Dual Channel 2"

The two switches should be bound directly to the Sonoff Thing Channels. The number item is in effect a fake or proxy item, used only to collect and act upon the desired status from the sitemap.

Sitemap

image

Switch item=nPIRMode label="PIR Mode" icon="camera" mappings=[0="OFF", 1="ON", 2="AUTO"]

Rule

rule "PIR mode changed"
when
	Item nPIRMode changed
then
	 switch(nPIRMode.state as Number) {
        case 0: {
            // Always OFF
            sSonoffDualChannel1.sendCommand(OFF)
            sSonoffDualChannel2.sendCommand(OFF)
        }
        case 1: {
            //Always ON
            sSonoffDualChannel1.sendCommand(ON)
            sSonoffDualChannel2.sendCommand(OFF)
        }
        case 2: {
            //ON/OFF via PIR
            sSonoffDualChannel1.sendCommand(OFF)
            sSonoffDualChannel2.sendCommand(ON)
        }
     }
end

The final result will depend on the binding that you are using to communicate to the Sonoff, because you probably want to check the state of the relays too (if some other source is able to switch them, or at a power loss event, or something else). You may also want to be certain that nothing bad will happen if both channel 1 and 2 are ON - it may happen, depending on the order and timing of sent and received events.

I have Flashed the sonoff with tasmota so would be preferably using MQTT.
I presume the setup will still work just add as items not things?

You will need to configure Things using MQTT. What have you configured so far?

This is my setup so far, It works but its a little sloppy.

I couldnt get the mappings to work and recieve status updates.
because the channels only output ON/OFF updates.

Items
Switch Rear_Floodlight_Main "Rear Floodlight" <light> (gOD_Lights) [ "Switchable" ] {mqtt=">[broker:cmnd/Rear_Floodlight/POWER1:command:*:default], <[broker:stat/Rear_Floodlight/POWER1:state:default]"} Switch Rear_Floodlight_Pir "Rear Floodlight Pir" <motion> [ "Switchable" ] {mqtt=">[broker:cmnd/Rear_Floodlight/POWER2:command:*:default],<[broker:stat/Rear_Floodlight/POWER2:state:default]"} Switch Front_Floodlight_Main "Front Floodlight" <light> (gOD_Lights) [ "Switchable" ] {mqtt=">[broker:cmnd/Front_Floodlight/POWER1:command:*:default],<[broker:stat/Front_Floodlight/POWER1:state:default]"} Switch Front_Floodlight_Pir "Front Floodlight Pir" <motion> [ "Switchable" ] {mqtt=">[broker:cmnd/Front_Floodlight/POWER2:command:*:default],<[broker:stat/Front_Floodlight/POWER2:state:default]"}

Sitemap
Switch item=Front_Floodlight_Main mappings=[OFF="Off", ON="Permanent"] Switch item=Front_Floodlight_Pir mappings=[OFF="Off", ON="Auto"] Switch item=Rear_Floodlight_Main mappings=[OFF="Off", ON="Permanent"] Switch item=Rear_Floodlight_Pir mappings=[OFF="Off", ON="Auto"]

Rules
`rule “Deactivate Rear FloodLight Pir”
when
Item Rear_Floodlight_Main received command ON
then
sendCommand(Rear_Floodlight_Pir, OFF)

end

rule “Deactivate Front FloodLight Pir”
when
Item Front_Floodlight_Main received command ON
then
sendCommand(Front_Floodlight_Pir, OFF)

end

rule “Activate Rear FloodLight Pir”
when
Item Rear_Floodlight_Pir received command ON
then
sendCommand(Rear_Floodlight_Main, OFF)

end

rule “Activate Front FloodLight Pir”
when
Item Front_Floodlight_Pir received command ON
then
sendCommand(Front_Floodlight_Main, OFF)

end`

OK - any reason your’re using the V1 MQTT binding? Or at least, your Item syntax is for the V1 binding.

I feel most comfortable with the v1 binding, i have v2 installed and run them side by side, some new items are with v2. personal preference i suppose!

What’s the IP address of your MQTT broker? I’ll write you a configuration for the V2 binding…

And am I right in thinking you’ve got two of these Sonoff Duals? One front, one rear?

192.168.1.113

yes one controlling 2 floodlights at the front and one controlling the rear floodlight.

Do you fancy rewriting all my v1 items to v2? haha

Do you already have a bridge to the MQTT broker setup with the V2 MQTT binding (via PaperUI, or otherwise)? If so, what did you call it? If not, I’ll write the bridge configuration too.

To be honest i installed the v2 binding and didnt think there was any other setup involved. currently got 2 brokers active.

there is no other information inside those bindings.

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…

1 Like

I will have play with this setup and report back to you. I don’t have any other way of controlling them. Only by accessing the web gui of the sonoff device itself or via sitemap. My only concern is if I have a power failure the sonoff is setup to go back to its last state and openhab sets all back to off when it reboots.

That’s true. You have some options there, the first being Persistence.

You could also setup a rule which fires only when the system is restarted, and sends the desired default commands to the Sonoffs?

...
when
    System started
then
...

Otherwise, because these are only floodlights it probably doesn’t matter that they’ll be out of sync until the next time you press a button on your sitemap?

This certainly isn’t the most fool proof of setups, but it should get you up and running quickly!

So how would the rule look for say

When System starts
Send command pir auto to both front and rear flood lights?

rule "System started - floodlights"
when
	System started
then
    // Set rear floodlights to AUTO
    nFrontPIRMode.sendCommand(2)
         
    // Set front floodlights to AUTO
    nRearPIRMode.sendCommand(2)
end
1 Like

Tried your setup out. Works a treat so i have deleted all old items/rules related to how i had it setup and works beautifully! thank you for your help. I am going to slowly merge all my old items into the new v2 format, just need to get my head round reading the temp and humidity from a few devices. Thanks again for your help

With the recent announcement that openHAB 3 will be released by the end of the year, switching to MQTT v2 will future proof you for a bit, so I think it’s worthwhile! Note that the bridge.things you’ve already created is now your connection to your MQTT broker - no need to create a new one for each new Thing! Just reference it when creating a new Thing, as shown in the example above.

Depending on your temperature and humidity sensors, this might be useful. If you get stuck, just start a new thread with what you’ve tried - people are only too happy to point out where you’ve gone wrong…! :slight_smile:

Pob lwc!

1 Like

Hi !!

I’m looking for a PIR equipped, POE (passthrough?) compatible floodlight that is controllable through MQTT …
What device did you use ?

Thanks a lot !!!