MQTT, Switch Item, Status and Time-based publishing

hi,

i’m an absolute beginner with openhab, so i wanted you to help me.

at first, i got a response from my arduino via mqtt with maybe “01100100” (it’s binary, but a string)

how do i split this value for 8 switches and set the correct status?

the second question.

if i - for ex. - press switch “sw_one”, what have i to do - if possible - that ~250ms later but depented on the status-value an other mqtt-publish-command is send by openhab?

  • sw_one pressed -> first command/publish
  • status arrived + time + 250ms
  • 2nd command published
  • status arrived -> set switch-icon and label-info
  • after maybe 2minutes 3rd command published

thanks so much
regards

Use a REGEX transformation. For example the following will give you the third switch:

REGEX(".*\d\d(\d)\d\d\d\d\d.*")

Assuming that the Item is a Switch or a Contact OH should handle the converting the 1 to ON/OPEN and 0 to OFF/CLOSED.

OH is not a real-time system. 250ms may be too fast of a response time depending on your hardware, load, and a whole host of other parameters. That being said:

rule "Switch One Triggered"
when
    Item sw_one received command
then
    // publish first command
    Thread::sleep(250)
    // check Items which receive status
    // publish second command
    Thread::sleep(250)
    // check Items which receive status
    Thread::sleep(120000)
    // publish third command
end

Notice I don’t mention “set switch-icon and label-info”. You can’t do that directly from a rule. All you can do is update and Item’s state. How you configure that Item or how you put it into the Sitemap determines how that Item appears on your sitemap. In other words, the icon and label is controlled by the Item’s state, you don’t change it directly. See the Item wiki page and pay particular attention to the label and icon fields.

ok, but how and where do i have to write it?
am i right that i have to put this in a map-file?
if so, did i have to make 8 files for this?

did i unterstood right, i am not able to change a label text from a rule?

Here is an example of a REGEX transform from the MQTT wiki page

Number mfase1 "mfase1 [%.3f]" {mqtt="<[flukso:/sensor/9cf3d75543fa82a4662fe70df5bf4fde/gauge:state:REGEX(.*,(.*),.*)]"}

No, REGEX transforms are inline as part of the binding string like the example above.

No, you do need eight Switch Items though, one for each and you will need to move the parens in the REGEX above depending on which Switch you are defining.

Mostly correct. Per the Items wiki page

The label text is used on the one hand side to display a description for the specific item e.g. in the sitemap, on the other hand to format or transform the output of the item.

In other words the label has two halves. On the left is a static String you cannot change except by editing the items or sitemap file and on the right is a formatted version of the Item’s state. For example:

"A String Item [%s]"

This label will put “A String Item” on the left hand side of the row on the sitemap and whatever the current state of the Item is to the right. See the Item page for bunches of examples of how to format the states of various types of Items.

Thus, you cannot directly change the label of an Item from a Rule. You can only change its state.

An alternative but similar approach to Rich’s would be have a single item that takes in the string of bits:

String AllSwitches { mqtt="<[broker:/switches:state:default]" }
Switch Switch1
Switch Switch2
...

and then have a rule that triggers when the state of that item changes (untested):

rule UpdateSwitches
when
  Item AllSwitches changed
then
  val String bits = AllSwitches.state.toString
  for (var i = 0; i < bits.length; i++) {
     postUpdate(String::format("Switch%d", i), if (s.charAt(i) == '1') ON else OFF)
  }
end

where is this from?

i give it a try, rightly after i got the regex tested, til now, i got errors in the designer.

Switch BB55_01 "Light (4)" { mqtt=">[mosquitto:i2c/dig/1_pin:command:ON:20.13.4.1.500],>[mosquitto:i2c/dig/1_pin:command:OFF:20.13.4.0.500],<[mosquitto:i2c/20/13:state:REGEX(".\d\d\d(\d)\d\d\d\d.")]", autoupdate="false" }


In the example rule, you have a bits variable of type String, which is a Java String that has a length() method. In the Xtend-like rule language, the empty parentheses can be omitted.

With this
REGEX([0..1]([0..1])[0..1][0..1][0..1][0..1][0..1][0..1])
i got no errors with the pattern: 01000000 i got an “1” shown in the status label.

but how to map it to ON or OFF? the switches are not responding with this …

openhab.log
[WARN ] [.c.i.events.EventPublisherImpl] - given new state is NULL, couldn't post update for 'sw02'

item
Switch sw02 "Switch 02 (Bit 4)" { mqtt=">[mosquitto:dig/1_pin:command:ON:20.13.4.1.500],>[mosquitto:BB55/hctl/v1/ardu01/sub/cmd/out/i2c/dig/1_pin:command:OFF:20.13.4.0.500],<[mosquitto:20/13:state:REGEX([0..1][0..1][0..1]([0..1])[0..1][0..1][0..1][0..1])]", autoupdate="false" }

how can i map this to ON/OFF for the buttonstate?