I'm not a good fly-fisherman, how to cast?

Hi Community, thank you for your tireless support.

OH3.1 on ubuntu20.04 using a combination of text file configuration and PaperUI.

I have a file based item, which I’m trying to update and read from in order to set light temperature. I’ve read over the forums, seems the right way to grab the number is via a particular command, but I’m still getting an error.

The item receives updates OK, but when I try to take the value and throw it into a variable in a rule, I get an error. In short here’s the line I’m trying to use:

var Number LT = (Colour_Temperature.state as QuantityType<number>).doubleValue
			logInfo ("Study", LT)

But I get the error:

2021-06-08 23:51:09.981 [INFO ] [org.openhab.core.model.script.Study ] - Study Lamp Day Motion Detected
2021-06-08 23:51:09.982 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'MotionStudySpotLight-1' failed: Could not cast 95 to org.openhab.core.library.types.QuantityType; line 11, column 21, length 48 in MotionStudySpotLight

Is it because I’m using the wrong command or casting incorrectly?


item definition for light temperature:
Number Colour_Temperature "Colour Temperature [%.2d]" <light> [ "CurrentLightTemperature" ]

Rule that updates it with the temperature based on the time of day:

rule "light_colour_temp"
when
    // 	every 10 seconds for debugging purposes
//	Time cron "0/10 * * ? * * *"
    // 	apply every two minutes when actually running - that should mean it rarely/never changes
    // 	by more than a 1% increment
	Time cron "0 0/2 * ? * * *"
    // 	Item HueMotionsensor2_motion received update or
then
    logInfo("light_colour_temp", "Sunset time: {}", Sunset_Time.state)
    val sunrise = (Sunrise_Time.state as DateTimeType).getZonedDateTime
    val sunset = (Sunset_Time.state as DateTimeType).getZonedDateTime 
    val preSunrise = sunrise.minusHours(3)
    val preSunset = sunset.minusHours(3)
    val postSunset = sunset.plusHours(0)
    logInfo("light_colour_temp", "Times: {}, {}, {}", preSunrise, preSunset, postSunset)
    val now = ZonedDateTime.now()
    val int CT_COOL = 20
    val int CT_WARM = 95
    var float colourTemp = 0

    if (now.isBefore(preSunrise)) {
        logInfo("light_colour_temp", "early hours: WARM")
        colourTemp = CT_WARM
    } else if (now.isBefore(sunrise)) {
        logInfo("light_colour_temp", "dawn: CHANGING TO COOL")
        val long period = Duration.between(preSunrise, sunrise).getSeconds
        val long progress = Duration.between(preSunrise, now).getSeconds
        val float factor = progress.floatValue / period.floatValue
        logInfo("light_colour_temp", "period: {}, progress: {}, factor: {}", period, progress, factor)
        colourTemp = ((1-factor) * (CT_WARM-CT_COOL) + CT_COOL).intValue
    } else if (now.isBefore(preSunset)) {
        logInfo("light_colour_temp", "daytime: COOL")
        colourTemp = CT_COOL
    } else if (now.isBefore(postSunset)) {
        logInfo("light_colour_temp", "evening: CHANGING TO WARM")
        val long period = Duration.between(preSunset, postSunset).getSeconds
        var long progress = Duration.between(preSunset, now).getSeconds
        val float factor = progress.floatValue / period.floatValue
        logInfo("light_colour_temp", "period: {}, progress: {}, factor: {}", period, progress, factor)
        colourTemp = (factor * (CT_WARM-CT_COOL) + CT_COOL).intValue
    } else {
        logInfo("light_colour_temp", "late evening: WARM")
        colourTemp = CT_WARM
    }

    logInfo("light_colour_temp", "setting CT to {}% ", colourTemp)
    logDebug("light_colour_temp", "updating {} to {}", Colour_Temperature, colourTemp)
    sendCommand(Colour_Temperature, colourTemp)
end

Output in the log on updating colour temperature (showing the rule works):

2021-06-08 23:48:30.322 [INFO ] [.core.model.script.light_colour_temp] - Sunset time: 2021-06-08T16:58:00.000+1000
2021-06-08 23:48:30.324 [INFO ] [.core.model.script.light_colour_temp] - Times: 2021-06-08T04:08+10:00, 2021-06-08T13:58+10:00, 2021-06-08T16:58+10:00
2021-06-08 23:48:30.325 [INFO ] [.core.model.script.light_colour_temp] - late evening: WARM
2021-06-08 23:48:30.326 [INFO ] [.core.model.script.light_colour_temp] - setting CT to 95% 
2021-06-08 23:48:38.126 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'LightTemperature.rules'

Rule that is meant to take the colour temperature and throw it into the light:

var Timer motionTimerStudy2 = null

rule "Study Motionsensor Spot Light"

when
    Item CygnettMotionSensor1_MotionIntrusion changed from OFF to ON
then
	if (motionTimerStudy2 === null || motionTimerStudy2.hasTerminated()) {
    		if (now.getHour <= 24 && now.getHour > 5) {
			logInfo("Study", "Study Lamp Day Motion Detected")
			var Number LT = (Colour_Temperature.state as QuantityType<number>).doubleValue
			logInfo ("Study", LT)
			Studyspotlight_Brightness.sendCommand("100")
			Studyspotlight_Color_Temperature.sendCommand(LT)
			motionTimerStudy2 = createTimer(now.plusMinutes(1), [|
				Studyspotlight.sendCommand("OFF")
				CygnettMotionSensor1_MotionIntrusion.sendCommand("OFF")
				motionTimerStudy2 = null
    			])
    		} 
		else {
			logInfo("Study", "Study Lamp Night Motion Detected")
			callScript("StudyNightLight")
			NightLightOn.sendCommand("ON")
			motionTimerStudy2 = createTimer(now.plusMinutes(1), [|
				NightLightOn.sendCommand("OFF")
				CygnettMotionSensor1_MotionIntrusion.sendCommand("OFF")
				Studyspotlight.sendCommand("OFF")
				motionTimerStudy2 = null
    			])
    		}
    	} 
    	else {
		logInfo("Study", "Study Lamp Motion Timer Reset")
		motionTimerStudy2.reschedule(now.plusMinutes(1))
    	}
end

Error message when motion is detected:

2021-06-08 23:51:09.981 [INFO ] [org.openhab.core.model.script.Study ] - Study Lamp Day Motion Detected
2021-06-08 23:51:09.982 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'MotionStudySpotLight-1' failed: Could not cast 95 to org.openhab.core.library.types.QuantityType; line 11, column 21, length 48 in MotionStudySpotLight

Get more precise, please, Stable 3.1.0 has not yet been released. Ideally, before asking for help on testing code you should be running the latest Milestone (currently M5) or a very recent snapshot.

Your Number Item is just a Number type Item, you cannot treat it as though it is a proper Quantity type.
Note that it is not an error to stuff a quantity with units, like 95%, into a plain Number type, but Quantity methods will not work on it.

What does your Item state really get set to, as shown in your events.log? It’s just a plain number isn’t it?

Yes @rossko57, it’s just a plain number. the paperUI shows 95. I wonder if casting it to a string would make a difference?

Apologies - 3.1.0 milestone 5 running in Zulu 11.0.8 (Zulu11.41+23-CA) on Linux/5.4.0-73-generic (amd64), desktop computer with 16GB RAM and 1TB HDD. The ‘server’ never goes below 11GB free memory, HDD has about 250GB free space. Also running SubSonic and Wordpress + PHP (a few versions of PHP) + MariaDB + Apache2.0.

1 Like

It’s OH3, there is no PaperUI, they call the new thing MainUI.

When fault finding, do not entirely trust what any UI says when it may be “helpfully” reformatting stuff for display. There was a reason I suggested looking in your events.log for unvarnished truth.
However, it seems probable this Number Item state does indeed carry no units.

What? No.

Don’t do that, it’s a plain Number and not a Quantity.
It’s telling you that is the problem -

Just treat as a Number

var Number LT = Colour_Temperature.state as Number

You only need to mess with doubleValue in specific circumstances that do not I think arise here, like passing to some function that demands a double.

Excellent, progress! Different error now:

Script execution of rule with UID 'MotionStudySpotLight-1' failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.model.script.actions.Log.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null in MotionStudySpotLight

However, that’s because I’m trying to log the variable. One minute, I’ll fix it.

SUCCESS!! MWAAAHAHAAA. Aaah the sweet apple of a small bit of knowledge. Watch out world, I’m dangerous now.

1 Like