[Solved] Problems converting simple DSL rule to jython

hi guys…

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 :wink:

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…

thanks,

Dan

You will also need the “state.toString()”.
e.g. like this
ir.getItem("Sun_Elevation").state.toString()
or
event.itemState.toString()

Thanks Ulrich!

I will try that :slight_smile:

I have additional questions

do I have to replace the

if items["Sun_Elevation"] <= "0":

with

if ir.getItem("Sun_Elevation").state.toString()  <= "0":

and what if the item type is decimal or number, do I need to convert to string anyway or just remove the quotes around the state?? e.g.

if items["Sun_Elevation"].state > 0:

sorry but thats not clear to me…

https://openhab-scripters.github.io/openhab-helper-libraries/Guides/But%20How%20Do%20I.html#convert-a-value-to-a-state-for-comparison

thanks this link looks very helpful hafniumzinc

I will have a look at it…

Two things to be aware of:

  1. 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

  2. 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:
1 Like

thanks for this clarification JustinG :wink:
this makes it a lot easier to understand for me

Ok I think it should work now…

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

I changed the logging and now it works

here is the final result

from core.rules import rule
from core.triggers import when
from core.actions import LogAction

@rule("Script - Monitor Sun Elevation for changes")
@when("Item Sun_Elevation changed")
@when("System started")
def monitor_sun_elevation(event):
    LogAction.logInfo("Sun Elevation", "Sun Elevation changed", 5 + 5)
    if items["Sun_Elevation"].floatValue() <= 0:
        if items["SunState"].toString() != "SUN DOWN":
            events.postUpdate("SunState", "SUN DOWN")
            LogAction.logInfo("Sun Elevation", "SUN IS DOWN", 5 + 5)
    if items["Sun_Elevation"].floatValue() > 0:
        if items["SunState"].toString() != "SUN UP":            
            events.postUpdate("SunState", "SUN UP")
            LogAction.logInfo("Sun Elevation", "SUN IS UP", 5 + 5)

thanks for all your helping hands!! :slight_smile:

p.s. can someone explain what the 5 + 5 stands for, is this necessary??