How to add mqtt device to openhab with embedded mqtt broker in my.items file?

Platform information:

  • Hardware: Raspberry Pi 3 b+
  • OS: openhab 2.4

I’ve openHab 2.4 , with embedded mqtt broker, I’ve a basic sonoff with tasmota firmware .
the sonoff mqtt topic is “sonoff1”

I’ve created the channel via paper ui with :
MQTT state topic: tele/sonoff1/STATE
MQTT command topic: cmnd/sonoff1/POWER

everything works fine till now, the sonoff1 item is working fine
but when I tried to add the item via ssh to the my.items , I could not struct the syntax !
I just want to add the mqtt item in the my.items file.

this where I stopped in my.items file
Switch Sonoff1 “Sonoff 1” [ “Switchable” ]

thanks

Use this channel name:
image
This is just an example, your channel name will be different.
Click on the little two square to copy it

Then in your items file:

Switch Sonoff1 "Sonoff 1" [ "Switchable" ] { channel="mqtt:topic:BathroomLight:OnOff" }

I recommend that you set up samba and VSCode

2 Likes

thanks a lot for your respond :slight_smile:

Can I use the following structure ?
{mqtt=">… instead of using the channel ?

NO, this is the syntax for the MQTTv1 binding. Your are using MQTTv2

1 Like

thanks again , I just want to create the item via coding not paper ui , is this possible ?

Yes

can you tell me please :kissing_heart::kissing_heart::kissing_heart:

As you did above,
You even created an item file

I cant write the right syntax to make the mqtt connection .

Switch Sonoff1 "Sonoff 1" <poweroutlet> [ "Switchable" ] {mqtt=">[mqtt:sonoff1/POWER:command:ON:on],>[mqtt:sonoff1/POWER:command:OFF:off]"}

this is not working

No, I told you earlier. I will not work.
You asked if you could use that syntax and I said no and you used it anyway.
If you don’t follow my advice, it will not work.
You need to use the channel syntax

1 Like

sorry I did not mean to upset you.
thanks your advice is great.
is there anyway to create the channel via coding not paper ui ?
I just need to keep my setup in code, that is easier for me to make duplicates openhab setups

Yes but I advise against it.

Use back ups or the REST api

1 Like

It’s all documented here.

And Vincent provided an example in his first reply for how to link a Channel to an Item.

There are tons of examples on the forum. The original developer (and others including myself) recommend against manually defining Things in .things files. People who do so, especially those who don’t yet have a lot of experience with OH, end up having a ton of problems getting the syntax right.

When you do stuff in PaperUI it gets saved to a JSON formatted text file that you can copy to other OH setups just as easily as you can with .items and .things files.

BTW, in this example you are using mqtt version 1.x syntax. Try it using syntax for mqtt version 2 and follow @vzorglub advice above.

1 Like