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!