Need help with things file for Sonoff switch

Try adding the ON and OFF as below:

Bridge mqtt:systemBroker:embedded-mqtt-broker [host=“openhab”, secure=false, retain=true] {
  Thing mqtt:topic:sonoffs26_1 “sonoffs26_1” @ “myHome/Living_Room” {
    Channels:
      Type switch : power “S26_1 On/Off” [
        stateTopic=“stat/sonoffs26_1/POWER”,
        commandTopic=“cmnd/sonoffs26_1/POWER”,
        on="ON",
        off="OFF"
      ]
  }
}

You’ve probably got two broker bridge Things now, one from PaperUI and one created from your file.
It is possible to make two bridges to the same broker but I think you need to be very careful about it. Just making a duplicate is doomed, I think.

In addition to @rossko57s statement, whenever creating things via file, look on PaperUI in order to get the correct syntax for the channel statement. You can’t change the thing created by file on PaperUi but it will show there!

I did try on=“ON” and off=“OFF” and just added it again but it made no difference.

Thanks rossko,
I think you are correct. I had the Bridge statement in both my nodemcu1.things and also sonoffs26.things files so I’ve removed the one from this file but it didn’t make any difference.
There is only one MQTT Binding showing under Bindings in PaperUI.
In Services I have MQTT Services Broker and MQTT system broker connection. Clicking Manage in the system broker connection show nothing. Are these correct?

I’ve no idea. You said you had one MQTT derived Item working, so if that still works you have a working broker. You probably need to link your other generic MQTT Thing to the correct bridge broker Thing.

It gets messy if you sdcatter your config across PaperUI, and multiple files as well, but it can be done…

If you do not use, or can not use the hierarchy -

Bridge blah { 
    Thing {
        blah
    }
}

Then you have to tell each topic Thing what Bridge it belongs to.

I’ve recreated the switch in PaperUI and again it is working.
Here is what I have in PaperUI for the working thing

Thing
Name = SonoffS26_1
Location = myHome/Living_Room
Bridge Selection = MQTT Broker-mqtt:systemBroker:embedded-mqtt-broker

Chanels
s26_1
mqtt:topic:e8c7a941:s26_1
MQTT State Topic = stat/sonoffs26_1/POWER
MQTT Command Topic = cmnd/sonoffs26_1/POWER

Linked Item
Label = S26_1 On/Off (s26_1)
Type = Switch

While testing I’ve found another strange behavior.
When testing the script I disable the GUI created thing and when testing the GUI created thing I change the thing file extension to disable it but when I do this all things go offline. In PaperUI they show UNINITIALIZED_HANDLER_MISSING_ERROR and I have to restart the openhab2 service to get them back online. Is this normal or is it part of my problem?

Many bindings are not so clever at handling in-flight edits of Things. It’s common to need to restart the binding at least to deal with it.

OK. I’ll ignore the offlines and just keep restarting after I have made changes.
Through experimenting with the things suggested I’ve found if I comment out the Bridge statement and add (mqtt:broker:127_0_0_1_1883) to my thing then the thing does not appear under things in PaperUI.

I have a two things files one for a nodemcu and the other for SonoffS26. Should I have the Bridge statement at the start of both or is it better to consolidate them into one things file with a Bridge statement at the start?

In my details of the PaperUI thing above there is a Link to s26_1 but s26_1 is not in my things file. Should it be?

No. Do not create duplicates.

You will find it easier to manage, but that’s up to you.

No idea. By editing and deleting stuff, you can create orphan links to non-existing Items, probably smart to remove if you think there is no Item.

Thanks rossko,
Am I getting frustrated.
Between posts I’ve continue to search the forum. In another thread it was suggested to create a Bridge thing which I did, removing the Bridge statements from my two things files and putting it in the bridge.things file but this didn’t work, nodemcu1 and sonoff26 things remained offline even after a reboot. Maybe it has something to do with the order in which openhab reads the things files.
I’ve now consolidated all scripted things into one file and the nodemcu1 is still working but my sonoffs26_1 is not.

Here are my items and new things files.

mythings.things

Bridge mqtt:systemBroker:embedded-mqtt-broker [host=“openhab”, secure=false, retain=true] {
Thing mqtt:topic:nodemcu1 “nodemcu1” @ “myHome/Outside” {
Channels:
Type number : temperature “Temperature” [
stateTopic=“tele/nodemcu1/SENSOR”,
transformationPattern=“JSONPATH:$.DS18B20.Temperature”
]
}
Thing mqtt:topic:sonoffs26_1 “SonoffS26_1b” @ “myHome/Living_Room” {
Channels:
Type switch : power “S26_1 On/Off” [
stateTopic=“stat/sonoffs26_1/POWER”,
commandTopic=“cmnd/sonoffs26_1/POWER”,
on=“ON”,
off=“OFF”
]
}
}

sonoffs26.items

Switch s26_1 “S26_1 On/Off [%s]” {channel=“mqtt:topic:sonoffs26_1”} // Not working
Switch s26_2 “S26_2 On/Off [%s]” {channel=“mqtt:topic:6ac5f883:sonoffs26_2”} // Working with GUI create thing

Have you looked on PaperUI in order to check the needed syntax for the channel of the sonoffS26_1?
My guess for the needed syntax would be:
Switch s26_1 “S26_1 On/Off [%s]” {channel=“mqtt:topic:embedded-mqtt-broker:sonoffs26_1”}

1 Like

If that one is working, then you must have another Bridge Thing as well as the one defined in your file, because 6ac5f883 is not the name of your file defined Bridge.

As I said earlier, you can have two Bridges to the same actual broker, but it needs to be carefully controlled. Two accidental links to same broker probably will not work.

1 Like

FYI,Support for the embedded broker has been dropped, you may want to use a different one e.g. Mosquitto.

Thanks opus,

I had already tried that syntax but it didn’t work but to be sure I tried it again and it still doesn’t work.
I’m restarting after every change now to make sure any changes I make are recognised.

Thanks everyone for you help, I found where I was going wrong.
I looked at the script created thing in PaperUI and as you can see below there is “power” at the end of the statement. I’ve been over this time and again and I’d swear that wasn’t there before. I added this to the switch definition in the items script and it worked.

image

Line from items file;
Switch s26_1 “S26_1 On/Off [%s]” {channel=“mqtt:topic:sonoffs26_1:power”} // Working

Thanks again to everyone who offered help, I was about to give up and stick with the PaperUI created thing. Now to change my second Sonoff S26 from GUI to script.

Update:
I configured my second Sonoff S26 using the first one as a template and it worked first time.
My next job is to get my devices displayed in ClassicUI to retain their values/state through a restart but I’ll make that a topic of another thread.
Thanks again everyone.

…and I tell you it was. Me bad, I should have noticed the missing .Power as well.

It was there all along in the channel info. I just looked at the PaperUI thing without opening it to see the channel I had created.