How does OH know the state of a switch?


(Simon Crump) #1

Hello,
I am slowly learning openhab and have a few questions that I can’t find the answer to. Can anyone point me at the right docs to read or answer my questions please?

  1. Using Alexa to turn on a light connected to a sonoff, is it her or the Sonoff that tells openhab the light is ON?

  2. Is there a way to turn off the MQTT state being sent from a Tasmota Sonoff device? Or ignore the message?

This all seems a bit strange I know but I can’t get my head round it at the moment.

Cheers

Crumpy


(Christoph Wempe) #2

The Switch (Sonoff device) sends the state to the mqtt broker.
openHAB is connected to the mqtt broker.
Your Item definition of the switch defines what topic to listen to to get the state.
And to what topic a command should be send.

In addition you can setup the item to support Alexa.
sonoff <-> mqtt broker <-> openHAB <-> openHAB Cloud <-> Alexa Cloud <-> your Alexa


(Udo) #3

alternatively:
sonoff <-> mqtt broker <-> openHAB <-> (openHAB-misc) HUE emulation <-> your Alexa
I am not sure what the better way is?


(Simon Crump) #4

Thanks that explains a few things.

I am slowly trying to sort out a way of informing openhab that a light is physically on or off with a CT coil and arduino sending MQTT messages back to Openhab. I have my system setup with openhab on a pi3, a sonoff 4 chan pro and Alexa working perfectly. Also the Arduino is sensing the light on and off, and sending message to Mosquito. Again working perfectly.

My problem now is understanding how to make openhab know the light is physically ON when the relay on the sonoff could be off because I have 2 way lighting circuits wired to the sonoff as below-:

image

I hope this explains my issue thats bending my brain at the moment!

Cheers


(Danny mullen) #5

What are you using the arduino for? The sonoff know if the relay is open or closed?


(Rich Koshak) #6

OH only knows what the devices tell it. So if you can change the device outside of OH (e.g. flip a physical switch) the only way that OH, and in turn Alexa will know the light changed state.

However, there are two cases where this breaks down a little bit. If you command a device using OH and that device doesn’t provide feedback then OH just assumes that the device is now in the state it was commanded to regardless of what state the device is in. If you post an update to an Item, OH will change the state of the Item and that will not go out to the device so if the update differs from the state of the device OH will have the wrong state. There is a further wrinkle. When you do have a device that reports states back to OH, there is a flag you can apply (autoupdate=false) to cause OH to not change the state of the Item when it receives a command and instead wait for the device to report back before changing the Item’s state.

The key thing is OH reflects the state it was last told. It doesn’t go out and check the state of the device on demand.


(Rossko57) #7

Short answer: you can’t. With your 2-way switching, relay positions A and B can arbritrarily represent either On or Off, depending on the wall switch position.

You need something else - a means to sense the wall switch position, so that can be taken into account to work out On or Off.
Or a means to sense the actual circuit state - a photocell, another relay, a current sensor.
Some Sonoff allow load sensing - does that model?


(Simon Crump) #8

The Arduino (and a CT coil sensor) is being used to send an MQTT message back to OH telling it that the light(s) are physically ON.

Thanks I will look into that.

Please see above, I have an Arduino with CT coil. Unfortunately the Sonoff 4 channel models do not sense loads. Would be a whole lot easier if they did.

Thanks everyone for your feedback, I actually have a sort of paralel thread going now because of the way this thread has progressed.

Please see this thread

https://community.openhab.org/t/thinking-of-using-openhab-but-would-like-some-advice-first/46002/32

and I will close this one

Cheers


(Rossko57) #9

Okay, I had to work out what that meant, I think it means Current Transformer - so there is load sensing.
As the input from that into OH will probably require some kind of massaging - checking to see if current is above a threshold, say - you’ll probably end up wth a rule to determine the on-off state of your light Item.


(Simon Crump) #10

Ah, Sorry I should have explained a little better.
What I have is an Arduino with the current transfomer sensing the load. The Arduino runs all the code needed to determine the threshold etc. It then sends a MQTT message to Mosquito (on openhabian pi3) to say that the light is either on or off physically. This all works and I have it changing the state of the switch in OH, and it shows in the log as changed. But Alexa still sees the relay state in the sonoff for some reason. Its the Alexa/Openhab interface I dont understand enough about…

This is where my problem starts and I cant figure out the openhab bit. I have got as far as this in the thread above.

Cheers


(Danny mullen) #11

@crumpy10 that’s an interesting way to know light is on. I was having this as a desire the other day also. I was thinking light sensor, but a ct would be more accurate per light.

My problem was I turned the switch on in openhab. It said on, but the light was off due to a comm issue on the gateway. In my head I was thinking how does openhab really know the light switches. Your method definitely confirms it! Looking into this issue is on my to do list but not the most important right now! Have a good one.


(Simon Crump) #12

Yeah, once I get this all running well on one of the channels of the sonoff pro, I intend to make an 8 channel board for an Arduino Mega and read all 8 channels of two Sonoff pros to do lighting. Once I am finished I hope to document it so others can follow it. I have had interest from quite a few people about doing it.

Cheers


(Rossko57) #13

OK, I’m assuming your OpenHAB Item has the option autoupdate=“false”. So that means sending a command to the Item does not change it’s state, that gets updated later when relays have clonked and an MQTT message comes in from the monitor arduino.

Confident that is all working, and your Item only connects outbound to pass commands to the Sonoff, never accepting inbound status update from Sonoff?

As pointed out earlier, Alexa is all cloudy connection - you need to check what state the my.OpenHAB cloud thinks is going on.


(Simon Crump) #14

Hi Rossko,
Yes I have this in my items file and assume the syntax is correct?

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

I am very new to openhab but I assume from what I know that the outbound part is this part

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

and the inbound part is this that feeds back from the Arduino message?

<[broker:arduino/channel1:state:default]", autoupdate="false" }

How do I do this? I do get messages in the log that the state has changed, but for some reason Alexa can still see the physical state of the relay not the light… oh man this is doing my brain in.

Cheers
Crumpy


(Rossko57) #15

I don’t know MQTT detail, but that looks right. OH commands get sent to the Sonoff, incoming arduino status updates the OH Item. Your OH Item state should be unrelated to Sonoff relay state.

I don’t know myOpenhab either, but I guess you log into it on the web to see what it thinks.

What makes you think Alexa ‘knows’ the light is on? So far as I’m aware, it has no idea.
Because of your 2-way switching, sending ON to your Item might turn the lights on or it might turn them off, right? Are we really trying to solve of problem of converting ‘Alexa turn xx on’ into the choice of commanding the Item ON or OFF depending on the actual relay state?


(Rossko57) #16

Openhab is going to need to know both the actual state of the light as well as the Sonoff relay state, to work out what to do. You’ll need two Items.

Switch virtual_switch "Kitchen Lights" <light> (gLight) [ "Switchable" ] { mqtt="<inboundArduino", autoupdate="false"}
Switch sonoff_relay "2-way switch" {mqtt=">sonoffrelay"} 

virtual_switch is the one you want on your sitemap and commanded by Alexa, and gets updated by the load sensing. But note it has no outbound binding.

sonoff_relay works one of th 2-way switches, and we let autoupdate do its thing there. You could also use the sonoff relay feedback to update it. (That’s probably a good idea for system startup)

Now you need a rule to convert ON/OFF commands to the virtual switch into A/B commands for the 2-way switch.

rule "control lights toggling"
when
   Item virtual_switch received command
then
   if (receivedCommand != virtual_switch.state) { 
          // only if new request does not mach actual current state
          // then we need to toggle the relay state
      if (sonoff_relay.state == OFF) {
         sonoff_relay.sendCommand(ON)
      } else {
         sonoff_relay.sendCommand(OFF)
      }
   }  // else already in requested state
end

EDIT - virtual_switch is the one to publish to myOpenhab of course, and not the actual relay.


(Rich Koshak) #17

Actually the outbound part of the MQTT is just

>[broker:cmnd/sonoff4chan_1/POWER1:command:*:default]

and the inbound part is just

<[broker:arduino/channel1:state:default]

The autoupdate=false means that when you send a command to this Item inside of OH, the Item will not change state. Instead it will wait for the new state to be reported back on the inbound MQTT subscription to change the state.

Do you have this device linked to Alexa directly? If so that is probably overriding the OH Item.

I see no other way Alexa could even know the physical state of the device. I can’t imagine it would work to have it controllable natively from Alexa and through OH would work very well.


(Simon Crump) #18

Hello, thanks for the MQTT info, i see now (i think…) Is the autoupdate syntax correct?

Well, I reflashed the Sonoff 4 chan pro with Tasmota, then added Alexa to OH, then she discovered the openhab controlled 4 chan pro. Is that what you mean by directly? Do I need to delete it off the Alexa App?

Control everything through OH is my goal… but have voice control as well.

Cheers

Crumpy


(Rich Koshak) #19

It looks OK to me but if you have questions about syntax the absolute best thing to do is load the file in VSCode. It will find syntax errors better than we humans ever can.

Something is telling Alexa the physical state of the switch and it isn’t through OH. Otherwise there is no way that Alexa can have a different state for the switch than the Item in OH. When you connect Alexa to OH, all that Alexa sees are the properly tagged Items.


(Simon Crump) #20

Hi Rossko,
Thanks for the info and effort on this.

Because if the light is off and another manual switch has been used, you have to ask her to turn the light ON instead of OFF and it always mirrors the state of the relay on the sonoff.

Correct.

Yes I think thats what I am trying to do. Basically we should be able to turn the lights ON or OFF with any manual switch or Alexa, but the relay in the sonoff could either be energised or not.

I would have thought if we can get Alexa to see the state sent out by the MQTT message from the Arduino then we are on a winner but its probably not that simple.