Mqqt state item

  • Platform information:

    • Hardware: Rasperry Pi 3
    • OS: Noobs
    • openjdk version “1.8.0_212”
  • Issue of the topic:
    Hello!
    I’m new to this OH thing, at least at editing files so sorry if my questions seem dumb.
    I want to build an item that reads the state of my sonoff relay but I don’t know what exactly to put in my commands. I tried different codes, but none seems to work. I want to use this state item to work with Webhooks and IFTTT. When someone uses the relay, it triggers a scene through Ifttt.
    I already managed to configure a switch button that works very well on this sonoff, but that doesn’t interest me.

    • Items configuration related to the issue
Switch Sonoff1 "Sonoff_1" { mqtt=">[broker:house/cmnd/house-light-1/power:command:*:default],<[broker:house/stat/house-light-1/power:state:default]" }
//house/cmnd/house-light/POWER -m on
String  State1sonoff "State_1:[%s]" { mqtt="<[broker:house/stat/house-light-1/state:state:default]"}
  • Sitemap configuration related to the issue
sitemap home label="Sonoff Switches" //creez o casa numita Sonoff Switches
{
       Frame label="Camera test"   //creez camera test
       {
               Switch item=Sonoff1     //adaug un itemin camera test
                                      //itemele trebuie create in fisierul .item
               Text item=State1sonoff
       }
}

Welcome to the OpenHAB community, excellent first post!

To access an items state, you need

myitem.state

Also helpful is

myitem.state.toString

Sorry I’m on my phone

Which version of the MQTT binding do you have installed? If you are on OH 2.4 or later and just installed MQTT then you are almost definitely running the MQTT 2 binding. Your Item syntax is for the MQTT 1 binding. See Using Sonoff Power Switches with Tasmota firmware and openHAB2 MQTT2 binding which should get you started.

I uninstalled mqtt binding 2.4 and turned on legacy binding 1.xx. Do you know if my code is correct?

Did you configure the binding in mqtt.cfg?

I can’t just look at the code and say whether it is correct or not. Nothing looks wrong but I’m looking at it on my phone and I’m not a computer.

Have you verified that the messages are being published?

Are there errors in the logs?

This is the only configuration I made in mqtt.cfg:

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

The switch button works ok but I don’t want to publish messages, just to subcribe. I want to know when the state is changed manually on the switch. Also I don’t know where to find the logs.

LE:
I got it working by adding new lines in mqtt.cfg

pid:org.openhab.mqtt
broker.clientId=openhab2

And changing the state item to:

String  State1sonoff "State_1:[%s]" { mqtt="<[broker:house/stat/house-light-1/POWER:state:default]"}

I put POWER instead of state.

Thanks guys, I look forward to asking new questions.