[SOLVED] Rule to convert string to number

Hi,

I need some help with one of the rules.
I’m trying to convert string to number, but I’m lacking skills.
Topic grew out from here, but I think it’s more proper to open new one under rules category:
https://community.openhab.org/t/http-table-value-pulling-and-conversion-with-rules/68356

items:

String Electricity_price "Electricity_price [%s]" {http="<[https://www.nordpoolgroup.com/api/marketdata/page/47?currency=EUR&endDate={}:60000:JSONPATH($.data.Rows[0].Columns[0].Value)]" }
Number Electricity_price2 "Electricity_price2 [%.2f]"
Switch Renew_price "Renew_price"

rules:

var Number conversion
var String get_Electricity_price 
 rule "conversion"
when
  Item Renew_price changed
  //Item Electricity_price changed //or
  //  System started //or
    //Item Electricity_price2 == NULL //gives error
  
then
    get_Electricity_price = Electricity_price.state.toString();
    //conversion = Integer.valueOf(get_Electricity_price);
    conversion = Double.parseDouble(get_Electricity_price);
    Electricity_price2.postUpdate(conversion);
end 

about triggering conditions:
As input data is updated only once per day, I created a switch to run the rule (while testing).
Ideally I would like to run the rule on startup and when value changes.

about script block:
I get error for conversion.
log:

2019-02-27 22:09:13.098 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'conversion.rules'

2019-02-27 22:09:34.850 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'conversion': For input string: "34,05"

Please share ideas. Thanks

I think you need to replace the German comma decimal separator with a dot.
If there may be a thousands dot separator, you’ll need to scrub those out first.

1 Like

Try this:

Electricity_price2.postUpdate(Float::parseFloat(String::format("%s",Electricity_price.state).replace(',','.')))

8 Likes

That worked :slight_smile: Thanks. I had never thought of the coma problem.

Hi @wjaceh, I am trying to read the dutch hourly prices into openhab. Please can you help me in the right direction. What do I have to do to import, once a day, the hourly prices from:

I have OH3.2 and a quite newbe with this concept.