Issue of the topic: split of a string gives error
‘split’ is not a member of ‘org.eclipse.smarthome.core.library.types.StringType’
Do I need to load a library, there are examples using split, with no mention of a library.
I receive the following via MQTT :“25|BUTTON|0” or “25|BUTTON|255” I need to split the string up into 3 parts, two integers and the string. Then I apply them to a case statement to set switches etc. What is the best method of performing this task in a rule?
It would be easier to diagnose if you’d posted your rule, but it sounds like you are trying to split the item (StringType) instead of the state… item.state.split(<String>).
val string1 = itemName.state.toString.split("|").get(0)
val string2 = itemName.state.toString.split("|").get(1)
val string3 = itemName.state.toString.split("|").get(2)
Thanks everyone, nearly there.
I have modified my rule to simplify it so it include redundant declarations, but at least I can trace whats going on:
Here is the rule
rule "DMX Payload from MQTT"
when
Item DMX_QLCPLUS_Status changed
then
var MQTTmessage = DMX_QLCPLUS_Status.state
QLC_MQTT_Message_string.postUpdate(MQTTmessage.toString)
var MQTTsplit = MQTTmessage.toString.split("|")// returns an array with three elements
val button = MQTTsplit.get(0)
val widget = MQTTsplit.get(1)
val widget_value = MQTTsplit.get(2)
QLC_button.postUpdate(button)
QLC_widget.postUpdate(widget)
QLC_widget_value.postUpdate(widget_value)
//val num = Integer::parseInt(transformtest.replace('[', '').replace(']', ''))
end
The response in tail is as follows:
2018-06-20 17:55:07.645 [vent.ItemStateChangedEvent] - DMX_QLCPLUS_Status changed from 14|BUTTON|0 to 20|BUTTON|0
2018-06-20 17:55:07.682 [vent.ItemStateChangedEvent] - QLC_MQTT_Message_string changed from 14|BUTTON|0 to 20|BUTTON|0
2018-06-20 17:55:07.699 [vent.ItemStateChangedEvent] - QLC_button changed from 1 to 2
2018-06-20 17:55:07.704 [vent.ItemStateChangedEvent] - QLC_widget changed from 4 to 0
The get(x) is returning just a single indexed character not a part of the payload. As widget_value does not change, it is not in tail.
Anybody got any ideas why I am getting single characters not a proper split?
rule "DMX Payload from MQTT"
when
Item DMX_QLCPLUS_Status changed
then
var String MQTTmessage = DMX_QLCPLUS_Status.state.toString
QLC_MQTT_Message_string.postUpdate(MQTTmessage)
//var MQTTsplit = MQTTmessage.toString.split("|")// returns an array with three elements
val String button = MQTTmessage.split("|").get(0)
val String widget = MQTTmessage.split("|").get(1)
val String widget_value = MQTTmessage.split("|").get(2)
QLC_button.postUpdate(button)
QLC_widget.postUpdate(widget)
QLC_widget_value.postUpdate(widget_value)
end
rule "DMX Payload from MQTT"
when
Item DMX_QLCPLUS_Status changed
then
var String MQTTmessage = DMX_QLCPLUS_Status.state.toString
QLC_MQTT_Message_string.postUpdate(MQTTmessage)
//var MQTTsplit = MQTTmessage.toString.split("|")// returns an array with three elements
val String button = MQTTmessage.split("|").get(0)
logInfo("GET0", button)
val String widget = MQTTmessage.split("|").get(1)
logInfo("GET1", widget)
val String widget_value = MQTTmessage.split("|").get(2)
logInfo("GET2", widget_value)
QLC_button.postUpdate(button)
QLC_widget.postUpdate(widget)
QLC_widget_value.postUpdate(widget_value)
end
Fixed it, thanks to you Vince, lead me down the right path, what we missed was that the ‘|’ character is not a recognised split character, it must be a weird ascii code that is outside the range of split. I did a .replace to get csv, and then the split was good.
Here’s the new code:
rule "DMX Payload from MQTT"
when
Item DMX_QLCPLUS_Status changed
then
var String MQTTmessage = DMX_QLCPLUS_Status.state.toString.replace('|',',')
QLC_MQTT_Message_string.postUpdate(MQTTmessage)
val button = MQTTmessage.split(",").get(0)
val widget = MQTTmessage.split(",").get(1)
val widget_value = MQTTmessage.split(",").get(2)
QLC_button.postUpdate(button)
QLC_widget.postUpdate(widget)
QLC_widget_value.postUpdate(widget_value)
end
and heres the log output:
2018-06-20 18:26:04.136 [vent.ItemStateChangedEvent] - DMX_QLCPLUS_Status changed from 11|BUTTON|0 to 11|BUTTON|255
2018-06-20 18:26:04.216 [vent.ItemStateChangedEvent] - QLC_MQTT_Message_string changed from 11,BUTTON,0 to 11,BUTTON,255
2018-06-20 18:26:04.233 [vent.ItemStateChangedEvent] - QLC_widget_value changed from 0 to 255
Heres the screen output - this may not show properly:
Now for the next stage of getting QLCplus and websockets with node-red and MQTT working. Buts thats another topic altogether.