Chromecast / Google Home - automatic start playing a stream when motion is detected in a room

I wonder if the Title channel of the Chromecast Thing is equivalent? One could link a String Item to the channel and use that in the Rules logic that would implement the same delay logic presented in the Python script you posted. I’ll bookmark this for a future enhancement to my Chromecast notifications logic.

Thanks.

Mike

1 Like

Yes it is the title channel. You can do the same thing with xtend as well.

String FF_GreatRoom_ChromeCastTitle "Title [%s]"                                
  { channel="chromecast:audio:greatRoom:title" } 

Here is the Title check implemented as openHAB Rules code:

Group gChromecastsTitle
Player myAudioPlayer
        "Google Home"
String  myAudioPlayer_title
        "Google Home: Title" (gChromecastsTitle)
val Functions.Function1<String,String> getChromecastSink = [ String itemName |
    try
    {
        val allLinks = sendHttpGetRequest("http://localhost:8080/rest/links")
        var String filter = "$..[?(@.itemName=='"+itemName+"')].channelUID"
                                                                                                                                                                                                                    
        var output = transform("JSONPATH", filter, allLinks)
        var chromecastSink = output.split(":").get(0)+":"+output.split(":").get(1)+":"+output.split(":").get(2)
        return chromecastSink
    }
    catch(Exception e)
    {
        logError("myLog", "Failure in getChromecastSink: {}", e.toString)
        return null
    }
]

var String itemName = null
var String broadcastMessage = null
var Timer tBroadcastWait = null

rule "Broadcast Notification"
when
    Item VT_messageBroadcast received update
then
    try
    {
        // If it's between 8am and 10pm, broadcast to a message
        if  ((now.getHourOfDay > 07)  && (now.getHourOfDay < 22))
        {
            itemName = VT_messageBroadcast.state.toString
            broadcastMessage = null
            
            if (itemName.contains(";"))
            {
                broadcastMessage = itemName.split(";").get(1)
                itemName = itemName.split(";").get(0)
            }

            val audioSink = getChromecastSink.apply(itemName)
            playSound(audioSink, "dingding.mp3")
            if (broadcastMessage !== null)
            {
                tBroadcastWait = createTimer(now.plusSeconds(1), [ |
                    if (gChromecastsTitle.members.filter [titleItem | titleItem.name == (itemName + "_title")].head.state == "Notification")
                    {
                        tBroadcastWait.reschedule(now.plusSeconds(1))
                    }
                    else
                    {
                        tBroadcastWait = null
                        say(broadcastMessage, "voicerss:enUS", audioSink)
                    }
                ])
            }
        }
    }
    catch(Exception e)
    {
        logError("myLog", "Failure in Broadcast Notification: {}", e.toString)
    }
end
2 Likes