Ste
(SteffenWagner)
July 18, 2021, 7:31pm
1
Hi!
today I did my first steps in ECMA, I tried to build an alarm clock which for example turns on the light in the morning.
I tried to find some templates, all I’ve found was for older OpenHab versions.
My adaption to Openhab3 does not work:
triggers:
id: “1”
configuration:
cronExpression: 0 * * * * ? *
type: timer.GenericCronTrigger
conditions: []
actions:
inputs: {}
id: “2”
configuration:
type: application/javascript
script: |
var sollStunde, sollMinute, hour_now;
sollStunde = itemRegistry.getItem('Wecker_h').getState();
sollMinute = itemRegistry.getItem('Wecker_Minute').getState();
hour_now = now.getHour()
if (sollStunde == hour_now) {
events.sendCommand('EG_Licht_Kueche', 'ON');
}
type: script.ScriptAction
The log shows the following messages:
2021-07-18 21:28:00.718 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘e292f18d18’ failed: ReferenceError: “now” is not defined in at line number 6
2021-07-18 21:28:00.911 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘ad860ef9ee’ failed: :3:97 Expected ) but found as
Does anybody see the bug?
Best regards
ubeaut
(Greg)
July 19, 2021, 12:00am
2
When I first started ECMA scripts I used the design with blockly option and wrote the code with it. I saved the blockly code and then click on the code tab of the rule and the ECMA script code is there.
I used that method to get the syntax and code structure. I am still not an expert but that got me started. I don’t use DSL rules anymore.
For simple rules I just use the UI for example below:
For everything else I just copy and past from example codes etc.
If you have JavaScript examples you have used then put them here so others can cut and paste them to help getting used to using JavaScript rules.
Don’t put DSL rules here as it will get confusing.
I have had problems trying to find JavaScript examples in the past and then I had to work out whether it was DSL or JavaScript as they are similar.
Anyway having said that I will put some examples of what has worked for me. Feel free to modify/correct/improve as I am definitely NOT a JavaScript expe…
PS: To get the hour you have to do this:
var datetoday = new Date();
var hour_now = datetoday.getHours();
Good luck with ECMA. I have been using it for about 6 months.
Ste
(SteffenWagner)
July 25, 2021, 3:46pm
3
thanks a lot for your support.
You helped me, solving my problem:
triggers:
id: “1”
configuration:
cronExpression: 0 * * * * ? *
type: timer.GenericCronTrigger
conditions:
inputs: {}
id: “3”
configuration:
itemName: Automatik_aus
state: OFF
operator: =
type: core.ItemStateCondition
actions:
inputs: {}
id: “2”
configuration:
type: application/javascript
script: >
var sollStunde, sollMinute, hour_now, datetoday, wecker_on;
sollStunde = itemRegistry.getItem('Wecker_h').getState();
sollMinute = itemRegistry.getItem('Wecker_Minute').getState();
wecker_on = itemRegistry.getItem('Wecker_on').getState();
var datetoday = new Date();
var hour_now = datetoday.getHours();
var minute_now = datetoday.getMinutes();
hour_now = datetoday.getHours()
if (sollStunde == hour_now && sollMinute == minute_now && wecker_on == "ON") {
events.sendCommand('Dimmaktor_LichtSchlafzimmer', '100');
}
type: script.ScriptAction