QuantityType<Number> into val within a Rule

  • Platform information:
    • Hardware: Docker on x64
    • OS: Linux/5.4.143-1-pve (amd64)
    • Java Runtime Environment: 11.0.10 (Zulu11.45+27-CA)
    • openHAB version: openHAB 3.0.2
  • Issue of the topic:
    I am trying to load the value of the item “solar_powerload” in a rule into a variable for further processing. The goal is that later I would like to control a color of a RGB light depending on the current power of the solar panel.
    However, I already fail to get the state value into my variable. After many attempts I get with “QuantityType” already the stat value (in the example 14) in the error message …

Does anyone have the tipp for me how I get this value simply as a variable in my rule ? i would estimate i have some fundamental understanding issues … i spend a lot of time to research and try and error.

  • Configurations :
    • things configuration related to the issue
  Thing mqtt:topic:rhasspy "MQTT" (mqtt:broker:rhasspy) {
Channels:
Type number : solar_powerload "Solarmodul Aktueller Verbrauch"             [ stateTopic="tele/tasmota_solarmodul/SENSOR", transformationPattern="JSONPATH:$.ENERGY.Power", unit="W"]
}
  • Items configuration related to the issue
Number solar_power            "solar_power solar"                        {channel="mqtt:topic:rhasspy:solar_powerload"}

Value of the item is displayed as “14 W” in the UI, at the the time of this Log. IT get successfully updated.

  • Rules code related to the issue
val String filename = "solarstatus.rules"

rule "Solar Status an LED Basis"
when
    Time cron "* 5 * * * *"
then
        var Number power = (solar_power.state as QuantityType<Number>)
        logInfo(power) 
        // further script 
end
  • If logs where generated please post these here using code fences:
2021-12-10 14:30:57.566 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'solarstatus-1' failed: Could not cast 14 to org.openhab.core.library.types.QuantityType; line 9, column 22, length 41 in solarstatus

best,

hagi

var Number power = (solar_power.state as Number).floatValue

or intValue or doubleValue

hi … a tried this allready.

Log:

2021-12-10 15:15:50.973 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'solarstatus-1' failed: An error occurred during the script execution: index=1, size=1 in solarstatus

That error is because your logInfo() is incorrectly formed - at least two arguments needed. And they need to be strings.

Thx … now it works as expected :wink:

Thx Guys !