[SOLVED] Multiple 'when' statements, (AND/OR)

OK, I cleaned up the persist file. I didn’t have temperature and humidity group. I rebooted and didn’t see that long error message. I did see the “Error during the execution of startup rule ‘Get time period for right now’: Cannot cast org.openhab.core.types.UnDefType to org.openhab.core.library.types.DateTimeType”. I rebooted again and that error is not coming up anymore. So it looks like I’m error free. My sitemap comes up with the “Day” light on. There is one problem though. I have a line on my sitemap for “Sunrise/Sunset Time”. It should be showing something like this 7:56 / 17:28. I is showing 20:55 / 06:21. Do you see anything that could be messing that up?

Edit: I rebooted again to double check and that UnDefType error came up again.

Do you have multiple rules and items files? If yes it is possible that your rules file is loading and executing before all your .items files or persistence has finished loading. That would explain the intermittent appearance of the error. Change the polling times in your openhab.cfg so your rules get loaded last at startup. I use these values:

folder:items=20,items
folder:sitemaps=26,sitemap
folder:rules=30,rules
folder:scripts=24,script
folder:persistence=22,persist

Change your Sunrise_Sunset.postUpdate line to:

Sunrise_Sunset.postUpdate(String::format("%0$tr / %1$tr", sunrise, sunset)

When doing String::format, the “%x” part is a reference to which argument you are referencing and the $tr means the argument is a DateTime object and to format it as HH:MM am/pm. If you want the time in 24 hour format use $tR instead. The full documentation for format is here.

I entered the replacement line and Openhab designer had a problem with it. I needed to add a extra parentheses at the very end of it. I hope thats where it went.
After I entered in the replacement line my sun.rules did a refresh and I got the following error:
Error during the execution of rule ‘Sunrise/Sunset changed’: r != org.joda.time.DateTime
I

Yes, that is where it goes.

Oh wait… try the following:

Sunrise_Sunset.postUpdate(String::format("%0$tr / %1$tr", (Sunrise_Time.state as DateTimeType), (Sunset_Time.state as DateTimeType)))

When I enter that line, the two lines directly above it get errors. I think you have to use the words “sunrise” and “sunset” somewhere in the line. I noticed that I am using the words sunrise and sunset in two different rules. Can that cause some type of conflict. rule “Sunrise/Sunset changed” and rule “Get time period for right now”

Errors (i.e. red) or just a warning that they are not used anywhere (i.e. yellow). The warnings are not a problem. You can ignore them and once it works delete those two lines.

The line that you use to populate Sunrise_Sunset has to be the same in both places.

We don’t want to use sunrise and sunset because they are the wrong type to work with $tr.

But, are you using both the Astro binding and the Astro action? Others have reported that they are not compatible. You can only use one or the other.

I have both. I read somewhere I needed it to be able to run it at startup. Is that true?
They were warnings that they were not used anymore. I get this error message when I change the one line:
Error during the execution of rule ‘Sunrise/Sunset changed’: r != org.openhab.core.library.types.DateTimeType

I’m still getting those strange sunrise/sunset times on my sitemap. Here are the polling times I have:
folder:items=20,items
folder:sitemaps=26,sitemap
folder:rules=40,rules
folder:scripts=24,script
folder:persistence=22,persist

There are two separate errors. One is the UnDef error which the polling times addresses. The other is the times on the sitemap.

You should uninstall one or the other. I recommend unibstalling the action and just use the binding. I have know idea what “run it at startup means”.

Please post you items, rules, and sitemap as they are now again.

OK. I removed the astro action binding. I removed the 2 lines in the Rule “Sunrise/Sunset changed” and added your line. I also changed the Items DateTime Sunrise_Time and DateTime Sunset_Time. Do they look right? rebooted and there were no errors so far. The sunrise/sunset on the sitemap is still messed up, but all my rules based on sunrise and sunset still appear to be running on time. Here are the items, rules and sitemap. My Openhab version is now 1.8. I noticed some of my bindings are 1.7. Should they all be updated to 1.8 versions? db4o binding is 1.7, Astro is 1.8

Switch   Day
Switch   Night
Switch Sunrise_Event   {astro="planet=sun, type=rise, property=start, offset=30"} 
Switch Sunset_Event    {astro="planet=sun, type=set, property=end, offset=-30"}
String Sunrise_Sunset  "Sunrise / Sunset [%s]"    <sun>
DateTime Sunrise_Time  "Sunrise [%0$tr]"    <sun>    {astro="planet=sun, type=rise, property=start"}
DateTime Sunset_Time   "Sunset [%1$tr]"     <sun>    {astro="planet=sun, type=set, property=end"}


import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*
import org.java.math.*

////////////////////////////////////////////////////////////////////////////
// Rule:    Initialize system on startup / rule update
// Purpose: Initialize the system
rule "Initialize system on startup / rule update"
when
    System started
then
    var DateTimeType sunrise = new DateTimeType(getAstroSunriseStart(now.toDate, 42.5803, 83.0303))
    var DateTimeType sunset = new DateTimeType(getAstroSunsetStart(now.toDate, 42.5803, 83.0303))
    Sunrise_Time.postUpdate(sunrise)
    Sunset_Time.postUpdate(sunset)
end

////////////////////////////////////////////////////////////////////////////
// Rule:    Sunrise/Sunset changed
// Purpose: Update Sunrise_Sunset string with the updated sunrise and sunset
//          times. The Sunrise_Sunset string is used to display both the
//          sunrise time and sunset time on a single line in the GUI
rule "Sunrise/Sunset changed"
when
    Item Sunrise_Time changed
    or Item Sunset_Time changed
then
    Sunrise_Sunset.postUpdate(String::format("%0$tr / %1$tr", (Sunrise_Time.state as DateTimeType),       
(Sunset_Time.state as DateTimeType)))
end

rule "Day"
when
    Item Sunrise_Event received update
then
    Night.sendCommand(OFF)
    Day.sendCommand(ON)
end

rule "Night"
when
Item Sunset_Event received update
then
    Day.sendCommand(OFF)
    Night.sendCommand(ON)
end

rule "Get time period for right now"
when
    System started
then
    val sunrise = new DateTime((Sunrise_Time.state as DateTimeType).calendar.timeInMillis)
    val sunset = new DateTime((Sunset_Time.state as DateTimeType).calendar.timeInMillis)
    if(now.isAfter(sunrise) && now.isBefore(sunset)) {
        Day.sendCommand(ON)
        Night.sendCommand(OFF)
    }
    else if(now.isAfter(sunset) && now.isBefore(sunrise)) {
        Day.sendCommand(OFF)
        Night.sendCommand(ON) 
    }
end 

Frame label="Date, Time and Weather" {
  Text item=Sunrise_Sunset
  Text item=Date icon="calendar"
  Text item=Temperature icon="temperature"
  Text item=Humidity icon="climate"
  Switch item=Night	
  Switch item=Day
  }
}

Follow up:
I noticed is that my db4o persistence directory is not filling up with item states like my rr4dj persistence use to do. I see the db4o persistence loading at bootup. I changed my Astro binding back to 1.7 to see if that would fix anything. It didn’t. Received this error message this morning(Thur, 1/21) : [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule ‘Sunrise/Sunset changed’: r != org.openhab.core.library.types.DateTimeType .

Rich are you still around. I provided you with the information you requested. Do you have any ideas how I can get this working properly?

I’m around but life sometimes makes it so I am away from the forum for extended periods.

Without the Astro action and with persistence you no longer need the Systems started rule. In fact, the rule as written will not work because there is no longer access to getAstroSunriseStart and getAstroSunsetStart.

However, you may still want a System started rule to populate Sunrise_Sunset.

You may need to experiment around a bit with the String::format some. If it still doesn’t like the DateTimeTypes you might need to go back to your original approach where you get the hours and minutes yourself. However, be aware that the %0 and %1 part is referring to the arguments that you are passing to format after the format string. I don’t have a lot of experience with format. The problem might be that we need to use %1 and %2 instead of %0 and %1.

True!

Maybe this will work instead:

String::format("%1$tr / %2$tr", (Sunrise_Time.state as DateTimeType).calendar, (Sunset_Time.state as DateTimeType).calendar)

I removed the Systems started rule relating to the getAstro commands. I tried using %1 and %2 and tried watou’s String::format line. None of that worked. My sitemap still displays the Sunrise/Sunset time as 20:55/6:21, when it should be 7:53/5:33 . My rules relating to turning off and on lighting at sunrise and sunset appear to be working correctly. I give up. I’m just going to take it off the sitemap. I really appreciate the help that both of you provided.