I’m trying to convert a simple rule but with no luck…
in DSL it looks like that and works just fine:
rule "Astro - Update SunState"
when
Item Sun_Elevation changed or
System started
then
logInfo("SunState", "SunState: "+Sun_Elevation.getStateAs(DecimalType)+" "+SunState.state.toString)
if ( Sun_Elevation.state.toString > "0" ) {
if ( SunState.state.toString != "SUN UP" ) {
SunState.postUpdate("SUN UP")
}
}
else if ( Sun_Elevation.state.toString <= "0" ) {
if ( SunState.state.toString != "SUN DOWN" ) {
SunState.postUpdate("SUN DOWN")
}
}
end
my conversion looks like this and does nothing
from core.rules import rule
from core.triggers import when
@rule("Script - Monitor Sun Elevation for changes")
@when("Item Sun_Elevation changed")
@when("System started")
def monitor_sun_elevation(event):
if items["Sun_Elevation"] <= "0":
if items["SunState"] != "SUN DOWN":
events.postUpdate("SunState", "SUN DOWN")
monitor_sun_elevation.log.info("SUN IS DOWN")
if items["Sun_Elevation"] > "0":
if items["SunState"] != "SUN UP":
events.postUpdate("SunState", "SUN UP")
monitor_sun_elevation.log.info("SUN IS UP")
any ideas on that?? I’m not really familar with the syntax so maybe there are errors…
second question: where should I look for a documentation for jython with openhab??
I only found these 2 documentations this one and this one which are very short and not very informative…
There two different methods of accessing an item state and they have slightly different syntax. items.item_name gets you the state of the item directly (you don’t have to include .state. ir.getItem('item_name') returns a more detailed object from the item registry with several properties including state (so you have to add the property you want, that is, .state
If you want to compare the item state to a numerical value then you don’t convert it to a string, you convert it to a numerical value:
if items["Sun_Elevation"].floatValue() > 0:
or
if ir.getItem("Sun_Elevation").state.floatValue() > 0:
from core.rules import rule
from core.triggers import when
@rule("Script - Monitor Sun Elevation for changes")
@when("Item Sun_Elevation changed")
@when("System started")
def monitor_sun_elevation(event):
monitor_sun_elevation.log.info("Sun_Elevation changed")
if items["Sun_Elevation"].floatValue() <= 0:
if items["SunState"].toString() != "SUN DOWN":
events.postUpdate("SunState", "SUN DOWN")
monitor_sun_elevation.log.info("SUN IS DOWN")
if items["Sun_Elevation"].floatValue() > 0:
if items["SunState"].toString() != "SUN UP":
events.postUpdate("SunState", "SUN UP")
monitor_sun_elevation.log.info("SUN IS UP")
but the logging seems to go nowhere… so I can’t see if the rule fires
I have to wait until sun gets down to confirm its working… any hints on the logging?
I think I copy&pasted this logging line from an example on this page