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