Is not a function error in jsscripting (ecma2021)

Hi Guys :wink:

my new JSRule gives an error and I don’t know how to solve it…

this is the error from the logs:

[ERROR] [ation.script.file.rules_tuya2mqtt.js] - Failed to execute rule TuyaAPI
---update-Tuya-SmartBulbs-5556ead8-cbb8-4ed9-b622-27703f5314e3: Type
Error: timev.isBefore is not a function: TypeError: timev.isBefore is not a function

it is this if clause which produces the error:

if (timev.isBefore(gap)) {

  ...

}

but I haven’t found a solution - I don’t even understand why this occurs…

in another rule I defined cases for a switch like this:

 switch (true) {
      case now.isAfter(bedtime) && now.isBefore(dawnstart):
        curr1 = "BEDTIME";
        break;

  ...
}

bedtime and dawnstart are also DateTimeStrings like timev and gap
the switch cases are working great… can anyone tell why it’s not working here in this rule and why js throws this “is not a function” error?

what’s the difference?

here is the complete rule:

const { log, rules, triggers } = require("openhab");

var now = time.ZonedDateTime.now();

const timevalue = items.getItem("TimeValue");
const lastchanged = null;

const LRColor = items.getItem("LightLivingroomColor");
const LRDimmer = items.getItem("LightLivingroomDimmer");
const LRMode = items.getItem("LightLivingroomMode");
const LRCt = items.getItem("LightLivingroomCT");
const LRSwitch = items.getItem("LightLivingroomSwitch");

const Color1 = items.getItem("LightSmartBulb01Color");
const Brightness1 = items.getItem("LightSmartBulb01Brightness");
const CT1 = items.getItem("LightSmartBulb01CT");
const Mode1 = items.getItem("LightSmartBulb01Mode");
const Switch1 = items.getItem("LightSmartBulb01Switch");

const Color2 = items.getItem("LightSmartBulb02Color");
const Brightness2 = items.getItem("LightSmartBulb02Brightness");
const CT2 = items.getItem("LightSmartBulb02CT");
const Mode2 = items.getItem("LightSmartBulb02Mode");
const Switch2 = items.getItem("LightSmartBulb02Switch");

const Color3 = items.getItem("LightSmartBulb03Color");
const Brightness3 = items.getItem("LightSmartBulb03Brightness");
const CT3 = items.getItem("LightSmartBulb03CT");
const Mode3 = items.getItem("LightSmartBulb03Mode");
const Switch3 = items.getItem("LightSmartBulb03Switch");

timevalue.sendCommandIfDifferent(now);
rules.JSRule({
  name: "TuyaAPI - update Tuya SmartBulbs",
  description: "update Tuya SmartBulbs",
  triggers: [
    triggers.ItemStateChangeTrigger("LightLivingroomColor"),
    triggers.ItemStateChangeTrigger("LightLivingroomDimmer"),
    triggers.ItemStateChangeTrigger("LightLivingroomCT"),
    triggers.ItemStateChangeTrigger("LightLivingroomMode"),
    triggers.ItemStateChangeTrigger("LightLivingroomSwitch"),
  ],
  execute: (data) => {
    let event;
    (function (event) {
      var now = time.ZonedDateTime.now();
      var timev = timevalue.state
      var gap = now.minusSeconds("2");

      console.log("TimeValue: " + timevalue.state + " Gap: " + gap);

      if (timev.isBefore(gap)) {
        if (Color1.state !== LRColor.state) {
          Mode1.sendCommand("OFF");
          Mode2.sendCommand("OFF");
          Mode3.sendCommand("OFF");
          Color1.sendCommand(LRColor.state);
          Color2.sendCommand(LRColor.state);
          Color3.sendCommand(LRColor.state);
        }
        if (Brightness1.state !== LRDimmer.state) {
          Brightness1.sendCommand(LRDimmer.state);
          Brightness2.sendCommand(LRDimmer.state);
          Brightness3.sendCommand(LRDimmer.state);
        }
        if (CT1.state !== LRCt.state) {
          CT1.sendCommand(LRCt.state);
          CT2.sendCommand(LRCt.state);
          CT3.sendCommand(LRCt.state);
        }
        if (Mode1.state !== LRMode.state) {
          Mode1.sendCommand(LRMode.state);
          Mode2.sendCommand(LRMode.state);
          Mode3.sendCommand(LRMode.state);
        }
        if (Switch1.state !== LRSwitch.state) {
          Switch1.sendCommand(LRSwitch.state);
          Switch2.sendCommand(LRSwitch.state);
          Switch3.sendCommand(LRSwitch.state);
        }
        timevalue = time.ZonedDateTime.now();
      }
    })(event);
  },
  tags: [],
});

thanks in advance, hope someone can help me understand the cause :wink:

It looks like the variable timev is a string instead of a ZonedDateTime. If I understand correctly, IsBefore is a method that has to work on a ZonedDateTime object, such as time.ZonedDateTime.now(). You can’t apply it to a string.

1 Like

Hi John, thanks a ton!

I changed the definition to

var timev = time.toZDT(timevalue.state);

now the error is gone :wink:

1 Like