Script issues with Date()

I have two OPH based on RaspberyPi 4. One system runs with t OH 4.0.1. another have been updated to 4.1.0. I wanted to copy one script checking the battery status.
Running the same script with 4.1.0 i receive following error. Are there any changes between versions that touches Date();?

2023-12-31 17:49:31.954 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'BatteryStatus' failed: var datetoday = new Date();
var datetoday = new Date();
var numberofweek = datetoday.getDay();
//var push_action = actions.thingActions('pushover', 'pushover',");

This will check the percentage of battery left and if it is low then email
Add the items to check into gBatteries group
var percenttocheck = 40;
var heading="[OH4 Wesiory] Daily low sensor battery report"

//Only do on Sundays (numberofweek is 0)
if (numberofweek == 0){ 
  var percenttocheck = 100;
  var heading="[OH4 Wesiory] Weekly sensor battery report"

var email_message = "";
var i;
var sendmail="";
var i_list = items.getItem("gBatteries").members;

for (var i_index in i_list) {
  i = i_list[i_index];
  if(i.state <= percenttocheck){sendmail="YES"
  email_message = email_message + i.label + " " + i.state + "%" + "\r\n";
  message = "<H2>Sensor battery report</H2> <br>" +email_message +"<br><br>" +"Percentage check was set at " +percenttocheck;
  actions.get("mail", mail","mail:smtp:google").sendHtmlMail("", heading, message );
  actions.NotificationAction.sendBroadcastNotification("Check Battery Level!", "batterylevel", "high");

The line above doesn’t look correct. Maybe it’s that giving the error.

It you want to just test the Date() syntax then try it in the scripts area of oh and run it and see if you still get the error.

I just ran a script on OH 4.1 and my script works.

It has:
var datetoday = new Date();
var numberofweek = datetoday.getDay();

in it and it works fine.

Thanks Greg,
this was a copy &paste failure indeed but the main problem was that I created DSL rule instead of ECMA Script. Now it does not sends any errors.

Glad you got it to work as the code you are using is what I wrote but I haven’t swapped over to OH4.1 just yet. I do have a test system and that was what I tested the code on.

Pity the error doesn’t say hey this is DSL not ECMA Script. :grinning:

True. It could help some unexperienced memebrs like me to find the solution faster.

Your code works also on 4.1.0 without issues. Thanks for sharing it.

No, but the helper library for the JS Scripting add-on is built around joda-js ZonedDateTimes, not the built in ECMAScript Date class.

Most of the docs and examples will be based on that.

var datetoday = time.toZDT();
var numberofweek =datetoday.dayOfWeek().value();

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.