[SOLVED] Better way to map Harmony activity state

Hello everyone,

as some you I have a Harmony and made an Item (Switch) for every activity, allowing me start/stop the activity with HabPanel and Alexa.

But it bothers me when the state shown in openHab is wrong when I started/stopped an activity with the remote instead of using openHab, which happens quite often. Therefore I made a rule which maps the harmony’s state to the corresponding Items.

The tricky part is that every time I update the Item state due to an external change (with the remote) the rule fires the command again. This isn’t a big deal when you just have one activity as it is already running, but things are getting a mess when you have more than one activity. Therefore I used a boolean variable for each activity to prevent that mess. It works perfectly, but it feels quick and dirty and is also more prone to error and complicated when it comes to adding another activity. I know this doesn’t happen very often but maybe someone has a better idea:

// Define Global Variables 
var boolean Internal_State_Changer_Harmony_1 = true
var boolean Internal_State_Changer_Harmony_2 = true

rule "Harmony Activity Movies start or stop"
when
        Item Harmony_Activity_Movies received update
then
        if(Internal_State_Changer_Harmony_1 && Internal_State_Changer_Harmony_2){
            if ( Harmony_Activity_Movies.state == ON ){
                sendCommand(HarmonyHub_CurrentActivity,"Movies")
            }else{
                sendCommand(HarmonyHub_CurrentActivity,"PowerOff")
            }
        }
        Internal_State_Changer_Harmony_1 = true
end



rule "Harmony Activity Server start or stop"
when
        Item Harmony_Activity_Server received update
then
        if(Internal_State_Changer_Harmony_1 && Internal_State_Changer_Harmony_2){
            if ( Harmony_Activity_Server.state == ON ){
                sendCommand(HarmonyHub_CurrentActivity,"Server")
            }else{
                sendCommand(HarmonyHub_CurrentActivity,"PowerOff")
            }
        }
        Internal_State_Changer_Harmony_2 = true
end



rule "Map external Harmony state to OpenHAB"
when
        Item HarmonyHub_CurrentActivity received update
then
        Thread::sleep(200) 
        var actistatus=HarmonyHub_CurrentActivity.state // Current Activity
        if(actistatus == "Movies"){
            postUpdate(Harmony_Activity_Movies, ON)
        }else{
            postUpdate(Harmony_Activity_Movies, OFF)
        }

        if(actistatus == "Server"){
            postUpdate(Harmony_Activity_Server, ON)
        }else{
            postUpdate(Harmony_Activity_Server, OFF)
        }
        }
        Internal_State_Changer_Harmony_1 = false
        Internal_State_Changer_Harmony_2 = false
end

Use “received command” to trigger your rules and use "postUpdate to update the current activity Item to match the new state.

I don’t have your problem.

My item:

String Harmony_Activity     "Harmony Activity [%s]"   <remote> { channel="harmonyhub:hub:Wohnzimmer:activity" }

sitemap:

sitemap harmony label="Harmony"
{	
    Frame label="Switches" {
	Switch    item=Harmony_Activity mappings=[ MediaPC='MediaPC', Chromecast='Chromecast', Radio='Radio', RadioBK='Radio Bad/Küche', BluRay='Blu-Ray', CastAudio='CastAudio', PC='PC', PowerOff='Off' ]
    }
    Frame label="Selection" {
	Selection item=Harmony_Activity mappings=[ MediaPC='MediaPC', Chromecast='Chromecast', Radio='Radio', RadioBK='Radio Bad/Küche', BluRay='Blu-Ray', CastAudio='CastAudio', PC='PC', PowerOff='Off' ]
    }
}

(you can choose between Switch and Selection.)

Same here … no rule necessary to have the same state when switched via remote …

@christoph_wempe @sihui
I can’t use your way because:

  1. I need to use switch Items for Alexa
  2. I am using HabPanel, so no sitemaps

@rlkoshak
I will give this a try as soon as I am home. But I sounds like what I was looking for. Is there an explanation for all available methods in rules ? Except: docs.openhab and OpenHab1 Github Samples
I didn’t even know there was something like “received command”

All the rule triggers are documented in the docs.

Thanks. Btw. it works perfectly!

Hey, im quite a noob when it comes to openHAB. I try to do the exact same thing!
Harmony Hub with different activities, mapped to separate switches in HABpanel.
Some commands from the devices (connected to the harmony hub), mapped to several sliders and buttons. I just don’t understand how I can separate these commands. I only can see them, if I choose the selection widget. How can I map the switch widgets to a specific harmony command? I would love to make that happen… :slight_smile:
Thank you!

I made a proxy/dummy item for each harmony action. This item has no direct channel (meaning it does not switch a device/thing) but a rule listens to its state. In this case the rule simply starts the according activity.

To keep track of changes outside OH (e.g. with the regular remote) I have another rule which tracks those changes and maps them to my proxy/dummy items.

If you want more than one activity in OH you need to alter my following examples: You need a rule for every proxy/dummy item and need to extend the second rule for mapping more than one state.

The item definition:

// Harmony Hubs
String harmonyActivity_current "Harmony Current Activity" { channel="harmonyhub:hub:myname:currentActivity" }

// Dummy Items

Switch dummyActivity_filmeserien "Fernseher" ["Switchable"] 

The Rules:

rule "Harmony Filme/Serien start or stop"
when
Item dummyActivity_filmeserien received command
then
    logInfo("Harmony", "Internally toggle activity and send command: Filme/Serien or off")
    sendCommand(harmonyActivity_current, if (dummyActivity_filmeserien.state == ON) "Filme/Serien" else "PowerOff" )
end

rule "Harmony state mapping to OpenHAB"
when
Item harmonyActivity_current received update
then
var currentActivity = harmonyActivity_current.state
    logInfo("Harmony", "Harmony activity update detected, now: " + currentActivity)
    postUpdate(dummyActivity_filmeserien, if (currentActivity == "Filme/Serien") ON else OFF )
end