How to setup a Sonoff 4ch in openHAB

I have recently installed the latest version of openHAB on a raspberry pi 3 b+. all the add ons were installed as well as mqtt binding. the consol from my Sonoff says it is connected via mqtt to openhab.

I am trying to get the the switch into opneHAB UI but am so far unsuccessful. the below content in the file “sonoff4ch.items” I created in the items folder.

//Sonoff 4ch Master Bedroom

Switch Sonoff4ch_4Channel1 “Channel 1” [ “Switchable” ] { mqtt=">[broker:sonoff/Sonoff4ch_4/cmnd/POWER1:command::default],<[broker:sonoff/Sonoff4ch_4/cmnd/POWER1:state:default]" }
Switch Sonoff4ch_4Channel2 “Channel 2” [ “Switchable” ] { mqtt=">[broker:sonoff/Sonoff4ch_4/cmnd/POWER2:command:
:default],<[broker:sonoff/Sonoff4ch_4/cmnd/POWER2:state:default]" }
Switch Sonoff4ch_4Channel3 “Channel 3” [ “Switchable” ] { mqtt=">[broker:sonoff/Sonoff4ch_4/cmnd/POWER3:command::default],<[broker:sonoff/Sonoff4ch_4/cmnd/POWER3:state:default]" }
Switch Sonoff4ch_4Channel4 “Channel 4” [ “Switchable” ] { mqtt=">[broker:sonoff/Sonoff4ch_4/cmnd/POWER4:command:
:default],<[broker:sonoff/Sonoff4ch_4/cmnd/POWER4:state:default]" }

and the code I wrote in “sitemap”

Frame label=“Lights”
{

Switch item=Sonoff4ch_4Channel1        label="Bedside Lamp 1"       icon="switch"

Switch item=Sonoff4ch_4Channel2 label=“Bedside Lamp 2” icon=“switch”
Switch item=Sonoff4ch_4Channel3 label=“Master Bedroom3” icon=“switch”
Switch item=Sonoff4ch_4Channel4 label=“Master Bedroom4” icon=“switch”

}

So far nothing seems to show up on the open tab control panel.

can someone please help me with this setup.

First you need to use the code fences when publishing code:

The answer to your problem is right under your nose:
The sonoff MQTT full topic is sonoff/cmnd/sonoff_MasterBedroom/

and topic used in your items is: sonoff/Sonoff4ch_4/cmnd/

Good luck

Hi vzorglub,

I already made that change. thank you. spending so much time sometimes we miss the little things.

I have already made that change but am still not seeing it in the paper ui.

how do I link the items to my sitemap??

any ideas

Your second error is because you didn’t read the docs, both for OH and tasmotta:

From tasmota wiki - openHAB section:

Switch LivingRoom_Light "Living Room Light" <light> (LR,gLight)
    { mqtt=">[broker:cmnd/sonoff-A00EEA/POWER:command:*:default],
            <[broker:stat/sonoff-A00EEA/POWER:state:default]" }

Compare that with your code.

Also you didn’t monitor your log openhab.log to see what errors you had. OH would have told you that ou had a binding parsing error.

I did read through the entire article. I made the following changes also,

Switch Sonoff4ch_4Channel1 “Channel 1” [ “Switchable” ] (LR,gLight) { mqtt=">[broker:sonoff/cmnd/Sonoff4ch_4/POWER1:command::default],<[broker:sonoff/stat/Sonoff4ch_4/POWER1:state:default]" }
Switch Sonoff4ch_4Channel2 “Channel 2” [ “Switchable” ] (LR,gLight) { mqtt=">[broker:sonoff/cmnd/Sonoff4ch_4/POWER2:command:
:default],<[broker:sonoff/stat/Sonoff4ch_4/POWER2:state:default]" }
Switch Sonoff4ch_4Channel3 “Channel 3” [ “Switchable” ] (LR,gLight) { mqtt=">[broker:sonoff/cmnd/Sonoff4ch_4/POWER3:command::default],<[broker:sonoff/stat/Sonoff4ch_4/POWER3:state:default]" }
Switch Sonoff4ch_4Channel4 “Channel 4” [ “Switchable” ] (LR,gLight) { mqtt=">[broker:sonoff/cmnd/Sonoff4ch_4/POWER4:command:
:default],<[broker:sonoff/stat/Sonoff4ch_4/POWER4:state:default]" }

it still does not show up in paper ui.

Because it’s still wrong!

Switch Sonoff4ch_4Channel1 “Channel 1” [ “Switchable” ] (LR,gLight) { mqtt=">[broker:sonoff/cmnd/Sonoff4ch_4/POWER1:command::default],<[broker:sonoff/stat/Sonoff4ch_4/POWER1:state:default]" }
Switch Sonoff4ch_4Channel2 “Channel 2” [ “Switchable” ] (LR,gLight) { mqtt=">[broker:sonoff/cmnd/Sonoff4ch_4/POWER2:command::default],<[broker:sonoff/stat/Sonoff4ch_4/POWER2:state:default]" }
Switch Sonoff4ch_4Channel3 “Channel 3” [ “Switchable” ] (LR,gLight) { mqtt=">[broker:sonoff/cmnd/Sonoff4ch_4/POWER3:command::default],<[broker:sonoff/stat/Sonoff4ch_4/POWER3:state:default]" }
Switch Sonoff4ch_4Channel4 “Channel 4” [ “Switchable” ] (LR,gLight) { mqtt=">[broker:sonoff/cmnd/Sonoff4ch_4/POWER4:command::default],<[broker:sonoff/stat/Sonoff4ch_4/POWER4:state:default]" }

You didn’t put the * between the two :: after the command section in the outbound binding.

These items will never show in paperUI because they are manually created in a text file for a 1.9x binding.
You should see them in the sitemap though once you get the syntax right

its already there. copying and pasting the text here is removing the * between the two : :

Then I would advise not copying and pasting, it tends to delete and insert characters at random especially from a browser.

Oh sorry… that’s y i out the image last time. It’s there still something wrong with the syntax.

No, looks right now.
Do your switches appear in the sitemap?

I have done ssh to my device and viewed the file from there. It is empty and also the switches are not appearing on paper ui

Again, the switches will NEVER appear on paperUI, but they should be on your sitemap
The events.log in is /var/log/openhab2/events.log

ssh in and do
tail /var/log/openhab2/events.log
That will keep a running screen with what happens in the log

Do you have an MQTT client installed like mqtt.fx or mqtt-spy
Install one and subscribe to your mqtt broker witht the topic #
Action the switches on the sonoff, what do you get on mqtt.fx?

Can you show the contents of your mqtt.cfg file, please?

The sitemap folder has only one readme and a file called sitemap which is empty

The following is from

tail /var/log/openhab2/events.log
2018-05-13 11:29:29.485 [vent.ItemStateChangedEvent] - sonoff_MasterBedroom2 changed from ON to OFF
2018-05-13 11:29:30.083 [ome.event.ItemCommandEvent] - Item 'sonoff_MasterBedroom3' received command OFF
2018-05-13 11:29:30.095 [vent.ItemStateChangedEvent] - sonoff_MasterBedroom3 changed from ON to OFF
2018-05-13 11:29:30.750 [ome.event.ItemCommandEvent] - Item 'sonoff_MasterBedroom' received command OFF
2018-05-13 11:29:30.761 [vent.ItemStateChangedEvent] - sonoff_MasterBedroom changed from ON to OFF
2018-05-13 11:29:38.642 [vent.ItemStateChangedEvent] - sonoff_MasterBedroom changed from OFF to ON
2018-05-13 11:29:38.645 [vent.ItemStateChangedEvent] - sonoff_MasterBedroom1 changed from OFF to ON
2018-05-13 11:29:38.667 [vent.ItemStateChangedEvent] - sonoff_MasterBedroom4 changed from OFF to ON
2018-05-13 11:29:38.671 [vent.ItemStateChangedEvent] - sonoff_MasterBedroom2 changed from OFF to ON
2018-05-13 11:29:38.675 [vent.ItemStateChangedEvent] - sonoff_MasterBedroom3 changed from OFF to ON

the file from mqtt.cfg has no settings activated. all fields contain a # in front.

#
# Define your MQTT broker connections here for use in the MQTT Binding or MQTT
# Persistence bundles. Replace <broker> with an ID you choose.
#

# URL to the MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883
#<broker>.url=tcp://<host>:1883

# Optional. Client id (max 23 chars) to use when connecting to the broker.
# If not provided a random default is generated.
#<broker>.clientId=<clientId>

# Optional. True or false. If set to true, allows the use of clientId values
# up to 65535 characters long. Defaults to false.
# NOTE: clientId values longer than 23 characters may not be supported by all
# MQTT servers. Check the server documentation.
#<broker>.allowLongerClientIds=false

# Optional. User id to authenticate with the broker.
#<broker>.user=<user>

# Optional. Password to authenticate with the broker.
#<broker>.pwd=<password>

# Optional. Set the quality of service level for sending messages to this broker.
# Possible values are 0 (Deliver at most once),1 (Deliver at least once) or 2
# (Deliver exactly once). Defaults to 0.
#<broker>.qos=<qos>

# Optional. True or false. Defines if the broker should retain the messages sent to
# it. Defaults to false.
#<broker>.retain=<retain>

# Optional. True or false. Defines if messages are published asynchronously or
# synchronously. Defaults to true.
#<broker>.async=<async>

# Optional. Defines the last will and testament that is sent when this client goes offline
# Format: topic:message:qos:retained <br/>
#<broker>.lwt=<last will definition>

That’s why it doesn’t work!

You need to put the same settings as your sonoff in the mqtt.cfg file:
Name your broker as it is named in your items: broker
Remove the # in the following lines and write the same value
Save
In your log you should see that OH connects to your broker

broker.url=tcp://192.168.1.151:1883
broker.user=openhab

But again, this was in the docs:

https://docs.openhab.org/addons/bindings/mqtt1/readme.html

I did everything now. but still the sitemap file is empty and I see no way to control the device from openhab.

It does not get populated by itself, you need to manually add your entries:

https://docs.openhab.org/configuration/sitemaps.html

So here is my items file

Switch Sonoff4ch_4Channel1 "Channel 1" [ "Switchable" ] <light> (LR,gLight) { mqtt=">[broker:sonoff/cmnd/sonoff_MasterBedroom/POWER1:command:*:default],<[broker:sonoff/stat/sonoff_MasterBedroom/POWER1:state:default]" }
Switch Sonoff4ch_4Channel2 "Channel 2" [ "Switchable" ] <light> (LR,gLight) { mqtt=">[broker:sonoff/cmnd/sonoff_MasterBedroom/POWER2:command:*:default],<[broker:sonoff/stat/sonoff_MasterBedroom/POWER2:state:default]" }
Switch Sonoff4ch_4Channel3 "Channel 3" [ "Switchable" ] <light> (LR,gLight) { mqtt=">[broker:sonoff/cmnd/sonoff_MasterBedroom/POWER3:command:*:default],<[broker:sonoff/stat/sonoff_MasterBedroom/POWER3:state:default]" }
Switch Sonoff4ch_4Channel4 "Channel 4" [ "Switchable" ] <light> (LR,gLight) { mqtt=">[broker:sonoff/cmnd/sonoff_MasterBedroom/POWER4:command:*:default],<[broker:sonoff/stat/sonoff_MasterBedroom/POWER4:state:default]" }

my sitemap file is

sitemap sonoff label="Sonoff Switches"
{
    Frame label="Date"
    {
        Text item=Date
    }

    Frame label="Master Bedroom"
    {
        Switch item=Sonoff4ch_4Channel1 label="Asma Bedside Light" icon="light"
        Switch item=Sonoff4ch_4Channel2 label="Mufid Bedside Light" icon="light"
        Switch item=Sonoff4ch_4Channel3 label="Bedside Light 1" icon="light"
        Switch item=Sonoff4ch_4Channel4 label="Bedside Light 2" icon="light"
    }
}

This is from my logs

2018-05-13 14:01:17.259 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'sonoff.sitemap'
2018-05-13 14:01:17.565 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item for widget org.eclipse.smarthome.model.sitemap.Text
2018-05-13 14:01:17.568 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item 'Date' for widget org.eclipse.smarthome.model.sitemap.Text
2018-05-13 14:01:17.570 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item 'Date' for widget org.eclipse.smarthome.model.sitemap.Text
2018-05-13 14:01:17.572 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item 'Date' for widget org.eclipse.smarthome.model.sitemap.Text
2018-05-13 14:01:17.574 [WARN ] [basic.internal.render.SwitchRenderer] - Cannot determine item type of 'Sonoff4ch_4Channel1'
org.eclipse.smarthome.core.items.ItemNotFoundException: Item 'Sonoff4ch_4Channel1' could not be found in the item registry
	at org.eclipse.smarthome.core.internal.items.ItemRegistryImpl.getItem(ItemRegistryImpl.java:70) [109:org.eclipse.smarthome.core:0.10.0.b1]
	at org.eclipse.smarthome.ui.internal.items.ItemUIRegistryImpl.getItem(ItemUIRegistryImpl.java:724) [155:org.eclipse.smarthome.ui:0.10.0.b1]
	at org.eclipse.smarthome.ui.basic.internal.render.SwitchRenderer.renderWidget(SwitchRenderer.java:55) [217:org.eclipse.smarthome.ui.basic:0.10.0.b1]
	at org.eclipse.smarthome.ui.basic.internal.render.PageRenderer.renderWidget(PageRenderer.java:172) [217:org.eclipse.smarthome.ui.basic:0.10.0.b1]
	at org.eclipse.smarthome.ui.basic.internal.render.PageRenderer.processChildren(PageRenderer.java:137) [217:org.eclipse.smarthome.ui.basic:0.10.0.b1]
	at org.eclipse.smarthome.ui.basic.internal.render.PageRenderer.processChildren(PageRenderer.java:158) [217:org.eclipse.smarthome.ui.basic:0.10.0.b1]
	at org.eclipse.smarthome.ui.basic.internal.render.PageRenderer.processPage(PageRenderer.java:100) [217:org.eclipse.smarthome.ui.basic:0.10.0.b1]
	at org.eclipse.smarthome.ui.basic.internal.servlet.WebAppServlet.service(WebAppServlet.java:159) [217:org.eclipse.smarthome.ui.basic:0.10.0.b1]
	at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:848) [88:org.eclipse.jetty.servlet:9.3.22.v20171030]
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:584) [88:org.eclipse.jetty.servlet:9.3.22.v20171030]
	at org.ops4j.pax.web.service.jetty.internal.HttpServiceServletHandler.doHandle(HttpServiceServletHandler.java:71) [191:org.ops4j.pax.web.pax-web-jetty:6.0.7]
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) [87:org.eclipse.jetty.server:9.3.22.v20171030]
	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548) [85:org.eclipse.jetty.security:9.3.22.v20171030]
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:226) [87:org.eclipse.jetty.server:9.3.22.v20171030]
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1180) [87:org.eclipse.jetty.server:9.3.22.v20171030]
	at org.ops4j.pax.web.service.jetty.internal.HttpServiceContext.doHandle(HttpServiceContext.java:284) [191:org.ops4j.pax.web.pax-web-jetty:6.0.7]
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:512) [88:org.eclipse.jetty.servlet:9.3.22.v20171030]
	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) [87:org.eclipse.jetty.server:9.3.22.v20171030]
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1112) [87:org.eclipse.jetty.server:9.3.22.v20171030]
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) [87:org.eclipse.jetty.server:9.3.22.v20171030]
	at org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection.handle(JettyServerHandlerCollection.java:80) [191:org.ops4j.pax.web.pax-web-jetty:6.0.7]
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134) [87:org.eclipse.jetty.server:9.3.22.v20171030]
	at org.eclipse.jetty.server.Server.handle(Server.java:534) [87:org.eclipse.jetty.server:9.3.22.v20171030]
	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:333) [87:org.eclipse.jetty.server:9.3.22.v20171030]
	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251) [87:org.eclipse.jetty.server:9.3.22.v20171030]
	at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:283) [79:org.eclipse.jetty.io:9.3.22.v20171030]
	at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:108) [79:org.eclipse.jetty.io:9.3.22.v20171030]
	at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:93) [79:org.eclipse.jetty.io:9.3.22.v20171030]
	at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.executeProduceConsume(ExecuteProduceConsume.java:303) [90:org.eclipse.jetty.util:9.3.22.v20171030]
	at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceConsume(ExecuteProduceConsume.java:148) [90:org.eclipse.jetty.util:9.3.22.v20171030]
	at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:136) [90:org.eclipse.jetty.util:9.3.22.v20171030]
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:671) [90:org.eclipse.jetty.util:9.3.22.v20171030]
	at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:589) [90:org.eclipse.jetty.util:9.3.22.v20171030]
	at java.lang.Thread.run(Thread.java:748) [?:?]

also there is the following error in the logs.

2018-05-13 14:12:18.579 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'sonoff4ch.items'
2018-05-13 14:12:18.628 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'sonoff4ch.items' has errors, therefore ignoring it: [1,57]: missing EOF at '<'

2018-05-13 14:12:18.655 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'sonoff4ch.items' has errors, therefore ignoring it: [1,57]: missing EOF at '<'

I think there is a problem with linking the switches. where am I going wrong here. Also in the basic UI it does show the switches now.

ok I figured it out.

I changed the switchable field

from

Switch Sonoff4ch_4Channel1 "Channel 1" [ "Switchable" ] <light> (LR,gLight) { mqtt=">[broker:sonoff/cmnd/sonoff_MasterBedroom/POWER1:command:*:default],<[broker:sonoff/stat/sonoff_MasterBedroom/POWER1:state:default]" }

to

Switch Sonoff4ch_4Channel1 "Channel 1" <light> (LR,gLight) [ "Switchable" ] { mqtt=">[broker:sonoff/cmnd/sonoff_MasterBedroom/POWER1:command:*:default],<[broker:sonoff/stat/sonoff_MasterBedroom/POWER1:state:default]" }
1 Like

Yes, order of arguments is important:

Itemtype Itemname "Itemlabel" <icon> (group/groups) [ "Tag" ] {binding/bindings}

First and second is mandatory while all other arguments are optional, but you mustn’t change the order.
Jfyi:
There is a new task ‘quantitytype’ which is defined in items as well:

Itemtype:Quantity Itemname "Itemlabel" <icon> (group/groups) [ "Tag" ] {binding/bindings}

But it is not well documented yet…