- Platform information:
- Hardware: Raspberry Pi 4 Model B Rev 1.1 ; 4GB memory
- Host: Linux openhabian 5.15.76-v7l+ #1597 SMP Fri Nov 4 12:14:58 GMT 2022 armv7l GNU/Linux
- Distro: Raspbian GNU/Linux 11 (bullseye)
- openjdk version “11.0.18” 2023-01-17
- OpenJDK Runtime Environment (build 11.0.18+10-post-Raspbian-1deb11u1)
- OpenJDK Server VM (build 11.0.18+10-post-Raspbian-1deb11u1, mixed mode)
- OH Version: 3.4.3 (Build)
- Installation method: openHABian
Directories: Folder Name | Path | User:Group
----------- | ---- | ----------
OPENHAB_HOME | /usr/share/openhab | openhab:openhab
OPENHAB_RUNTIME | /usr/share/openhab/runtime | openhab:openhab
OPENHAB_USERDATA | /var/lib/openhab | openhab:openhab
OPENHAB_CONF | /etc/openhab | openhab:openhab
OPENHAB_LOGDIR | /var/log/openhab | openhab:openhabian
OPENHAB_BACKUPS | /var/lib/openhab/backups | openhab:openhab
I took the plunge, after two years of contemplation , to move from ruleDSL to ECMAscript 2021. I did not need to install or change my environment, as I had installed Automation JS two years ago.
openhab> bundle:list | grep JavaScript
268 │ Active │ 80 │ 3.4.3 │ openHAB Add-ons :: Bundles :: Automation :: JavaScript Scripting
309 │ Active │ 75 │ 3.4.3 │ openHAB Add-ons :: Bundles :: Transformation Service :: JavaScript
The script I ran, actually works, but does not log.
const { log } = require('openhab');
rules.JSRule({
name: "TestSwitch",
triggers: [triggers.ItemStateChangeTrigger('test_Switch')],
execute: () => {
log.info(`test_Switch is ${items.getItem('test_Switch').state}`);
}
});
The error I am getting is:
2025-04-15 22:20:08.701 [INFO ] [ort.loader.AbstractScriptFileWatcher] - Loading script '/etc/openhab/automation/js/test.js'
2025-04-15 22:20:15.430 [INFO ] [.openhab.automation.openhab-js.rules] - Adding rule: TestSwitch
2025-04-15 22:20:19.914 [ERROR] [enhab.automation.script.file.test.js] - Failed to execute rule TestSwitch-dfe231ee-bca2-40aa-98ce-57e84d42edde: TypeError: log.error is not a function: TypeError: log.error is not a function
2025-04-15 22:20:19.928 [ERROR] [e.automation.internal.RuleEngineImpl] - Failed to execute rule 'TestSwitch-dfe231ee-bca2-40aa-98ce-57e84d42edde': Fail to execute action: 1
2025-04-15 22:20:23.301 [ERROR] [enhab.automation.script.file.test.js] - Failed to execute rule TestSwitch-dfe231ee-bca2-40aa-98ce-57e84d42edde: TypeError: log.error is not a function: TypeError: log.error is not a function
2025-04-15 22:20:23.303 [ERROR] [e.automation.internal.RuleEngineImpl] - Failed to execute rule 'TestSwitch-dfe231ee-bca2-40aa-98ce-57e84d42edde': Fail to execute action: 1
2025-04-15 22:24:39.818 [INFO ] [ort.loader.AbstractScriptFileWatcher] - Loading script '/etc/openhab/automation/js/test.js'
2025-04-15 22:24:45.876 [INFO ] [.openhab.automation.openhab-js.rules] - Adding rule: TestSwitch
2025-04-15 22:24:50.457 [ERROR] [enhab.automation.script.file.test.js] - Failed to execute rule TestSwitch-9dd9bcf9-9609-4c8e-b004-dc0b2b37492a: TypeError: log.info is not a function: TypeError: log.info is not a function
2025-04-15 22:24:50.464 [ERROR] [e.automation.internal.RuleEngineImpl] - Failed to execute rule 'TestSwitch-9dd9bcf9-9609-4c8e-b004-dc0b2b37492a': Fail to execute action: 1
2025-04-15 22:24:52.614 [ERROR] [enhab.automation.script.file.test.js] - Failed to execute rule TestSwitch-9dd9bcf9-9609-4c8e-b004-dc0b2b37492a: TypeError: log.info is not a function: TypeError: log.info is not a function
2025-04-15 22:24:52.616 [ERROR] [e.automation.internal.RuleEngineImpl] - Failed to execute rule 'TestSwitch-9dd9bcf9-9609-4c8e-b004-dc0b2b37492a': Fail to execute action: 1
I then changed to the SLF4J logger:
const LoggerFactory = Java.type('org.slf4j.LoggerFactory');
const logger = LoggerFactory.getLogger('org.openhab.TestSwitch');
rules.JSRule({
name: "TestSwitch",
triggers: [triggers.ItemStateChangeTrigger('test_Switch')],
execute: function(event) {
logger.info("test_Switch changed to " + event.newState);
}
});
This works, without creating an error.
2025-04-15 22:39:55.902 [INFO ] [ort.loader.AbstractScriptFileWatcher] - Loading script '/etc/openhab/automation/js/test.js'
2025-04-15 22:40:02.605 [INFO ] [.openhab.automation.openhab-js.rules] - Adding rule: TestSwitch
2025-04-15 22:40:06.171 [INFO ] [org.openhab.TestSwitch ] - test_Switch changed to ON
2025-04-15 22:40:08.739 [INFO ] [org.openhab.TestSwitch ] - test_Switch changed to OFF
My questions, though I understand time has moved on from 3.4.3,…
- was
log.info
andlog.error
not included in 3.4.3? - should I upgrade to 3.4.5, which I think was the lastest in v3?
- should I upgrade to 4-latest, before moving my DSL rules to ECMAscript?
- editing rule DSL in VScode gives me name completion of OH items; is this available for ECMAscript as well?
As usual, any hints appreciated.