Thinking of using OpenHAB but would like some advice first

(Vincent Regaud) #21

It will not work in your situation as OH has the relay ON and doesn’t know the light is OFF
You need two items:

Switch Relay { your tasmota binding }
Switch WallSwitch
Switch Light

And a rule:
Assuming the the light is ON when both switches and ON or OFF

rule "Light"
    Item Relay changed or
    Item WallSwitch changed
    if (Relay.state == WallSwitch.state) Light.postUpdate(ON)
    else Light.postUpdate(OFF)

But we come back to this
You NEED to feedback the switch position to OH

(Simon Crump) #22

Yes I understand, its the reporting back that stumped me… But I have just had an idea that I will explore tomorrow (its late in NZ), what about connecting some type of CT coil to a GPIO to read back the ‘On or Off’ status to OH…



(Jürgen Baginski) #23

“Thats one way of doing it but due to restrictions of wiring and also if the system is down I want anyone else to be able to use the normal light switch.”
By having it worked like that the sonoff is doing it all, no openHAB MQTT or whatever is needed! You either switch the sonoff directly or using the other rewired switch you toggle a change.
I’m using a sonoff dual that way! While openHAB was down for maintenance the migth could be switched.

(Simon Crump) #24

Hi Vincent,

OK, So I have been working on this over the last few weeks, on and off (sorry excuse the pun!)

What I now have is a standalone Arduino with CT sensors that detect when there is a load on the lighting circuits of the Sonoff 4 channel. I have also managed to get the Arduino to send a MQTT message that the circuit is either ON or OFF. Either when Alexa is used to operate the Sonoff or the conventional switch is used. Its sends the message because it detects the load.

So I now have a way of feeding back the state to OH!

But this is where I am a little stuck and showing my OH and programming lack of knowledge. I am trying to figure out how I log the fact that the light is physically off in the OH system. So that if someone then uses Alexa to switch the light back on, the system checks the state and turns it on.

Hope this makes sense.

Any pointers would be great.



(Simon Crump) #25

Hello vzorglub,

Not sure if you have seen this post updated? I have managed to figure out a way of feeding back the info to Openhab, I am just a bit stuck on what I need to do next…

The code and rule you showed, I dont understand how the MQTT message I am sending back is used to update the switch?

Hope you can help, I think I am getting close to making this work. I just dont have enough Openhab experience…yet.


(Vincent Regaud) #26

How did you do that and what is the item definition?

(Simon Crump) #27

Thanks for your reply.

I have an arduino that has a CT coil sensor connected to it. When this arduino and coil sense a voltage on the live wire to the light it sends an MQTT message to Mosquito saying the light is ON, and the same when its turned OFF.


 //First 4 Channel Pro Sonoff for 2 way lighting
Switch Sonoff4ch_1Channel1 "Kitchen Lights" <light> (gLight) [ "Switchable" ] { mqtt=">[broker:cmnd/sonoff4chan_1/POWER1:command:*:default],<[broker:stat/sonoff4chan_1/POWER1:state:default]" }
Switch Sonoff4ch_1Channel2 "Family Lights" <light> (gLight) [ "Switchable" ] { mqtt=">[broker:cmnd/sonoff4chan_1/POWER2:command:*:default],<[broker:stat/sonoff4chan_1/POWER2:state:default]" }
Switch Sonoff4ch_1Channel3 "Dining Lights" <light> (gLight) [ "Switchable" ] { mqtt=">[broker:cmnd/sonoff4chan_1/POWER3:command:*:default],<[broker:stat/sonoff4chan_1/POWER3:state:default]" }
Switch Sonoff4ch_1Channel4 "Office Lights" <light> (gLight) [ "Switchable" ] { mqtt=">[broker:cmnd/sonoff4chan_1/POWER4:command:*:default],<[broker:stat/sonoff4chan_1/POWER4:state:default]" }

(Vincent Regaud) #28

These are your sonoff items.
What message does the arduino send to mqtt?
Do you have an OH item for that?

(Simon Crump) #29

Ah, this is the section of code in the arduino that sends the MQTT message-:

 if ((Irms >= 1) && !sonoff4ch1_On) {

    Serial.println("Light is ON");
    client.publish("stat/sonoff4ch1/", "Kitchen light is ON");
    sonoff4ch1_On = true; //Remember what state the light is
  else if ((Irms <= 1) && sonoff4ch1_On)
    Serial.println("Light is OFF");
    client.publish("stat/sonoff4ch1","Kitchen light is OFF");
    sonoff4ch1_On = false; //Remember what state the light is

But I dont understand how I make an item for openhab when its a message not an item?? Sorry I am very new to Openhab and not a programmer by trade…but very willing to learn.


(Vincent Regaud) #30

And this is an arduino different than your sonoff, right?

(Simon Crump) #31

Yes, its on a standalone Arduino Uno with an ethernet shield connected to the network. The CT coil is around the common wire to the light which senses the ON or OFF.

(Vincent Regaud) #32

Ok, change the arduino code to:

if ((Irms >= 1) && !sonoff4ch1_On) {
    Serial.println("Light is ON");
    sonoff4ch1_On = true; //Remember what state the light is
  else if ((Irms <= 1) && sonoff4ch1_On)
    Serial.println("Light is OFF");
    sonoff4ch1_On = false; //Remember what state the light is

And change your items to:

Switch Sonoff4ch_1Channel1 "Kitchen Lights" <light> (gLight) [ "Switchable" ] { mqtt=">[broker:cmnd/sonoff4chan_1/POWER1:command:*:default],<[broker:arduino/channel1:state:default]" }

How does OH know the state of a switch?
(Simon Crump) #33

Thank you, I will have to do this when I get home tomorrow night. I am working remotely from home until then.

I will let you know how it goes.


(Simon Crump) #34

OK I have made those changes and they work ok it seems.
The Arduino sends the messages to Mosquito, which I can see in MQTTfx.
I can also see the state change event in the Openhab log viewer.
It appears to update the basic UI after a page refresh.
Alexa does not see the state change though, she is still seeing the relay state in the Sonoff.

If Openhab is seeing the change of state of the light being physically on or off, how does Alexa not see the same?

There must be more I need to do? Do I somehow have to disable the state updates from the Sonoff? or did changing the broker in the items file do this?

Sorry for all the questions.