MQTT items and things file

  • Platform information: raspberry pi 4 4gb
    • Java Runtime Environment: zulu java
    • openHAB version: latest

hi,
I have a shelly button1 that i want to use in openhab, my mqtt bridge etc al work fine,
I’m already controlling other stuff with it.

now this is the first time I’m using a ‘button’ a.k.a. a wireless switch that i later want to add to scenes etc…

the topic is shellies/shellybutton1-A4CF12F455A3/input_event/0

but the reply is: '{“event”:“S”,“event_cnt”:9}

S meaning short pres en event_cnt the number of times in total the button has been pressed (not important)

when i try to make it a switch i get 2020-07-07 18:06:04.981 [WARN ] [ab.binding.mqtt.generic.ChannelState] - Command '{"event":"S","event_cnt":9}' not supported by type 'OnOffValue'

when I make it a string, I get zero erros or nothing in the log

Channels:

this is how it’s in my items file at the moment
Type string : msg "Praktijkdeur" [stateTopic="shellies/shellybutton1-A4CF12F455A3/input_event/0"]

That all seems to be working as designed.

You could use a JSONPATH transformation to extract the event, “S”

But then what would you do with it? It is possible to make “S” set a Switch Item ON. That’s less use than it appears, there’s nothing to ever turn it off.

It’s probably worth at this stage looking what else you can get out of it - are there other vents than “S”?
You might want to have the topic just trigger openHAB events, and not set an Item at all.

It’s indeed just a wireless button I want to use as a garage door remote.

I have a relay configured with a rule that turns of 3 secs after it gets turned on.

When openhab received the S event from mqtt I would like to be able to use that event in a rule

As @rossko57 mentioned, you can add a JSONPath transformation to extract the S, like so:

Type string : msg "Praktijkdeur" [ 
   stateTopic="shellies/shellybutton1-A4CF12F455A3/input_event/0",
   transformationPattern="JSONPATH:$.event"
]

You then attach that to an Item (we can’t give you a full example because we can’t see your entire things file). In your rules, you can then watch that Item, and if it changes receives a command you can perform some actions.

If you also want to catch how many times your button was pressed, simply add another channel to your thing, and extract event_cnt, again using the JSONPath transform.

Note that by default, incoming stateTopic payloads are converted to Item state changes.
You could set a rule up to listen for Item state updates, which still occur for updates-to-same-state.
(We can’t use change, because we wouldn’t see two “S” events)

But in this case where the button press is a “do something” instruction from a user, it fits more comfortably into the openHAB model as a command, same as if you poke a UI button.
The MQTT binding includes a feature to enable this behaviour, the postCommand option.
Seems you’d probably want to do that @jeroenvdd
https://www.openhab.org/addons/bindings/mqtt.generic/#channel-configuration

I’ve added the transformation pattern.
but I’ll have to read up on jonsonpath since i get the error:

2020-07-08 10:15:07.029 [WARN ] [t.generic.ChannelStateTransformation] - Transformation service JSONPATH for pattern $.event not found!

It means what it says. JSONPATH is an installable optional service for openHAB. It needs installing.

hehe, I wasn’t kidding when I said I need to read up on JSONPATH,

I’ve now installed it,

2020-07-08 10:25:24.913 [ERROR] [.moquette.broker.NewNettyMQTTHandler] - Error processing protocol message: SUBSCRIBE
java.lang.NullPointerException: null

Is the next error but i think the previous error might like a reboot of the openhab service

I want to make the thing a switch but atm i get this error:

==> /var/log/openhab2/openhab.log <==
2020-07-08 12:38:27.767 [WARN ] [ab.binding.mqtt.generic.ChannelState] - Command 'S' not supported by type 'OnOffValue': No enum constant org.eclipse.smarthome.core.library.types.OnOffType.S
  Channels:
Type switch : power "Praktijkdeur" [
  stateTopic="shellies/shellybutton1/input_event/0",
transformationPattern="JSONPATH:$.event",
on="JSONPATH($.event)]"
] 

can i convert the s to “on”

I wonder whether on="s" might work?

unfortunatly it doesn’t,
maybe i need to transform the s in on with regex

Why not keep the channel type as a string, then act upon a command received inside a rule, as mentioned a few posts up?

1 Like

on="s" is not the same as on="S"

Reminder; once your Switch is turned ON, there is nothing to ever turn it off. What use are you planning to make of this switch?

i was thinking about using the thing i use on my ‘software pulse buttons’

rule "draag button2"
when
Item deur received command ON
then
createTimer(now.plusMillis(1500), [ | deur .sendCommand(OFF) ] )
end

If you want a rule to listen for a command, then you’ll have to make the MQTT message into a command as advised earlier.

You can link your MQTT string type channel to an openHAB String type Item and listen for command ‘S’. Or just any command at all.
This would also allow for any other tricks your button deviee can do, maybe it sends "D’ for doubleclick, no idea.

yeah it sends L for longpress,
(its a shelly button1)

how can i turn only the S into a command? since the count doesnt matter to me

postCommand="true"

well i got where I wanted to get :slight_smile:

{
  Channels:
Type switch  : power "Praktijkdeur" [
  stateTopic="shellies/shellybutton1/input_event/0",
transformationPattern="JSONPATH:$.event",
postCommand="true",on="S"
] 
            

}

combined with

Switch Praktijkdeur "Praktijkdeur" <light> { channel="mqtt:topic:praktijk:deurknop:power" }

rule "draag button2"

when

    Item Praktijkdeur received command ON

then

    createTimer(now.plusMillis(1500), [ | Praktijkdeur.sendCommand(OFF) ] )

end

if my button is ofline shorty after sending a command again i get:

Unexpected exception while processing MQTT message. Closing Netty channel. CId=shellybutton1-A4CF12F455A3
java.io.IOException: Connection reset by peer

is this a harware issue?
or something wrong with the broker?

No idea what that’s about. Going to guess this is a battery powered device that powers down (drops off WiFi) after use. “Connection reset by peer”

You’re using the embedded Moquette broker. This is known to have minor issues, maybe getting excited about devices dropping offline is one. i have no idea if there is any configuring that might sooth it for this case.

You could live with the logging if it seems to have no ill effects - check especially for delays in use.
Or change to another broker like Mosquitto, as Moquette appears to be end of life with no further fixes coming.