Compare times in rule

Hi openHAB-community,

I am running OH3.1 on a synology docker and i do have troubles with rules and comparing times.
The rule code (DSL type) is seemingly simple and as follows:

if(LokaleZeit_String > AstroSonnendaten_Set_Start) {
  AlleRollaeden.sendCommand(100)
}

So therefore i have installed the NTP-binding (local time) and the Astro Binding to get the time of sunset. Rule should say (excerpt):
If the sunset has already started, shut down all rollers.

Please see attached item information.
Maybe it’s because the item state/data is formatted differently?


You do not need to compare the times by yourself. If you already have the astro binding installed you can use it’s functions to trigger actions.

Something like:

rule "rule triggered by sun set ( to turn on lights on ouside on sunset )"
when
    Channel 'astro:sun:home:set#event' triggered START
then
 ...

There are different events that can be used see

The link jumps into the trigger events in the docs

Unfortunately, the Astro Event is not the trigger. The trigger is something else. The rule is like:
WHEN event happens >> shut down all rollershutters>> but only if current time is later than sunset start
(= it is dark outside)

triggers:
  - id: "1"
    configuration:
      itemName: LupusAlarmZentrale_LupusArea1Status
      state: "1"
    type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: |-
        AlleLichter.sendCommand(OFF)
          
        if(LokaleZeit_String > AstroSonnendaten_Set_Start) {
          AlleRollaeden.sendCommand(100)
        }
    type: script.ScriptAction

You could use an item that represents true/false or ON/OFF triggered by the astro binding and use this state to evaluate if it is dark outside.
Have a look at this thread ( How to get and use Astro sunrise and sunset via OH3 Javascript ) as far as I understand that should help.

Take care about Item types. That one is a String type, you will not be able to compare a collection of alphanumeric characters directly with a DateTime type representing a moment in time. At some point you need to convert stuff to the same type to do meaningful comparisons.

Also bear in mind an Item is a complex object, with icon, label, etc. properties. In a rule you are almost always interested in its state property.

1 Like

Great input from both of you guys! Thanks for that :slightly_smiling_face:
I took Wolfgang_S’ idea to create an item “Dunkelheit” (darkness) which is on state “1” once the sunset event has been triggered (and set to state “0” once sunrise fires).

I have not tested the script yet, but hopefully it will do it’s job