Error when casting Temperature value to E-Mail

Hi all,

I am working on an automatic e-mail report with various usage infos and statistics for the past week. getting desperate with my weather values. I managed to send my gas and power consumption and tried to do it for weather in the same manner but I always get an error:

2022-06-06 11:32:58.346 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'mail-1' failed: f != org.openhab.core.library.types.DecimalType in mail

The items for wind and temperature are dimensionless Number items.

I split up the calculation to find the source for the error and I guess it arises during the creation of the e-mail. This is the calculation part:

    var Rx = now.minusDays(7)
    var ReportWindMax1 = AT_Dachflache_Wind.maximumSince(Rx, "jdbc")
    logInfo ("Log Mailreport", "WindMax Woche: " + ReportWindMax1.toString)
    var Number ReportWindMax = ReportWindMax1.state as Number
    logInfo ("Log Mailreport", "WindMax Woche State: " + ReportWindMax.toString)
    
    var ReportWindAvg1 = AT_Dachflache_Wind.averageSince(Rx, "jdbc")
        logInfo ("Log Mailreport", "WindAvg Woche: " + ReportWindAvg1.toString)
    var Number ReportWindAvg = ReportWindAvg1 as Number
         logInfo ("Log Mailreport", "WindAvg Woche State: " + ReportWindAvg.toString)
   
    var ReportTempMax1 = AT_Dachflache_Temperature.maximumSince(Rx, "jdbc")
        logInfo ("Log Mailreport", "TempMax Woche: " + ReportTempMax1.toString)
    var ReportTempMax = ReportTempMax1.state as Number
        logInfo ("Log Mailreport", "WindAvg Woche: " + ReportTempMax.toString)

    var ReportTempLow1 = AT_Dachflache_Temperature.minimumSince(Rx, "jdbc")
        logInfo ("Log Mailreport", "TempLow Woche: " + ReportTempLow1.toString)
    var ReportTempLow = ReportTempLow1.state as Number
        logInfo ("Log Mailreport", "TempLow State: " + ReportTempLow.toString)

    var ReportTempAvg1 = AT_Dachflache_Temperature.averageSince(Rx, "jdbc")
        logInfo ("Log Mailreport", "TempAvg Woche: " + ReportTempAvg1.toString)
    var Number ReportTempAvg = ReportTempAvg1 as Number
        logInfo ("Log Mailreport", "TempAvg State: " + ReportTempAvg.toString)

Log result for this part:

2022-06-06 11:32:57.144 [INFO ] [hab.core.model.script.Log Mailreport] - WindMax Woche: JdbcItem [name=AT_Dachflache_Wind, state=28.44, timestamp=2022-06-02T14:09:52.597+02:00[Europe/Berlin]]
2022-06-06 11:32:57.147 [INFO ] [hab.core.model.script.Log Mailreport] - WindMax Woche State: 28.44
2022-06-06 11:32:57.972 [INFO ] [hab.core.model.script.Log Mailreport] - WindAvg Woche: 3.129599539862326
2022-06-06 11:32:57.975 [INFO ] [hab.core.model.script.Log Mailreport] - WindAvg Woche State: 3.129599539862326
2022-06-06 11:32:58.070 [INFO ] [hab.core.model.script.Log Mailreport] - TempMax Woche: JdbcItem [name=AT_Dachflache_Temperature, state=32.9, timestamp=2022-06-05T15:49:02.288+02:00[Europe/Berlin]]
2022-06-06 11:32:58.073 [INFO ] [hab.core.model.script.Log Mailreport] - WindAvg Woche: 32.9
2022-06-06 11:32:58.163 [INFO ] [hab.core.model.script.Log Mailreport] - TempLow Woche: JdbcItem [name=AT_Dachflache_Temperature, state=6.7, timestamp=2022-06-03T04:18:12.971+02:00[Europe/Berlin]]
2022-06-06 11:32:58.166 [INFO ] [hab.core.model.script.Log Mailreport] - TempLow State: 6.7
2022-06-06 11:32:58.284 [INFO ] [hab.core.model.script.Log Mailreport] - TempAvg Woche: 17.45758061814136
2022-06-06 11:32:58.287 [INFO ] [hab.core.model.script.Log Mailreport] - TempAvg State: 17.45758061814136

I use StringBuilder to create the e-mail. Here’s an excerpt for one consumption value (gas usage previous week) and for the maximum wind value:

    message.append("Gasverbrauch in der Vorwoche: " + String.format("%.2f", GasMeterVorwoche) +" m³")

    logInfo ("Log Mailreport", "Mail creation. Consumption table created successfully.")

    message.append("Wind Max: " + String.format("%.2f", ReportWindMax) +" m/s.")

I can see the (German) logInfo for the successful creation of the consumption table but the next message is the error:

2022-06-06 11:32:58.293 [INFO ] [hab.core.model.script.Log Mailreport] - Mailerstellung beginnt: Aufbau des Strings.
2022-06-06 11:32:58.342 [INFO ] [hab.core.model.script.Log Mailreport] - Mailerstellung. Verbrauchstabelle erstelt.
2022-06-06 11:32:58.346 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'mail-1' failed: f != org.openhab.core.library.types.DecimalType in mail

I tried to change the types in the var definition from “as Number” to “as Decimaltype” back and forth but it doesn’t work. I don’t see any difference in the value types of the consumption values compared to the weather values. Calculation also works. There is just some weird issue with publishing.

Any ideas how to solve the issue?

Thanks, Max

There’s a bit about formatting at the bottom of this post

example

var message = "Temperature is " + (tempItemC.state as QuantityType<Temperature>).format("%.1f%unit%")

“as Number” will not work because it comes with the units string which cannot be processed by decimal formatters like “%.2f”
“as QuantityType” gives you a units-friendly formatter.

Thank you for your advise, @rossko57.
My temperature and wind speeds come without units. I defined the items as pure Numbers:

Number                            AT_Dachflache_Temperature               "Dach Temperatur [%.1f °C]"                            <temperature>         (AT_Dachflache, gTemperature, g_Chart_Temperatures)    ["Temperature"]                     {channel="knx:device:bridge:generic:Wetterstation_Aussentemperatur"}
Number                            AT_Dachflache_Wind                      "Dach Wind [%.1f m/s]"                                 <wind>                (AT_Dachflache, gmeter)                                                              ["Wind"]                            {channel="knx:device:bridge:generic:Wetterstation_Wind"}

When I change the rule to

    var ReportWindAvg1 = AT_Dachflache_Wind.averageSince(Rx, "jdbc")
    var Number ReportWindAvg = ReportWindAvg1 as QuantityType<Speed>
   
    var ReportTempMax1 = AT_Dachflache_Temperature.maximumSince(Rx, "jdbc")
    var ReportTempMax = ReportTempMax1.state as QuantityType<Temperature>

then VS Code says:

Cannot cast from DecimalType to QuantityType<Speed>
Cannot cast from DecimalType to QuantityType<Temperature>

When I send just the value of the variable to the e-mail output everything works fine. But as soon as I try to format, I get the error. I guess I make a mistake in my formatting.

String.format("%.1f", ReportTempMax)

is apparently wrong.

Okeydoke.

Something cannot be formatted as a number, you’ll have to pry more closely to find out what, using logging. We can’t see the bit of rule that is failing.

Ok. I am logging every important step of the rule. It seems to stop right at the point where the temperature and wind values are inserted into the mail string.
When I remove the

and replace it with just the variable

ReportTempMax

the rule is executed fully and correctly.

Here’s the log again with the calculation and the error:

2022-06-06 15:06:12.682 [INFO ] [hab.core.model.script.Log Mailreport] - WindMax Woche: JdbcItem [name=AT_Dachflache_Wind, state=28.44, timestamp=2022-06-02T14:09:52.597+02:00[Europe/Berlin]]
2022-06-06 15:06:12.686 [INFO ] [hab.core.model.script.Log Mailreport] - WindMax Woche State: 28.44
2022-06-06 15:06:13.342 [INFO ] [hab.core.model.script.Log Mailreport] - WindAvg Woche: 3.123895134057001
2022-06-06 15:06:13.345 [INFO ] [hab.core.model.script.Log Mailreport] - WindAvg Woche State: 3.123895134057001
2022-06-06 15:06:13.433 [INFO ] [hab.core.model.script.Log Mailreport] - TempMax Woche: JdbcItem [name=AT_Dachflache_Temperature, state=32.9, timestamp=2022-06-05T15:49:02.288+02:00[Europe/Berlin]]
2022-06-06 15:06:13.436 [INFO ] [hab.core.model.script.Log Mailreport] - WindAvg Woche: 32.9
2022-06-06 15:06:13.535 [INFO ] [hab.core.model.script.Log Mailreport] - TempLow Woche: JdbcItem [name=AT_Dachflache_Temperature, state=6.7, timestamp=2022-06-03T04:18:12.971+02:00[Europe/Berlin]]
2022-06-06 15:06:13.538 [INFO ] [hab.core.model.script.Log Mailreport] - TempLow State: 6.7
2022-06-06 15:06:13.758 [INFO ] [hab.core.model.script.Log Mailreport] - TempAvg Woche: 17.64534159064354
2022-06-06 15:06:13.761 [INFO ] [hab.core.model.script.Log Mailreport] - TempAvg State: 17.64534159064354
2022-06-06 15:06:13.764 [INFO ] [hab.core.model.script.Log Mailreport] - Calculations completed successfully.
2022-06-06 15:06:13.767 [INFO ] [hab.core.model.script.Log Mailreport] - Mail creation started: Setting up StringBuilder.
2022-06-06 15:06:13.815 [INFO ] [hab.core.model.script.Log Mailreport] - Mail Creation: Consumption table completed.
2022-06-06 15:06:13.820 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'mail-1' failed: f != org.openhab.core.library.types.DecimalType in mail

And here are the parts of the long report rule for the weather data:

rule "Power Report"

when
//    Time cron "0 0 0 ? * SUN" or
    //Time cron "0 * * * * ?" //for debug
    Item Mailversand changed to ON //switch for manually triggering email creation

then

    var Rx = now.minusDays(7)
    var ReportWindMax1 = AT_Dachflache_Wind.maximumSince(Rx, "jdbc")
    logInfo ("Log Mailreport", "WindMax Woche: " + ReportWindMax1.toString)
    var Number ReportWindMax = (ReportWindMax1.state as Number)
    logInfo ("Log Mailreport", "WindMax Woche State: " + ReportWindMax.toString)
    
    var ReportWindAvg1 = AT_Dachflache_Wind.averageSince(Rx, "jdbc")
        logInfo ("Log Mailreport", "WindAvg Woche: " + ReportWindAvg1.toString)
    var Number ReportWindAvg = (ReportWindAvg1 as Number)
         logInfo ("Log Mailreport", "WindAvg Woche State: " + ReportWindAvg.toString)
   
    var ReportTempMax1 = AT_Dachflache_Temperature.maximumSince(Rx, "jdbc")
        logInfo ("Log Mailreport", "TempMax Woche: " + ReportTempMax1.toString)
    var Number ReportTempMax = (ReportTempMax1.state as Number)
        logInfo ("Log Mailreport", "WindAvg Woche: " + ReportTempMax.toString)

    var ReportTempLow1 = AT_Dachflache_Temperature.minimumSince(Rx, "jdbc")
        logInfo ("Log Mailreport", "TempLow Woche: " + ReportTempLow1.toString)
    var Number ReportTempLow = (ReportTempLow1.state as Number)
        logInfo ("Log Mailreport", "TempLow State: " + ReportTempLow.toString)

    var ReportTempAvg1 = AT_Dachflache_Temperature.averageSince(Rx, "jdbc")
        logInfo ("Log Mailreport", "TempAvg Woche: " + ReportTempAvg1.toString)
    var Number ReportTempAvg = (ReportTempAvg1 as Number)
        logInfo ("Log Mailreport", "TempAvg State: " + ReportTempAvg.toString)

        logInfo ("Log Mailreport", "Calculations completed successfully.")


    val mailActions = getActions("mail","mail:smtp:openhab")
    val subject ="Wöchentlicher OpenHAB Report"
    val message = new StringBuilder()
        logInfo ("Log Mailreport", "Mail creation started: Setting up StringBuilder.")
    

    message.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">" )
    message.append("<head>")
    message.append("<title>Wöchentlicher Haus-Report</title>")
    message.append("<style type=\"text/css\">
    body{background-color: grey; color: white; font-family: Lucida Sans,Lucida Grande,Lucida Sans Unicode,sans-serif; margin:0px;width:100%;}
    h1{display: block; font-family: Lucida Sans,Lucida Grande,Lucida Sans Unicode,sans-serif; font-size: 1.5em; font-weight: bold;}
    h2{font-size:1.1em; font-family: Lucida Sans,Lucida Grande,Lucida Sans Unicode,sans-serif; font-weight: bold; font-style:italic; margin-left: 0px; margin-bottom: 0px;}
    h3{font-size:0.9em; font-family: Lucida Sans,Lucida Grande,Lucida Sans Unicode,sans-serif; font-style:italic; margin-left: 0px; margin-bottom: 0px;}                    </style>")
    message.append("</head>")
    message.append("<body>")
    message.append("<h1>Wöchentlicher Hausbericht</h1>")

// I removed the long table for consumption values from the rule.

    message.append("Gasverbrauch in der Vorwoche: " + String.format("%.2f", GasMeterVorwoche) +" m³")

    logInfo ("Log Mailreport", "Mail Creation: Consumption table completed.")

    message.append("Wind Max: " + ReportWindMax +" m/s.")

message.append("<div>
            <h2>Wetterdaten</h2>
            <h3>Temperatur</h3>
            Die Höchsttemperatur lag in der vergangenen Woche <B>"+ String.format("%.1f", ReportTempMax) +"°C.</B>
            Die Tiefsttemperatur betrug <B>%1.f" + String.format ("%1.f", ReportTempLow) +"°C.</B> und die durchschnittliche Temperatur war "+ String.format ("%1.f",ReportTempAvg) +"°C.</B>\n
            <h3>Wind</h3>
            Die höchste Windgeschwindigkeit betrug %1.f<B>"+ String.format ("%1.f",ReportWindMax) + "m/s.</B>. Im Durchschnitt blies der Wind mit "+ String.format ("%1.f",ReportWindAvg) +"m/s.</B>\n
        </div>")
    

    logInfo ("Log Mailreport", "Mail creation. Weather report completed.")


    message.append("</body>")
    message.append("</html>")

    logInfo ("Log Mailreport", "Mail creation completed successfully.")

    logInfo ("Log Mailreport", "Mail creation: Starting sending.")

    mailActions.sendHtmlMail("XXX@yyy.com", subject, message.toString)

    logInfo ("Log Mailreport", "Mail creation completed. Rule finished.")

end

I already tried to dig in deep but I can’t find the source of error. The string formatting works for the consumption values (not pasted in the rule here). I wonder what the difference with the weather data is. In the log they all look alike.

Best, Max

this does not look like

Omg, what a stupid mistake. Thanks for the advise.

However, the fix leads to a new error message:

2022-06-07 14:41:14.765 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'mail-1' failed: Conversion = '1' in mail

Alright, it’s not a very helpful message, so you’ll have to look closely at your logs, and perhaps add more temporary logging, to find out the point at which your rule errors out.

The word “conversion” suggests you might still have something about QuantityType where it is not wanted, working on a plain Number.

Thank you for the explanation @rossko57. I will have a look into that.
As always, I really appreciate your patience and kind help here on the forum. Thank you very much for that.