[SOLVED] RULES: error message dot is replaced by a comma state.format("%.1f")

Hi,
I use the following items and rule to generate the temperature from two-digit decimal to single digits for Alexa.

items:

Number 	    ZWUS_Pool_T1V		"Temperatur Pool [%.1f °C]" 	[ "CurrentTemperature" ]
Number 	    ZWUS_Pool_T1		"Pool Wasser Temperatur [%.1f °C]" 		                  	```

rules:

  /* ------------------------------------------------------------------------ */
 /* Alexa Temperatur Pool | ZWUS_Pool_T1V 
/* ------------------------------------------------------------------------- */

rule "Alexa Temperatur Pool | ZWUS_Pool_T1V"
when
	Item ZWUS_Pool_T1 received update
then
	ZWUS_Pool_T1V.postUpdate(ZWUS_Pool_T1.state.format("%.1f"))
end

Yesterday I set the locale setting in openhabian-config to German.

Since then I get the following error message:

2019-04-04 08:02:40.710 [vent.ItemStateChangedEvent] - ZWUS_Pool_T1 changed from 7.75 to 7.81

2019-04-04 08:02:40.714 [WARN ] [rthome.model.script.actions.BusEvent] - Cannot convert '7,8' to a state type which item 'ZWUS_Pool_T1V' accepts: [DecimalType, QuantityType, UnDefType].

The reason for the error message is that now the dot is replaced by a comma.

Does anyone know how to reuse the rule without changing the language set up again?

I think it’ll work if you don’t format it for the postUpdate.
You don’t need to format the transfer, because you format the result for display anyway.

If I ask alexa for the temperature, she answer all decimal places.

Okay, i see.

Well, the problem is the format using the locale but the postUpdate doesn’t. Really I think there should be an enhancement request for postUpdate to understand numbers in current locale.

Meantime we can workaround by doing maths instead.

var Number temp = (((ZWUS_Pool_T1.state as Number)*10).intValue)/10
ZWUS_Pool_T1V.postUpdate(temp)

Thanks

I changed the rule:

  /* ------------------------------------------------------------------------ */
 /* Alexa Temperatur Pool | ZWUS_Pool_T1V 
/* ------------------------------------------------------------------------- */

rule "Alexa Temperatur Pool | ZWUS_Pool_T1V"
when
	Item ZWUS_Pool_T1 received update
then
	var Number temp_ZWUS_Pool_T1 = (((ZWUS_Pool_T1.state as Number)*10).intValue)/10
	ZWUS_Pool_T1V.postUpdate(temp_ZWUS_Pool_T1)
end

Now i got the following:

2019-04-04 13:44:27.793 [vent.ItemStateChangedEvent] - ZWUS_Pool_T1V changed from 11.87 to 11

what I want is: ZWUS_Pool_T1V 11.9 or 11.8

I also use a different temperature sensor this way.
This gives me the values to three decimal places.

Well, you can see how it works. Why don’t you break it down into steps, and log the numbers out to see what is happening.
Knowing the number you started with would help at least.

Umm, that’s nice. Was there a problem about that?

I’ve already done that, but it’s totally funny.
I hope you understand what the different is?

  /* ------------------------------------------------------------------------ */
 /* Alexa Temperatur Pool | ZWUS_Pool_T1V 
/* ------------------------------------------------------------------------- */

rule "Alexa Temperatur Pool | ZWUS_Pool_T1V"
when
	Item ZWUS_Pool_T1 received update
then
	var Number temp_ZWUS_Pool_T1= (ZWUS_Pool_T1.state as Number)
	logInfo("Rules", "Alexa Temperatur Pool 1: " + temp_ZWUS_Pool_T1)

	temp_ZWUS_Pool_T1 =  ((temp_ZWUS_Pool_T1 *10).intValue)
	logInfo("Rules", "Alexa Temperatur Pool 2: " + temp_ZWUS_Pool_T1)

	temp_ZWUS_Pool_T1 = temp_ZWUS_Pool_T1 /10
	logInfo("Rules", "Alexa Temperatur Pool 3: " + temp_ZWUS_Pool_T1)
	
	temp_ZWUS_Pool_T1 = (((ZWUS_Pool_T1.state as Number) *10).intValue) /10
	logInfo("Rules", "Alexa Temperatur Pool 4: " + temp_ZWUS_Pool_T1)

	ZWUS_Pool_T1V.postUpdate(temp_ZWUS_Pool_T1)
end

Only what I’m looking for: 12.3 is not there. :sob:

Try postUpdate the .toString version of your result, postUpdate can be picky

Excuse me, but what do you mean? WHERE should I use .toString?

MyItem.postUpdate(someNumber.toString)

Good Morning,

Thank you for your support.
I have tested it, but unfortunately the result remains the same.

The value is only represented as an integer and not as a comma with one decimal place.

Does somebody has any idea?

Have you tried
ZWUS_Pool_T1V.postUpdate(ZWUS_Pool_T1.state as Number)
in your original code?

I tested that. The problem is that I would like to have the value with one decimal place.

It’s really curious that

temp_ZWUS_Pool_T1 = ((temp_ZWUS_Pool_T1 *10).intValue)
temp_ZWUS_Pool_T1 = temp_ZWUS_Pool_T1 /10

gives a different result to

temp_ZWUS_Pool_T1 = (((ZWUS_Pool_T1.state as Number) *10).intValue) /10

I guess in the all-in-one case the interim value is of type integer - we did ask for intValue after all - and dividing integer still gives integer.
In the two-step process, the interim integer gets converted to the variable’s declared Number type, before moving on to the division, which then results in a decimal…

So you know how to do it - two steps.

In my code I have some strange conversions like this:

ZWUS_Pool_T1V.postUpdate((ZWUS_Pool_T1.state as DecimalType).floatValue)

Maybe that helps you, too?
I’m referring to your first example, without the workaround of multiplying and dividing by 10.

The formatting with one decimal place should be done by the Item ZWUS_Pool_T1V itself, as in your initial definition.

SOLVED:

var Number temp_ZWUS_Pool_T1= (ZWUS_Pool_T1.state.format("%.1f"))
temp_ZWUS_Pool_T1 = (Float::parseFloat(String::format("%s",temp_ZWUS_Pool_T1).replace(',','.')))

Rules:

  /* ------------------------------------------------------------------------ */
 /* Alexa Temperatur Pool | ZWUS_Pool_T1V 
/* ------------------------------------------------------------------------- */

rule "Alexa Temperatur Pool | ZWUS_Pool_T1V"
when
	Item ZWUS_Pool_T1 received update
then
	var Number temp_ZWUS_Pool_T1= (ZWUS_Pool_T1.state.format("%.1f"))
	logInfo("Rules", "Alexa Temperatur Pool 1: " + temp_ZWUS_Pool_T1)
	
	temp_ZWUS_Pool_T1 = (Float::parseFloat(String::format("%s",temp_ZWUS_Pool_T1).replace(',','.')))
	logInfo("Rules", "Alexa Temperatur Pool 2: " + temp_ZWUS_Pool_T1)
	
	ZWUS_Pool_T1V.postUpdate(temp_ZWUS_Pool_T1)
end

LOG: