Hi Folks,
I have been quietly bashing away at migrating from OH2 to OH3 (currently on RC1) and trying not to raise daft questions as I know everybody is busy beavering away trying to get OH3 ready for release, I have however ran into a few rule rule problems so wondering if anybody can help?
I’m essentially moving from OH2 text based things/items/rules to OH3 UI based syntax to try and adapt to this new way (been slow going) and getting there gradually, for rules I’m moving (as mentioned) from test based to using the UI and using dsl rules so that I can keep the main “rule action” syntax as is (assuming this is an ok approach).
First is a simple rule that uses “Date()” syntax but even searching the forum cant get my head round how to make it work:
OH2 text based rule looks like:
rule "Generate Time of Day Events"
when
System started or
Item CurrentTime received update
then
val now = new Date()
val sunset = new Date((SunsetStart_Time.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)
val sunrise = new Date((SunriseStart_Time.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)
if((now.getTime-(now.getTime%60000)) == (sunrise.getTime-(sunrise.getTime%60000))) {
//logInfo("time-of-day.rules", "Start of Sunrise!")
SunriseStart_Event.postUpdate(ON)
}
else if((now.getTime-(now.getTime%60000)) == (sunset.getTime-(sunset.getTime%60000))) {
//logInfo("time-of-day.rules", "Start of Sunset!")
SunsetStart_Event.postUpdate(ON)
}
end
The new DSL rule looks like:
triggers:
- id: "1"
configuration:
startlevel: 20
type: core.SystemStartlevelTrigger
- id: "2"
configuration:
itemName: CurrentTime
type: core.ItemStateUpdateTrigger
conditions: []
actions:
- inputs: {}
id: "3"
configuration:
type: application/vnd.openhab.dsl.rule
script: >
val now = new Date()
val sunset = new Date((SunsetStart_Time.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)
val sunrise = new Date((SunriseStart_Time.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)
if((now.getTime-(now.getTime%60000)) == (sunrise.getTime-(sunrise.getTime%60000))) {
//logInfo("time-of-day.rules", "Start of Sunrise!")
SunriseStart_Event.postUpdate(ON)
}
else if((now.getTime-(now.getTime%60000)) == (sunset.getTime-(sunset.getTime%60000))) {
//logInfo("time-of-day.rules", "Start of Sunset!")
SunsetStart_Event.postUpdate(ON)
}
type: script.ScriptAction
The logged error when testing the rule is:
2020-12-17 12:31:16.207 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID '6ee051d55f' failed: val now = new ___ Date()
val sunset = new Date((SunsetStart_Tim ___ e.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)
val sunrise = new Date((SunriseStart_Time.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)
if((now.getTime-(now.getTime%60000)) == (sunrise.getTime-(sunrise.getTime%60000))) {
//logInfo("time-of-day.rules", "Start of Sunrise!")
SunriseStart_Event.postUpdate(ON)
}
else if((now.getTime-(now.getTime%60000)) == (sunset.getTime-(sunset.getTime%60000))) {
//logInfo("time-of-day.rules", "Start of Sunset!")
SunsetStart_Event.postUpdate(ON)
}
1. Date cannot be resolved.; line 1, column 14, length 4
2. Date cannot be resolved.; line 2, column 38, length 4
3. Date cannot be resolved.; line 3, column 140, length 4
The second rule issue is around the ookla/speedtest rule, has anyone got that working on OH3?, and I will paste the issue in a separate topic later.
Cheers