Alarm Clock in OpenHab3

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

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.

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.

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