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?
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)
/* ------------------------------------------------------------------------ */
/* 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.
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
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…
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