Number From String in Rules[SOLVED]

I want to do a sendMail when an item’s value is less than a number but the item is a String because of exec2 binding issues as
per https://github.com/openhab/openhab2-addons/issues/2149
My rule looks like:
when
Time cron "0 59 21 * * ?"
then
if(Double::parseDouble(pwrHWS.state) < 1000) {
sendMail(blah blah)
}
end

not sure if I have the “if” line correct yet but I get:

21:49:01.144 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule send mail for testing : An error occurred during the script execution: Could not invoke method: java.lang.Double.parseDouble(java.lang.String) on instance: null

I tried adding this to the top of the rules file:
import java.lang.Double.parseDouble

but I obviously am guessing at the right syntax. It seems you can no longer use * in the import line as suggested in earlier forum issues. Any suggestions ?

Assuming pwrHWS is an String item which is bound to the output channel of the exec Binding.

Is there an item pwrHWS and does this have a value in pwrHWS.state

you can use the karaf console to display the items.

smarthome:items list
exec_Out (Type=StringItem, State=NULL, Label=null, Category=null)

As i think what you want to achieve is as following.

  1. use the exec binding to get a value, as string.
  2. send an mail.

I would suggest to set the thing to execute in a defined intervall and then execute the rule to see if the is smaller than 1000.
And not to set a cronjob to check the output which is maybe not initialized at all.


exec.things
Thing exec:command:pwrHWS [
        command="<YOUR_COMMAND>",
        interval=10,
        autorun=false]

value.items
// Output of command line execution 
String pwrHWS_out { channel="exec:command:pwrHWS:output" }

email.rules
rule "Email"
  when
     Item pwrHWS_out changed
  then

    if(Double::parseDouble(pwrHWS_out.state.toString ) < 1000) {
        sendMail(blah blah)
    }

end

But if you want the check to be explicitly done with a cronjob at a specific time. Then you can use a setup like this.


exec.things
Thing exec:command:pwrHWS [
        command="<YOUR_COMMAND>",
        interval=0,
        autorun=true]

value.items
Switch pwrHWS_status { channel="exec:command:pwrHWS:run" }
String pwrHWS_Arg { channel="exec:command:pwrHWS:input"}
// Output of command line execution 
String pwrHWS_out { channel="exec:command:pwrHWS:output" }

email.rules
rule "Email"
  when
     Time cron "0 59 21 * * ?"
  then
    // execute the command
    pwrHWS_Arg.sendCommand("1")

     // wait for the command to complete, state will be NULL if not used before or ON while command is executed
      while(pwrHWS_status.state != OFF){
         Thread::sleep(500)
      }

    // Do your thing
    if(Double::parseDouble(pwrHWS_out.state.toString ) < 1000) {
        sendMail(blah blah)
    }

end

Everything Josar said is a good idea but your specific problem is you need to parse a String but you are trying to pass a State object to parseDouble. Try

if(Double::parseDouble(pwrHWS.state.toString) < 1000) {

You are right for sure, how could i miss that although i had the same problem days ago. aaaarggg

Thanks to you both. This solves the problem:

I guess I misunderstood the docs where it states:

Conversion of Item.state to String
All Item states can be converted into a string by invoking MyItem.state.toString

I was looking for a function that would convert a String to a Number for the comparison with the number 1000 because the Item is already a String. I would have thought state.toNumber would make more sense. Perhaps some examples of this in the docs would help. How do I post this suggestion?

Now that it is working I have added the item value to the content of the email:

sendMail("rsomeName@gmail.com", “OH2 Email Alert HWS Power”, “HWS Power is: “+ pwrHWS.state.format(”%s”))

so in the email I get
HWS Power is: 38.01
Nice!

And you found it with your Double::parse

That is the source of your confusion. An Item is an Item. When you call My item.state you get a State. To convert a State to a String you call My item.state.toString, just as that part of the docs you quoted says with it’s example. This is true for all tires of Items, even String Items.

You mean like the four examples that exist just a few paragraphs down from the line you quoted?

http://docs.openhab.org/configuration/rules-dsl.html#number-item

Thanks for yr explanations. That’s helpful.
I hope you didn’t think I was being critical because no, the four examples do not help unless you already have a full understanding of the function in which case they clarify syntax. To help people like me who are not developers or programmers it would be more helpful to show the full case where this can be applied. Even more so since this docs page does not even mention Double::parseDouble and thus assumes the user has knowledge of the available functions and their application.
I am sure there are many like me who want to build a great OH system (and thanks to all contributors for this product) for myself and along the way learn as much as possible about system coding/programming or whatever.
I hope that is seen as helpful feedback. I am working on the assumption that this community is for all users and not just skilled developers. Let me know if I am wrong.
Thanks again for the support.

This is really almost impossible because there are literally an infinite number of examples that would be required to show the full number of use cases.

Unfortunately, this is going to be a requirement no matter what. We can do our best to give the user tools to learn these things but we cannot present an example of every example of every way one can do something in the rules. To just take your current use case, I count no fewer than eight ways one can convert a String to a Number. And that is just for this one little use case. And the examples are repetetive.

We could argue whether there should be a floating point example in the Number’s section but to expect the docs to have examples for every possible combination of use cases is unfortunately unreasonable. We’d spend all our time writing up examples instead of adding to the code and no one could use the docs because you can’t find anything with all of the examples.

We welcome all comers but at the end of the day home automation is hard and users who want to be successful with home automation will have to learn at least a little bit of coding skills. Every user runs into something in their rules that do not make sense or doens’t work like they assume it should. We try to cover all those cases in our docs but the OH docs are not “learn how to code” but doing so is impossible. This is why we have the beginnners section of the Forum.

Things will get somewhat better over time. There is a new Experimental Rules Engine being written which hopefully will make it easier for non-programmers to accomplish things in OH without learning too much coding. But until that happens it is unavoidable.

And I just realized I never answered your question. To make suggestions file an issue on the openhab-docs repo on GitHub. Even better, you can make the changes yourself and submit them once you file the issue.