[SOLVED] Converting rules to JS: rule 'execute: (event) =>' gives syntax error

Switching to OH 3.3.0 (from 2.5) I also decided to switch from Python to JavaScript, given the absence of support for Python 3 in Jython.
For instance, using the example rule definition from the documentation, I received a syntax error on the require.
Disclaimer: I’m learning JavaScript along the way.

But first another simple example from my first converted rule:

const {getSunrise, getSunset} = require('sunrise-sunset-js');


[INFO ] [rulesupport.loader.ScriptFileWatcher] - Loading script '/openhab/conf/automation/jsr223/javascript/personal/astro_sun_tomorrow.js'
[ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/openhab/conf/automation/jsr223/javascript/personal/astro_sun_tomorrow.js': /openhab/conf/automation/jsr223/javascript/personal/astro_sun_tomorrow.js:9:0 Expected an operand but found const
const {getSunrise, getSunset} = require('sunrise-sunset-js');
^ in /openhab/conf/automation/jsr223/javascript/personal/astro_sun_tomorrow.js at line number 9 at column number 0

I also tried variations with var and even import, but all failed. The only way it works is by specifying them one by one with a var declaration as const is not accepted:

var getSunrise = require('sunrise-sunset-js').getSunrise;
var getSunset = require('sunrise-sunset-js').getSunset;

This looks lik the ECMAScript 2021 syntax is not accepted.

Second example:

[INFO ] [rulesupport.loader.ScriptFileWatcher] - Loading script '/openhab/conf/automation/jsr223/javascript/personal/astro_sun_tomorrow.js'
[ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/openhab/conf/automation/jsr223/javascript/personal/astro_sun_tomorrow.js': /openhab/conf/automation/jsr223/javascript/personal/astro_sun_tomorrow.js:149:21 Expected comma but found =>
    execute: (event) => {
                     ^ in /openhab/conf/automation/jsr223/javascript/personal/astro_sun_tomorrow.js at line number 149 at column number 21

The code:

    name: "Calculate tomorrows sunrise and sunset times",
    description: "Recalculate tomorrow's sunrise/sunset time every 10 minutes",
    triggers: [triggers.GenericCronTrigger("0 10 0 ? * * *")], //For testing purpose
    execute: (event) => {
        var tomorrow = new Date();
        let tomorrow.setDate(tomorrow.getDate()+1);
        var tomorrowSunrise = getSunrise(LATITUDE, LONGITUDE, tomorrow);
        var tomorrowSunset = getSunset(LATITUDE, LONGITUDE, tomorrow);
        events.postUpdate("Astro_Sun_RiseTomorrow", tomorrowSunrise);
        events.postUpdate("Astro_Sun_SetTomorrow", tomorrowSunset);
        logger.info("Tomorrow's sunrise [{}] and sunset [{}] calculated", tomorrowSunrise, tomorrowSunset);
    tags: ["Astro"],
    id: "AstroSunTomorrow"

What is wrong with my code? I suppose it might be related to the JS dialect, but I have to admit that the documentation and in sometimes outdated forum posts confused me as to what exact setup to use for the ECMAScript 2021+ JavaScript syntax.
I have installed @CrazyIvan359 's helper libraries and the JSScripting add-on (not the Nashorn version).

Hi, from one openHab beginner to another :wink: I’m also familiar with other programming languages and using the JS-script in openHab to get complicated actions done. What I noticed, somehow the script engine doesn’t accept all javascript construction/code. Especially the more complex ones and shortened notations. So your solution is what I did a lot too, avoid the complex/shorted statements and use the simple (old) ones. They usually work.

Not much help probably but the reason for this reply … I found a great Astro binding. It has all the things you probably want (sunrise, sunset) and the best one ‘civilDusk/Dawn’ (the best to trigger lights on/off because it is for ‘dark/light for humans’. It has triggers also that you can use in standard actions. Works great for me, the best code is no code :smiley: Interested, check out https://www.openhab.org/addons/bindings/astro/

Thanks for your reply. What confused me most was the fact that I did see some examples in forum posts with the ‘complex’ grammar. This seems to be for the newer ECMAScript 2021+ standard, but as far as I understand, the older standard (ECMAScript 5.1) is included with OH and the newer standard comes with the JavaScript add-on (which I did install).

BTW, I do use the astro binding for some stuff like the day phase, but I need the sunset/-rise times for tomorrow, not only for today. Example use case: once the sun is up on a day, I would like to show the sunrise time for tomorrow and not the one for today which has already passed.

OK, I made progress… Turns out I had to place the script in conf/automation/js and not in conf/automation/jsr223… now it is interpreted as ECMAScript 2021.