Formatting item value output for notification

Hi,

I have a problem depending a notification in a rule. Every morning I will receive a message with the actual measures outdoor temperature from one of my temperature sensors (WEATHER1Aussen_Temperature.state).
The item (WEATHER1Aussen_Temperature; number) is configured with paper ui. My problem is, that the measured temperatures from the sensors are in the following format, e.g.: 12.9000000000000003552713678800500929355621337890625. Exactly with this format I receive my notification. Is it possible to configure the output format for the item value in the sendNotification command?

rule "Aktuelle Temperatur 7 Uhr"
when
Time cron "0 0 7 1/1 * ? *"
then
logInfo("rules", "Die aktuelle Außentemperatur beträgt " +WEATHER1Aussen_Temperature.state+ " °C")
sendNotification("xxx@xxx.de","Die aktuelle Außentemperatur beträgt " +WEATHER1Aussen_Temperature.state+ " °C")
end

Thank you for your help.

Greetings Johannes

Hi,

you can try something like

sendNotification("xxx@xxx.de","Die aktuelle Außentemperatur beträgt " +String.format("%.2f",WEATHER1Aussen_Temperature.state)+ " °C")

Regards,

André

Hello André,

thank you for your help. I have tried your proposal without success. I get an error message:

[ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule Aktuelle Temperatur 7 Uhr: f != org.eclipse.smarthome.core.library.types.DecimalType

Sorry. Yes of course - my fault. Converting a state is sometimes a little bit tricky…

sendNotification("xxx@xxx.de","Die aktuelle Außentemperatur beträgt " +String.format("%.2f",((WEATHER1Aussen_Temperature.state as DecimalType).floatValue())+ " °C")

Hope this works now - I currently don’t have the chance to double check it.

1 Like

Perfect, Thank you, thumbs up. Works as expected by you.
Please can you explain to me why I need at the end of the expression

.floatValue()

The rest of the expression I understand.

Thank you!

So the idea is first to say to openHAB that the state object we have is of DecimalType (and not e.g. a DateTimeType,OnOffType and so on). Next we have to convert that Eclipse Smart Home Object (!) to something Java understands. And that is then done by the method .floatValue() which is a method of DecimalType and which returns a float value, which can then used to do everthing you can do with a float value :grin:

In my configuration this expression works: WEATHER1Aussen_Temperature.state.format("%.2f") + " °C"

Fair point :wink:. For pure formatting of the value for a string this is the leaner approach. For those who a interested the method “format” of the public interface “Type” it is described here:

https://eclipse.org/smarthome/documentation/javadoc/org/eclipse/smarthome/core/types/Type.html#format-java.lang.String-

I have used as you suggested.
it works.as expected.
thanks a lot
My rule file is as below

rule "Trial for Notificaiton"
when
   Item Socket1 changed

then
   sendBroadcastNotification("Socket1 is Switched "+ String.format(Socket1.state.toString), "light" ,"high")
end