[SOLVED] Telegram - format String like Number

  • Platform information:

    • Hardware: Synoology Diskstatio DS218play, Realtek RTD1296
    • OS: DSM 6.2-23739 Update 2 (Custom Linux)
    • Java Runtime Environment: Java8, 1.8.0_181
    • openHAB version: 2.3.0 Release Build
  • Issue of the topic: Problem with string format

I have a Item with following definition:

Number ProxyAussen_Temp1 			"Labor / Keller  [%1.1f°C]"     <temperature> (Temperaturen, EG_Aussen, UnterG)

The format ([%1.1f°C] works well in my sitemap and app.
I send the same item with Telegram on my handy (at given times or for testing by a trigger).
Here is my problem with the format. In the Telegram rule i want to format it as the same like in the sitemap (Number with one digit).

First i thougt the format is defined in the item defination. In the telegram rule i have to convert into a string. At the same time the string gets its format. Actually i have a workaround:

/* Temperatur senden */ 
rule "Telegram_Test1"

when
    Time cron "0 0 18 * * ?" or Time cron "0 0 6 * * ?" or Item MySwitch3 changed to ON
then
    sendTelegram("Benedikt", "Aussentemperatur Labor beträgt %.4s °C", ProxyAussen_Temp1.state.toString) // ergibt "21.6 °C", String auf 4 Zeichen beschränkt
end

I get the right format only if temperature is over 10 °C, if its under 10 there will be 2 digits after the comma.

For me there are two questions:

  • Can i convert the item “Number ProyAussen_Temp1” into a String including the format?
  • Is there a other method instead of “.state.toString” (i tryed “.state.floatValue” but i did not work)?

If you change the item to use [%.1f °C] how does that effect your setup?

Try this:

/* Temperatur senden */ 
rule "Telegram_Test1"

when
    Time cron "0 0 18 * * ?" or Time cron "0 0 6 * * ?" or Item MySwitch3 changed to ON
then
    sendTelegram("Benedikt", String::format("Aussentemperatur Labor beträgt %.1f °C", ProxyAussen_Temp1.state)) // ergibt "21.6 °C", String auf 4 Zeichen beschränkt
end

I was editing my post to include String::format, needed to double check first, when you replied.:laughing:
I gotta get faster fingers and a bigger memory.:rofl:

Hello Guys,

sorry for my late reply - wow - your answers were very fast - thank you.

I tryed your solution proposal but i did not work.
Log:

Blockquote
2018-11-29 20:43:21.520 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Telegram_Test1’: f != org.eclipse.smarthome.core.library.types.DecimalType

Is ther anywhere a explanation of the syntax (i am searching for Java examples but i think it is not realy the same)?

Btw: I am used to PLC programming… scripting in open hab is a little bit tricky for me

Hello benostra.

Try this, that worked for me.

sendTelegram ("bot","Guten Morgen, die Wetterlage ist heute "+Condition.state+" die Aussentemperatur beträgt "+T_Aussen.state+" Grad und der Pufferspeicher hat "+P_Puffer.state+"%.")}

CU

Thanks for your answers, lot of try an error until now…learning by doing :grinning:

I read the information for posting, but i think i have forgotten some information…
Once again for better understanding:

I get my values from a plc (modbus binding) as integer - so there is no comm, and the values are xx *10. So first what i do ist to dividide with 10 (for example 247 from PLC means 24,7 °C).

Itemdefinition:

Number Aussen_Temp1 				{modbus="slave3:0"}

First rule:

rule "AdjustTemp1"
when
    Item Aussen_Temp1 received update
then
    ProxyAussen_Temp1.sendCommand((Aussen_Temp1.state as DecimalType) / 10)
end

Itemdefinition for Sitemap:

Number ProxyAussen_Temp1 			"Labor / Keller  [%.1f °C]"     <temperature> (Temperaturen, EG_Aussen, UnterG)

Rule for telegram:

/* Temperatur senden */ 
rule "Telegram_Test1"

when
    // Item MySwitch3 changed to ON 
    Time cron "0 0 18 * * ?" or Time cron "0 0 6 * * ?" or Item MySwitch3 changed to ON
then
        sendTelegram("Benedikt", "Aussentemperatur Labor beträgt %.4s °C", ProxyAussen_Temp1.state.toString) // ergibt "21.6 °C", String auf 4 Zeichen beschränkt
end

Whithout the limitation to 4 digits (%s instead of %.4s) i get the telegramm message with many digits after the comma.

Aussentemperatur Labor beträgt 6.10000000 °C

I want to have always at least one digit before and one digit after the comma. I thougt the item ProxyAussen_Temp1 is already formated [%.1f °C], and this format is taken over with the item.state.toString method, but it does not work…

@Matz666:
May i ask how your items are defined?

It is formatted in its label. Not in the value itself.
Did you try:

sendTelegram("Benedikt", "Aussentemperatur Labor beträgt %.1f °C", ProxyAussen_Temp1.state as Number) // ergibt "21.6 °C", String auf 4 Zeichen beschränkt

Or try:

rule "Telegram_Test1"

when
    // Item MySwitch3 changed to ON 
    Time cron "0 0 18 * * ?" or Time cron "0 0 6 * * ?" or Item MySwitch3 changed to ON
then
    val String temp1 = ProxyAussen_Temp1.state.toString.format(".1f")
    sendTelegram("Benedikt", "Aussentemperatur Labor beträgt " + temp1 + " °C") // ergibt "21.6 °C", String auf 4 Zeichen beschränkt
end

Why not use modbus 2.x binding with read an write transformations? This way oh would always “see” correct values.

Hy benostra.

Sorry for the late Answer.

Items

String P_Puffer   "Pufferspeicher [%s %%]"                  		<cistern>			(puffer)	{ mqtt="...........
String   Condition        "Wetterlage [%s]"   <weather>  (gWetter)  {channel="weatherunderground............
Number T_Aussen   "Aussentemperatur [%.2f °C]"                  	<temperature>		(gHZG,gPoolchart)	{ mqtt="<[mosquitto

I remember that i got this Problem by an Item that get his state from 3 other Items.
Here ive got to “floatValue”

rule "Wetterstatus"
when
	System started or
	Item Condition changed or
	Item Temperature changed or
	Item Wind_Speed changed or
	Item test changed
then
	temperatur = (Temperature.state.toString)
	wind = String::format("%.2f",(Wind_Speed.state as DecimalType).floatValue())
	wetterstatus.postUpdate(""+Condition.state+", "+temperatur+"°C, "+wind+"km/h")
end

or you try to round like in this thread.

https://community.openhab.org/t/rounding-a-number-in-a-rule/7842/26

And at last. Have you tried postUpdate instead of sendCommand???

CU MATZ

First try:

sendTelegram(“Benedikt”, “Aussentemperatur Labor beträgt %.1f °C”, ProxyAussen_Temp1.state as Number)
Error message
2018-12-16 10:44:23.318 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Temperatur_senden’: f != org.eclipse.smarthome.core.library.types.DecimalType

Second try:

val String temp1 = ProxyAussen_Temp1.state.toString.format(“.1f”)
sendTelegram(“Benedikt”, “Aussentemperatur Labor beträgt " + temp1 + " °C”)
no error, but format not ok
“Aussentemperatur Labor beträgt 2.70000000 °C”

Hello @gitMiguel,

i don’t see a modbus 2.x binding, only Modbus Binding binding-modbus1 - 1.12.0 in PaperUi.

You need upgrade your openHAB server to 2.4. It’s not yet included in 2.3.

To all of you:

thank you for your support and patience, got it finally to work:

    sendTelegram("Benedikt", "Aussentemperatur Labor beträgt "+String::format("%.1f°C", (ProxyAussen_Temp1.state as DecimalType).floatValue()))

Recieved Message:
Aussentemperatur Labor beträgt -2.4°C