Cannot convert state from string to number


first I want to say: thanks for that great system!
I am coming from Pilight and controlling my 433MhZ transmitter and receiver mostly with bash scripts and Pilight.
I switched two places to openhab and its incredible how much and complex you can control stuff!
I will dive into Z-Wave or other solutions later, but for now I try to use my current setup as good as possible. Most of the stuff is working so far (controlling my switchable power outlets), but I have some sensor data (for example I use an old android phone to get the light data). This data is written into a log file on the raspi.

As far as I red, bash script results from reading files always come in a string-format. This I can see in the rest-api:

link "" state "1254" stateDescription pattern "%s" readOnly true options [] editable false type "String"

But I cannot figure out, why it is not possible to convert that string-state into a number…

Here is my items config:

String licht_homer “Licht [%s]” { channel=“exec:command:licht_homer:output” }
Number licht_homer_number “Licht [%.1f]”

My things:

Thing exec:command:licht_homer [command="/etc/openhab2/scripts/", interval=60, timeout=5]

And my rule:

import org.openhab.core.library.types.DecimalType
import java.lang.Double.parseDouble

var boolean Abend = false
var String licht_homer
var Number licht_homer_number

rule “Abenddaemmerung”
Channel ‘astro:sun:Abenddaemmerung:set#event’ triggered START
//or Time cron “/10 * * * * ?” //for testing

//var licht_homer_number = (Float::parseFloat(String::format("%s",licht_homer.state).replace(’ ‘,’’)))
//var licht_homer_number = licht_homer.state.toString.replace(" “,”")
//var Integer licht_homer_number = Integer::parseInt(licht_homer.state)
//var Number licht_homer_number = (licht_homer.state as DecimalType).intValue()
//licht_homer_number.postUpdate(Float::parseFloat(String::format("%s",licht_homer.state).replace(’ ‘,’’)))
//var licht_homer_number = (Double::parseDouble(licht_homer.state) as Number)
var licht_homer_number = (licht_homer.state as Number)

logInfo(“light”, (licht_homer_number))

if (licht_homer_number < 300) {
logInfo(“light”, “night”)

else {
logInfo(“light”, “day”)



gives me this log-entry:

2019-02-18 15:06:50.052 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule ‘Abenddaemmerung’: ‘state’ is not a member of ‘java.lang.String’; line 176, column 27, length 17

to be shure that the state is there, changed it:

var licht_homer_number = (licht_homer as Number)

gives me:

2019-02-18 15:11:50.025 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule ‘Abenddaemmerung’: Could not cast licht_homer (Type=StringItem, State=958, Label=Licht, Category=null) to java.lang.Number; line 176, column 27, length 21

all right, but the “state” is there…
I tried “postUpdate” (gives me: ‘postUpdate’ is not a member of java.lang.String…), “item.state.toString” (again: ‘state’ is not a member of java.lang.String…), all the different Integer::parse; Float::parseFloat and so on…

I am using a raspi 2B, Openhab 2.4 and Java 1.8.0_201.

Does anyone have an idea, why this ‘state’ error shows up?
I think it is a dumb error on my side, but I can’t see it…

Any help is very appreciated.
Thanks a lot!

I can’t test it here, but to give you an idea:

I think
logInfo(“light”, (licht_homer_number))
has to be
logInfo(“light”, licht_homer_number.state.toString)

var licht_homer_number = (licht_homer as Number)
has to be
var licht_homer_number = (licht_homer.state as Number)

Thanks for the answer, Felix.

But unfortunately I already did this and this gives me the error I mentioned:

2019-02-18 18:46:00.025 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule ‘Abenddaemmerung’: ‘state’ is not a member of ‘java.lang.String’; line 176, column 27, length 17

my updated rule with your code:

var licht_homer_number = (licht_homer.state as Number)
logInfo(“light”, licht_homer_number.state.toString)
if (licht_homer_number < 300) {
logInfo(“light”, “night”)
else {
logInfo(“light”, “day”)

very strange…

btw: exec binding is installed and working

Does this thread give you any ideas?

Hey Stuart,

thanks for your hint.
The value of licht_homer varies between (around) 60 and 2300 and the error occurs independently of that number…

This defines an Item

String licht_homer “Licht [%s]” { channel=“exec:command:licht_homer:output” }
Number licht_homer_number “Licht [%.1f]”

This creates a variable, of String type, in a global scope for this rules file.

var String licht_homer

Now, whenever you refer to licht_homer by name in this rules file, it refers to your (empty) variable.
Variables are not the same as Items.
String variables do not have a .state method.
There is no way to get at your Item because you have re-used the same name.

Thanks to all!
I really appreciate your help a lot!

Finally I found a way around the problem.
Instead of sending the light*variable in Tasker with the Rest-PlugIn to the String-Item “licht_homer”, I used the Number-Item “licht_home” (could have also used the already established Number-Item “licht_homer_number”…)
Now I can compare in my rule.
This solves my problem but not this Issue…should I close anyway?

If you want to use item.state as a string, the correct syntax should be (item.state).toString

Otherwise you try to find an object toString in the item.state

But toString is defined in object of result of item.state

If there are issues left:

Please put rule in separate file and show error again, so it‘s easier to find line if error in your rule.

And use the code block to insert here :wink:

Edit: you have a lot of var in your Rules at the beginning of Lines. You only need to declare variables once, then you can give them new values.

var Number xyz


Another thing… Please always use code fences when posting code, configuration or even logs, as it’s more readable. Furthermore discourse (this is the software running the forum) will change some chars when not marked as code. this is e.g. “” or ‘’

"" vs.  “” and '' vs. ‘’

and this is a big difference, as it’s code, not text.