Use trigger channel in rule

Hi!

I would like to use the Harmony’s starting trigger inside a rule. Is there a function which can return the current trigger (like Things have getThingStatusInfo)?

Thanks!

Yes, there are several examples for this in the docs:

rule "Starting TV"
when
    Channel "harmonyhub:hub:GreatRoom:activityStarting" triggered Watch_TV
then
    logInfo("Harmony", "TV is starting...")
end

rule "TV started"
when
    Channel "harmonyhub:hub:GreatRoom:activityStarted" triggered Watch_TV
then
    logInfo("Harmony", "TV is started")
end

rule "Going off"
when
    Channel "harmonyhub:hub:GreatRoom:activityStarting" triggered PowerOff
then
    logInfo("Harmony", "Hub is going off...")
end

rule "Hub off"
when
    Channel "harmonyhub:hub:GreatRoom:activityStarted" triggered PowerOff
then
    logInfo("Harmony", "Hub is off - no activity")
end

I think this answers what you are looking for:

1 Like

These are what I’m Looking for. I was unable to find it. Thanks!!

For JSR libs, these are even easier:

@rule("Test rule")
@when("Channel harmonyhub:hub:LivingRoomHarmonyHub:activityStarting triggered")
def test_rule(event):
    test_rule.log.info(str(event.event))

You can easily use event.event for this.
If you have multiple triggers with not just Channel triggers in it, you can do something like this, so your rule won’t fail:

@rule("Test rule")
@when("Item Test_Switch received command")
@when("Channel harmonyhub:hub:LivingRoomHarmonyHub:activityStarting triggered")
def test_rule(event):
    if hasattr(event, 'event'):
        test_rule.log.info(str(event.event))
    else:
        # Do anything here when not a channel triggered this rule
        test_rule.log.info("No attrib")

You can even get the triggering channel with event.channel. These were the ones I was looking for then, now it is much easier to use

1 Like

More details on what you might find in event

Rather than using str, you can use event.event.toString().