String manipulation. How?

I need to update time of the control panel connected to OH. The current date and time should be send one character at a time, HHMMmmddyy should be divided to H, H etc. and sent by

sendDSCAlarmCommand('070','H')
sendDSCAlarmCommand('070','H')

etc.
How can I do it?

I thought about formatting the date and time into a string and then iterate throug the string char by char. Worst case, to write 10 lines of code, one per each char. Is there a way to get n-th character of a string item?

myString.charAt[i]

will give you the i’th character (starting from zero) of myString as a char.

Thanks.

I am probably doing something wrong:

import org.openhab.core.library.types.*
import java.util.Date
import java.text.SimpleDateFormat

    rule "dscSetTime"
    
    when
        Time cron "0 59 20 ? * *"
    then
        var SimpleDateFormat df = new SimpleDateFormat( "HHmmMMddYY" )
        var String Timestamp = df.format( new Date() )
        logInfo( "DSC", "Settime to:{}", Timestamp )
        logInfo( "DSC", "First char is:{}", Timestamp.charAt[0])
        .....
    end

because

logInfo( "DSC", "First char is:{}", Timestamp.charAt[0])

throws an exception:

2015-10-29 20:59:00.049 [INFO ] [org.openhab.model.script.DSC  ] - Settime to:2059102915
2015-10-29 20:59:00.064 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule dscSetTime
java.lang.IllegalStateException: Could not invoke method: java.lang.String.charAt(int) on instance: 2059102915
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeOperation(XbaseInterpreter.java:738) ~[na:na]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._featureCallOperation(XbaseInterpreter.java:713) ~[na:na]
	at sun.reflect.GeneratedMethodAccessor46.invoke(Unknown Source) ~[na:na]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_65]
	at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_65]
	at org.eclipse.xtext.util.PolymorphicDispatcher.invoke(PolymorphicDispatcher.java:291) ~[na:na]
	at org.openhab.model.script.interpreter.ScriptInterpreter.internalFeatureCallDispatch(ScriptInterpreter.java:69) ~[na:na]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateMemberFeatureCall(XbaseInterpreter.java:549) ~[na:na]
	at sun.reflect.GeneratedMethodAccessor51.invoke(Unknown Source) ~[na:na]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_65]
	at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_65]
	at org.eclipse.xtext.util.PolymorphicDispatcher.invoke(PolymorphicDispatcher.java:291) ~[na:na]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:218) ~[na:na]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluateArgumentExpressions(XbaseInterpreter.java:767) ~[na:na]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._featureCallOperation(XbaseInterpreter.java:712) ~[na:na]
	at sun.reflect.GeneratedMethodAccessor46.invoke(Unknown Source) ~[na:na]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_65]
	at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_65]
	at org.eclipse.xtext.util.PolymorphicDispatcher.invoke(PolymorphicDispatcher.java:291) ~[na:na]
	at org.openhab.model.script.interpreter.ScriptInterpreter.internalFeatureCallDispatch(ScriptInterpreter.java:69) ~[na:na]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateAbstractFeatureCall(XbaseInterpreter.java:658) ~[na:na]
	at sun.reflect.GeneratedMethodAccessor44.invoke(Unknown Source) ~[na:na]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_65]
	at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_65]
	at org.eclipse.xtext.util.PolymorphicDispatcher.invoke(PolymorphicDispatcher.java:291) ~[na:na]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:218) ~[na:na]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateBlockExpression(XbaseInterpreter.java:321) ~[na:na]
	at sun.reflect.GeneratedMethodAccessor78.invoke(Unknown Source) ~[na:na]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_65]
	at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_65]
	at org.eclipse.xtext.util.PolymorphicDispatcher.invoke(PolymorphicDispatcher.java:291) ~[na:na]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:218) ~[na:na]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:204) ~[na:na]
	at org.openhab.model.script.internal.engine.ScriptImpl.execute(ScriptImpl.java:59) ~[na:na]
	at org.openhab.model.rule.internal.engine.ExecuteRuleJob.execute(ExecuteRuleJob.java:55) ~[na:na]
	at org.quartz.core.JobRunShell.run(JobRunShell.java:213) [quartz-all-2.1.7.jar:na]
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557) [quartz-all-2.1.7.jar:na]
Caused by: java.lang.IllegalArgumentException: argument type mismatch
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_65]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_65]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_65]
	at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_65]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeOperation(XbaseInterpreter.java:729) ~[na:na]
	... 36 common frames omitted

Any hints?

This should be

Timestamp.state.toString.charAt(0)

Does not work, Timestamp is not an item but var of String type, therefore .state method is unknown

2015-10-29 22:24:00.060 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule dscSetTime
java.lang.RuntimeException: The name '<XFeatureCallImplCustom>.state' cannot be resolved to an item or type.

However Timestamp.charAt(0) worked just fine…

So the problem was with [] instead of ()

woops, missed that. But changing the ‘[ ]’ to ‘( )’ is still a valid correction.

Timestamp.charAt(0)

Not sure if it fixes anything though.

Thank you so much, now it works fine.

Finally I had to use code like this

sendDSCAlarmCommand('070',Timestamp.charAt(1).toString)

as charAt was rejected when supplied for string argument to binding fuction…

How if I would like to cut out 3 characters after a specific phrase / chanracter?
I have a string:
2016-11-17T14:46:00 act:H dcd:6A ccd:227 orig:0 cat:L'
and would like to get only “6A” (after dcd:) and “227” (after ccd:).
Is this possible with charAt as well?

Is the message a fixed size every time?

Is the message always of the same format (i.e. [standard date time] [three letters]:[letter] [three letters]:[two characteters] …)?

If not you probably want to use a regular expression. Search the forum and you should find some examples.

1 Like

Thanks for the hint Rich,

I tried in a rule:
Bud_dcd_tmp = transform(Heat_actErrStr.toString, "REGEX", ".*dcd\:(.*).*")

Where
Heat_actErrStr = 2016-11-17T14:46:00 act:H dcd:6A ccd:227 orig:0 cat:L

but escaping the : with \ is not accepted.

The arguments are in the wrong order.

transform(<transform>, <file or transform path>, <string to transform>)

So you should have (note the val):

val Bud_dcd_tmp = transform("REGEX", ".*dcd:(.*).*", Heat_actErrStr)

Note, that REGEX will return everything after the “dcd:”

Thank you so much.
After the dcd: there’s the 2 character string.
How to ignore everything after the blank / space which comes right after the 2 characters?
:blush:

Something like one of the following

".*dcd:(.*) ccd.*"
".*dcd:(..).*"
".*dcd:([.]{2}).*"
".*dcd:(\S\S).*"
".*dcd:(.*)\b.*"