Item rules question

Hey there, all you wise ones!

Total first-timer here and my question might 'cause hilarity or cringe, but after all the research and tests I gave up and came here to ask this.

Here’s the scenario:

I have an arduino “box” full of different sensors and I have succesfully managed to make it communicate with OpenHAB via MQTT. I have made a sitemap and the items, but I can’t figure out a way to create the proper rules/configuration to make everything show up in OpenHAB.

Say if I had a mqtt message coming from Arduino flame sensor, stating “fire1” or “fire0” (indicating whether arduino detects fire or not) and that would change the item “Fire Sensor” state to “ON” or “OFF”.
I can’t figure out a way to properly make it work, mqtt comes through but my item’s values don’t change the way I want it to.

I wish I could get some sort of a practical example for such a simple operation, but I can’t find the info I need after experimenting/researching for hours. (I know this sounds really noobish to most of you, but hey I tried to make this work, many times)

Help, please?

Please post the applicable part of your .items and .sitemap.

SItemap:

sitemap sebi label=“mylittlesetup”
{
Frame label=“LivingRoom”
{
Switch item=sebi_pir label="Motion"
Switch item=sebi_flame label=“Flame sensor”
}
}

Items:

Switch sebi_flame
Switch sebi_pir

Switch sebi_flame_mqtt {mqtt="<[broker:topic:state:default]"}
Switch sebi_pir_mqtt {mqtt="<[broker:topic:state:default]"}

That’s my current setup. Changed several times thoroughly for experimenting. (More sensors will be added once I get these two working)
Idea was that, once sebi_sensor_mqtt value changes, swtich sebi_sensor value changes from OFF to ON etc.

Sorry, would you also post your .things? Forgot that OH2 has things as well.

Also, have you verified that your sensor is posting to the MQTT server?

For this I think you don´t need to go through a rule. Since this is a feedback change it to a Contact item type and use a mapping in the item definition like this:

Contact sebi_flame_mqtt {mqtt="<[broker:topic:state:default:MAP(mqttbool.map)]"}
Contact sebi_pir_mqtt {mqtt="<[broker:topic:state:default:MAP(mqttbool.map)]"}

Make a mapping file in the Transformation folder named mqttbool.map containing this:

fire1=CLOSED
fire0=OPEN

To get a good icon use the one named fire ref:
http://docs.openhab.org/addons/iconsets/classic/readme.html

Is your broker named broker or do you use mosquitto, might be that you also need to check your mqtt.cfg file.

1 Like

Gave your item definition and mapping a go. Didn’t work out, and apparently mosquitto(which i am using for mqtt) is not getting through after all…

Earlier I tested the connection with a simple String item to see if the message gets through and it did. OpenHab subscribed to mosquitto.
Trying now with “sebi” results in mosquitto not getting a subscription from OpenHab. (Also tried with different topics and messages, so no “overruling” would happen)

mqtt.cfg

URL to the MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883
mqtt:mymosquitto.url=tcp://localhost:1883

Optional. Client id (max 23 chars) to use when connecting to the broker.
If not provided a default one is generated.
mqtt:mymosquitto.clientId=openhab

Optional. True or false. Defines if the broker should retain the messages sent to
it. Defaults to false.
mqtt:mymosquitto.retain=false

for persistence and eventbus configurations I have also named the broker as ‘mymosquitto’ and have changed nothing else.
What is it, that I’m not understanding here?

Edit: this is what my item definition looks like in full:
String sebi_flame_mqtt {mqtt="<[mymosquitto:sebi_living/flame:state:default:MAP:mqttbool.map]"}

In your mqtt.cfg you should not have mqtt: in front of each line since the cfg file it self tells the binding it is related to.

My mqtt.cfg file below but if you remove mqtt: for each config line it should be fine:

#
# 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
mosquitto.url=tcp://openhabianpi.local:1883

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

# 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.
mosquitto.qos=2

# 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>

For the item it might be a need for a / at the start of the string, see example from my example item below. This of course depends on what your components are sending. To test the mqtt-broker there are several clients that you can download for free and use from a Mac or Windows computer to see the topics sent to the broker. Just subscribe to # and you will see all messages. Then you also can publish to the broker for testing against openhab. I use MQTT fx on Mac.

Contact GarasjeClosed "Garasjeport [%s]" <garagedoor> (Garasje) {mqtt="<[mosquitto:/garasje/out/10:state:MAP(mqttbool.map)]"}

I see you use mymosquitto in the mqtt.cfg and .items files. Is this a setting you have selected somewhere? My installation is a plain unconfigured installation from openhabianpi and I use mosquitto not mymosquitto.

For the mapping it should be parenthesis around the mapping file, see item example above.

Hi,

@TheApprentice I’ll give you an example

sitemap:

sitemap sebi label="mylittlesetup"
{
  Frame label="LivingRoom"
  {
    Text item=sebi_pir
    Text item=sebi_flame
  }
}

items:

String sebi_flame "Flame sensor [%s]" <switch> {mqtt="<[mymosquitto:>topic<:state:default]"}
String sebi_pir "Motion [%s]" <switch> {mqtt="<[mymosquitto:>topic<:state:default]"}

please fill in >topic< your MQTT Topic like home/room1/flame or home/room1/pir

greeting
Peter

Alrighty
I changed the configurations like you suggested and the messages are now getting through. Weird that the test config worked fine with the old settings.

But that doesn’t matter anymore, the point here is that everything is working like a charm! :slight_smile:

Thank you all!