Successful Use Of Ephemeris

I wrote a javascript rule several months ago that included my first use of Ephemeis. I wanted a rule that would turn on a virtual switch, Christmas Season, on the first Advent Sunday. I was struggling with the use of Ephemeris but after a lot os searching around, and writing scripts to test possible solutions, I came up with a rule that I thought would work. I just had to wait for the first Advent Sunday. Well, today is the day and it worked, Christmas season is on, and that action in turn, activated other Christmas rules. Anyway here is my rule to turn on the Christmas season switch. It’s time activated through a cron expression.

var Ephemeris = Java.type("org.openhab.core.model.script.actions.Ephemeris");
if(Ephemeris.getBankHolidayName(0, "/etc/openhab/services/Holidays_de.xml") == "FIRST_ADVENT") {
  items.getItem("Christmas_Season").sendCommand("ON")
}

Thank you for posting! It would be most helpful though if you posted the entire rule, not just the Script Action. How is this rule triggered for example? Click on the code tab from the rule’s page and paste the YAML you find there and we can see the whole rule in context.

Also, this appears to be Nashorn JS, correct? If it’s the newer JS Scripting add-on the following would be more standard.

if(actions.Ephemeris.getBankHoliday(0, '/etc/openhab/services/Holidays_de.xml') == 'FIRST_ADVENT') {
  items.Christmas_Season.sendCommand('ON');
}

There’s rarely a need to use Java.type in a JS Scripting rule.

If you split this up and move the test to the condition the rule could look something like this:

configuration: {}
triggers:
  - id: "1"
    configuration:
      time: 00:01
    type: timer.TimeOfDayTrigger
conditions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/javascript
      script: actions.Ephemeris.getBankHoliday(0,
        '/etc/openhab/services/Holidays_de.xml') == 'FIRST_ADVENT'
    type: script.ScriptCondition
actions:
  - id: "3"
    configuration:
      itemName: TisTheSeason
      command: ON
    type: core.ItemCommandAction

Finally, please post your xml file. There are not a lot of examples of custom Ephemeris holiday xml files on the forum and a lot of people struggle with them.

Thanks Rich, I’ll update my code. The script is using the JS Scripting 2022 addon.

Here’s the entire rule (incorporating the JS Scripting suggestion):

configuration: {}
triggers:
  - id: "1"
    configuration:
      time: 00:05
    type: timer.TimeOfDayTrigger
conditions: []
actions:
  - inputs: {}
    id: "3"
    configuration:
      type: application/javascript;version=ECMAScript-2021
      script: >-
        var Ephemeris =
        Java.type("org.openhab.core.model.script.actions.Ephemeris");

        if(actions.Ephemeris.getBankHoliday(0, "/etc/openhab/services/Holidays_de.xml") == "FIRST_ADVENT") {
          items.getItem("Christmas_Season").sendCommand("ON")
        }
    type: script.ScriptAction

And here is my xml file:
Holidays_de.xml (8.5 KB)

1 Like

You don’t need this line. The JS Scripting helper library does all this stuff for you. That’s what I meant by “more standard” above. You should almost never have to have a line that starts with Java.type in a JS Scripting 2022 rule.

Doh! I must have gotten my copy and paste order messed up. Here’s my current version of the rule:

configuration: {}
triggers:
  - id: "1"
    configuration:
      time: 00:05
    type: timer.TimeOfDayTrigger
conditions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/javascript
      script: actions.Ephemeris.getBankHoliday(0,
        '/etc/openhab/services/Holidays_de.xml') == 'FIRST_ADVENT'
    type: script.ScriptCondition
actions:
  - inputs: {}
    id: "4"
    configuration:
      itemName: Christmas_Season
      command: ON
    type: core.ItemCommandAction

1 Like

I wanted to test the script condition ability within a rule so I created a test rule that sends a notification when a switch changes to on, but only if the above mentioned, First_Advent script evaluates to true. The rule threw an error. The reason is that getBankHoliday should be getBankHolidaName.
I wanted to set the record straight so that anyone finding this thread will have a working example of Ephemeris uses. Here’s the entire rule, one more time:

configuration: {}
triggers:
  - id: "1"
    configuration:
      time: 00:05
    type: timer.TimeOfDayTrigger
conditions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/javascript
      script: actions.Ephemeris.getBankHolidayName(0,
        '/etc/openhab/services/Holidays_de.xml') == 'FIRST_ADVENT'
    type: script.ScriptCondition
actions:
  - inputs: {}
    id: "4"
    configuration:
      itemName: Christmas_Season
      command: ON
    type: core.ItemCommandAction
1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.