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');
gives:
[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:
rules.JSRule({
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);
calcNextSunTimes();
},
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).