How to link knx to Sonoff over mqtt?

Hi together,

I thought it would be very easy, but now I figure out, that I didn’t get it to work and I hope somebody can help me. I’m definitely a beginner.

I’m using OH 2.3.0 on a windows system. My basic house installation is KNX, but also using Sonoff, TP-Link, Magic LED Ufo, Alexa and so on …

I have a SONOFF device (over MQTT) controlling a light which works properly in UI.
item:

Switch sonoff_1343 "Treppenlich" <light> (gLichtEG) ["Lighting"] {mqtt="<[broker:stat/sonoff-1343/POWER:state:default], >[broker:cmnd/sonoff-1343/POWER:command:*:default]"}

So now I would like to have an additional real switch (using installed MDT KNX hardware taster devices) to control this SONOFF. I already searched for topics discussing this, but unfortunately I didn’t found one.

I thought by creating a virtual KNX address for switching and status (done in ETS5 and assigned to the hardware switch on my wall) and then linking the item together in OH, would work.

knx.things

Type switch		   : eg_treppenlicht				"Light"				[ ga="11/1/21+<11/4/21"]

linking in my.items

Switch sonoff_1343 "Treppenlich" <light> (gLichtEG) ["Lighting"] {mqtt="<[broker:stat/sonoff-1343/POWER:state:default], >[broker:cmnd/sonoff-1343/POWER:command:*:default]", channel="knx:device:bridge:generic:eg_treppenlicht"}

But it didn’t work.

What did I do wrong?
Isn’t it possible in this way?

Thanks and best,
Lars

The knx channel will post an update to the Switch not a command that your then action the mqtt outgoing binding. You will need a proxy item and a rule

Hi vzorglub,
you mean like this:

rule "Treppenlcht an"
when
	Item eg_treppenlicht received update
then
    sonoff_1343.sendCommand(ON)
end

items:

Switch sonoff_1343 "Treppenlich" <light> (gLichtEG) ["Lighting"] {mqtt="<[broker:stat/sonoff-1343/POWER:state:default], >[broker:cmnd/sonoff-1343/POWER:command:*:default]"}
Switch eg_treppenlicht	"Treppenlich KNX"	<light>	(gLichtEG)	{ channel="knx:device:bridge:generic:eg_treppenlicht" }

This turns light on, but I don’t get status back to KNX bus … so I think I have to extend the rule somehow?
And so on I can’t turn it off …

Best,
Lars

Leave the knx channel in the sonoff item

Change the trigger of the rule and use an if statement to send command ON or OFF

As Vincent suggested.

rule "Treppenlcht an"
when
	Item eg_treppenlicht changed
then
    if(eg_treppenlicht.state == OFF)
        sonoff_1343.sendCommand(ON)

    if(eg_treppenlicht.state == ON)
        sonoff_1343.sendCommand(OFF)
end

Ok I get it to work with this rule:

rule "Treppenlicht an"
when
	Item eg_treppenlicht changed
then
	val itemStatus = eg_treppenlicht.state
	sendTelegram("bot1", "Status "+ itemStatus)
	
    if(itemStatus == ON) 
		sonoff_1343.sendCommand(ON)
    else 
		sonoff_1343.sendCommand(OFF)
end

and leave the channel in the item like this.

Switch sonoff_1343 "Treppenlich" <light> (gLichtEG) ["Lighting"] {mqtt="<[broker:stat/sonoff-1343/POWER:state:default], >[broker:cmnd/sonoff-1343/POWER:command:*:default]", channel="knx:device:bridge:generic:eg_treppenlicht"}
Switch eg_treppenlicht	"Treppenlich KNX"	<light>	(gLichtEG)	{ channel="knx:device:bridge:generic:eg_treppenlicht" }

but I don’t get the status back to KNX bus, so that the hardware switch lamp changes as usually.

@H102 … ok yours is a finer solution :wink: Thanks.

And I tried a time but get the following error

rule "Treppenlicht an"
when
	Item eg_treppenlicht changed
then
	val itemStatus = eg_treppenlicht.state
	sendTelegram("bot1", "Status "+ itemStatus)
	
    if(itemStatus == ON) 
		sonoff_1343.sendCommand(ON)
		timer = Timer.createTimer(now.plusSeconds(3) [|
			sonoff_1343.sendCommand(OFF)
			timer = null 
		])

    if(itemStatus == OFF)	
		sonoff_1343.sendCommand(OFF)
end

error:

2019-01-13 15:35:36.670 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Treppenlicht an’: ‘createTimer’ is not a member of ‘java.lang.Class’; line 37, column 12, length 98

Your missing a , after the (3) and you may need {}'s after the if statement when more than a single command is used.

rule "Treppenlicht an"
when
	Item eg_treppenlicht changed
then
	val itemStatus = eg_treppenlicht.state
	sendTelegram("bot1", "Status "+ itemStatus)
	
    if(itemStatus == ON){ 
		sonoff_1343.sendCommand(ON)
		timer = Timer.createTimer(now.plusSeconds(3), [|
			sonoff_1343.sendCommand(OFF)
			timer = null 
		])
    {

    if(itemStatus == OFF)	
		sonoff_1343.sendCommand(OFF)
end

Ok great, that was it :smiley:

So only open is the problem about getting the current status or an status update back to the knx bus.
Because currently the knx status will not being updated.

Ideas?

What about adding something like eg_treppenlicht.postUpdate(OFF) or ON in the rule?

rule "Treppenlicht an"
when
	Item eg_treppenlicht changed
then
	val itemStatus = eg_treppenlicht.state
	sendTelegram("bot1", "Status "+ itemStatus)
	
    if(itemStatus == ON){ 
		sonoff_1343.sendCommand(ON)
                eg_treppenlicht.postUpdate(ON)
		timer = Timer.createTimer(now.plusSeconds(3), [|
			sonoff_1343.sendCommand(OFF)
                        eg_treppenlicht.postUpdate(OFF)
			timer = null 
		])
    {

    if(itemStatus == OFF)	
		sonoff_1343.sendCommand(OFF)
                eg_treppenlicht.postUpdate(OFF)
end

NOTE: I don’t use knx so not really sure if the item used is the correct one. Adjust as needed.

Hello Lars,

i’m doing the same: using little “blue cans” with an intern relay and wlan antennas together with a “proper” KNX installation. (Do not ask …)

With the predecessor KNX Binding (1.4?, not 2.0) and OH 2.3 and MDT Wall Switches this works really well with:

Switch Licht_2OG_KiZi_Steckdose                     "Lichterkette"           <light>        (g2OG_Lichter) 
    { mqtt=">[broker:test/switch/cmnd/KiZi_Lichterkette/POWER:command:*:default], <[broker:test/switch/stat/KiZi_Lichterkette/POWER:state:default]", knx="0/0/33+<0/0/34" }

So i think there should be a way like your first approach. Using a rule for such a task (ON/OFF) is - overkill.

Now i’am moving to the new KNX2 binding and OH 2.4. Therewhile only my KNX devices work. Not to think of rules …
I will post again here, when i the same question as you - or perhaps a solution. :slight_smile:

Hi Richard,
great to hear, but I guess your discribed solution is based on the KNX1 binding (… knx=“0/0/33+<0/0/34”)

Also H102 described a similar solution like in this post [SOLVED] How to send status telegrams to KNX?

I tried to use the Control Channel Types from KNX Binding documentation, because I thought this will be the right way, but unfortunately I don’t get status ON/OFF on knx bus. So the wall switch lamp didn’t change its status… :roll_eyes: