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?
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.
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.
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
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.