Accessing HomeKit scene events from OH

Is there a way to access HomeKit scene events in OH?

I’m trying to set up an automation that will operate differently if the HomeKit “Good Night” scene is enabled.

add a virtual item and toggle it in your homekit good night scene. then create a rule which triggers when the switch is toggled and put your automation in that rule

That was my initial thought, and it’s probably the easiest.

I created the following item (in a file homekit.items):

// Proxy for identifying activation of "Good Night" scene in HomeKit:
Switch HomeKit_Scene__Good_Night    "Good Night [%s]"

then I added some nodes in Node-RED:


The code in the OH to HomeKit function node reads as follows:

if (msg.payload == "ON") {
    msg.payload = {
        "On": true
    };
} else {
    msg.payload = {
        "On": false
    };
}
return msg;

And the code in the HomeKit to OH function node reads as follows:

if (msg.hap.context !== undefined ) {
    if (msg.payload.On === false){
        msg.payload = "OFF";
    } else if(msg.payload.On === true) {
            msg.payload = "ON";
    }
    return msg;
}

Deploy the changes in Node-RED and restart the nodered process to expose the new Switch item to HomeKit. if on Linux-like OS, type:
$ sudo systemctl restart nodered

Now your switch item appears in Apple Home. You can now include this switch to your “Good Night” scene. It will toggle on when the scene is activated, and it will turn off when the scene is deactivated.

I recommend to remove the newly discovered switch from the favorites to avoid using it by accident in Apple Home.

EDIT: What I could’t find, is how to enable and disable a HomeKit scene from OH. There’s some hint at using a Stateless Programmable Switch but I can’t get it to work.