[Solved] OH3.1.0 python script - string to number conversion

  • Platform information:
    • Hardware: Intel J4105/4GB
    • OS: Ubuntu 18.04
    • Java Runtime Environment: Openjdk 11.0.11
    • openHAB version: 3.1.0

I’ve got rule which executes python script to get data from solar inverter. It was working fine in OH3.0 but it doesn’t work anymore since upgrading to latest stable version OH3.1.0.


rule “solar inverter”
when
Time cron “0 0/5 * 1/1 * ? *”
then
if(solarman.state == ON) {
var String invdata = executeCommandLine(Duration.ofSeconds(10),"/usr/bin/python", “/var/lib/openhab/INVT-inverter/getPVData.py”)
invtstring.postUpdate(invdata)
val data1 = invtstring.state.toString.split("\n").get(0)
invttot.postUpdate(data1) //worked with OH 3.0
val data2 = invtstring.state.toString.split("\n").get(1)
invtday.postUpdate(data2)
val data3 = invtstring.state.toString.split("\n").get(2)
invtcurrent.postUpdate(data3)
logInfo(“results”, data1)
}
end

Below are errors from openhab log


2021-12-08 7:19:31.689 [WARN ] [b.core.model.script.actions.BusEvent] - Cannot convert ‘12471.4’ to a state type which item ‘invttot’ accepts: [DecimalType, QuantityType, UnDefType].
2021-12-08 7:19:31.692 [WARN ] [b.core.model.script.actions.BusEvent] - Cannot convert ‘12.66’ to a state type which item ‘invtday’ accepts: [DecimalType, QuantityType, UnDefType].
2021-12-08 7:19:31.694 [WARN ] [b.core.model.script.actions.BusEvent] - Cannot convert ‘3170’ to a state type which item ‘invtcurrent’ accepts: [DecimalType, QuantityType, UnDefType].
2021-12-08 7:19:31.695 [INFO ] [rg.openhab.core.model.script.results] - 12471.4

Items config


String invtstring
Number invttot “inverter total [%.1f]”
Number invtday “inverter daily [%.1f]”
Number invtcurrent “inverter current power [%.1f]”

It seems that the python script executes ok but there is an issue with updating number items? Any idea how to resolve it?

Here’s a magic trick, try
postUpdate(invttot, data1)
The Action accepts strings

Interesting that you were able to switch the font to courier. But as you can see that’s not really helpful as all the white space is lost. When posting code and logs and the like please use code fences.

```
code goes here
```

You can also use the icons at the top of the post entry form.

1 Like

thanks, I changed rule as per your advice but I receive same warning as before

rule "solar inverter"
when
    Time cron "0 0/5 * 1/1 * ? *"   
then
    if(solarman.state == ON) {
	var String invdata = executeCommandLine(Duration.ofSeconds(10),"/usr/bin/python", "/var/lib/openhab/INVT-inverter/getPVData.py")
	invtstring.postUpdate(invdata)
	val data1 = invtstring.state.toString.split("\n").get(0)
	postUpdate(invttot, data1)
	val data2 = invtstring.state.toString.split("\n").get(1)
//  invtday.postUpdate(data2)
	val data3 = invtstring.state.toString.split("\n").get(2)
//  invtcurrent.postUpdate(data3)
//  logInfo("results", data1)
	}
end
2021-12-09 6:58:00.644 [WARN ] [b.core.model.script.actions.BusEvent] - Cannot convert '12494.7
' to a state type which item 'invttot' accepts: [DecimalType, QuantityType, UnDefType].

thank you

Oh, I forgot - two strings for that
postUpdate("invttot", data1)

Nope, still same warning. Is there any other way to convert string to number?

In a rule I’ll use below

 mode = int(str(event.itemState))

thanks, can you share full example? I am not sure how can I apply this to my rule.

Only thing I can think of, you’ve got some invisible control characters in your strings.
Try
postUpdate("invttot", "3.142")
to prove stuff works.
Try
logInfo("test", "Chars " + data1.length)
for character count.

Sure, it’s called ‘parsing’.

To clarify, are you using RulesDSL or Jython/Python?

Below works without errors

postUpdate("invttot", "3.142")

Length of data1 is

2021-12-10 05:17:01.126 [INFO ] [org.openhab.core.model.script.test  ] - Chars 8

String is separated by line breaks

logInfo("test", invtstring.state.toString())

returns

2021-12-10 09:15:30.704 [INFO ] [org.openhab.core.model.script.test  ] - 12509.0
2.10
2534

how do I check?

You must be using the Rules DSL - if you configured python you would know.

Ok, in that case I cannot test your suggestion?

And that is this string with 7 visible characters, yes?

Guess you have output separate NL/CR characters from your script.

    logInfo("results", data1)
	logInfo("test", "Chars " + data1.length)
	logInfo("results", data2)
	logInfo("test", "Chars " + data2.length)
	logInfo("results", data3)
	logInfo("test", "Chars " + data3.length)

returns

2021-12-10 05:35:01.616 [INFO ] [rg.openhab.core.model.script.results] - 12509.8
2021-12-10 05:35:01.618 [INFO ] [org.openhab.core.model.script.test  ] - Chars 8
2021-12-10 05:35:01.619 [INFO ] [rg.openhab.core.model.script.results] - 2.98
2021-12-10 05:35:01.634 [INFO ] [org.openhab.core.model.script.test  ] - Chars 5
2021-12-10 05:35:01.636 [INFO ] [rg.openhab.core.model.script.results] - 2957
2021-12-10 05:35:01.637 [INFO ] [org.openhab.core.model.script.test  ] - Chars 5

No, @rossko has the right stuff to get you going :slight_smile:

I tried by using “parsing” and it seems to be working so far. Below is the code

rule "solar inverter"
when
    //Time cron "0 0/5 * 1/1 * ? *"  
then
    if(solarman.state == ON) {
	var String invdata = executeCommandLine(Duration.ofSeconds(10),"/usr/bin/python", "/var/lib/openhab/INVT-inverter/getPVData.py")
	invtstring.postUpdate(invdata)
	val data1 = invtstring.state.toString.split("\n").get(0)
	invttot.postUpdate(Float::parseFloat(data1))
	val data2 = invtstring.state.toString.split("\n").get(1)
    invtday.postUpdate(Float::parseFloat(data2))
	val data3 = invtstring.state.toString.split("\n").get(2)
    invtcurrent.postUpdate(Float::parseFloat(data3))
	}
end

How can I convert invttot.postUpdate sentences to below format while still using parsing? Or is it not necessary?

postUpdate("invttot", data1)