[JS Scripting] Need help with rule triggering

Hi everyone,

I’m currently trying to set the current time-of-day phase using the Astro binding in a JS Scripting rule (openHAB 4.x ). The rule is being triggered correctly, but the phase variable always ends up as "Unknown", meaning none of my if conditions are matching.

Here’s my rule code:

rules.JSRule({
  name: "Time of Day via Astro Events (robust)",
  description: "Sets the time-of-day phase based on Astro events with if-conditions",
  triggers: [
    triggers.ChannelEventTrigger("astro:sun:home:civilDawn#event", "START"),
    triggers.ChannelEventTrigger("astro:sun:home:sunrise#event", "START"),
    triggers.ChannelEventTrigger("astro:sun:home:sunrise#event", "END"),
    triggers.ChannelEventTrigger("astro:sun:home:noon#event", "START"),
    triggers.ChannelEventTrigger("astro:sun:home:sunset#event", "START"),
    triggers.ChannelEventTrigger("astro:sun:home:sunset#event", "END"),
    triggers.ChannelEventTrigger("astro:sun:home:nauticDusk#event", "END")
  ],
  execute: (event) => {
    const logger = log("TimeOfDay");

    const channel = event.channelUID;
    const eventType = event.event;
    logger.info("EVENT DEBUG → Channel: " + channel + ", Type: " + eventType);

    let phase = "Unknown";

    if (channel === "astro:sun:home:civilDawn#event" && eventType === "START") {
      phase = "Dawn";
    } else if (channel === "astro:sun:home:sunrise#event" && eventType === "START") {
      phase = "Sunrise";
    } else if (channel === "astro:sun:home:sunrise#event" && eventType === "END") {
      phase = "Morning";
    } else if (channel === "astro:sun:home:noon#event" && eventType === "START") {
      phase = "Noon";
    } else if (channel === "astro:sun:home:sunset#event" && eventType === "START") {
      phase = "Sunset";
    } else if (channel === "astro:sun:home:sunset#event" && eventType === "END") {
      phase = "Evening";
    } else if (channel === "astro:sun:home:nauticDusk#event" && eventType === "END") {
      phase = "Night";
    }

    logger.info("New time phase: " + phase);
    items.getItem("Tageszeit_Status")?.postUpdate(phase);
  }
});

As you can see, even though the channel and type should match the conditions, the rule still outputs "Unknown".

My question:
Where is my mistake here? Is event.channelUID really a plain string, or do I need to extract something from it? Is there something wrong with how I’m comparing event.event?

Any help or insight would be greatly appreciated!

Hi,
let ist not supported, see here, use var.
Without testing, maybe this is the solution.

@uk59821 unfortunately not solved :frowning:

For comparable triggers, I use the variable receivedEvent for the eventType like this:

const eventType = event.receivedEvent;

@uk59821 is correct, the problem is that you are are trying to access event.event, but the event object has no such property.

You are using the file-based js rules, so you need to reference the event object table near the very bottom of the main doc:

In that table you will see there is no event property and the one you are looking for is receivedEvent.

In this case that means that none of your if statements are triggering because your eventType constant’s value is undefined.

Here is some more on how to possibly tackle event.