Hi Guys,
I have a Philips Hue remote which starts and stops a fan. When doing so it also sets two hub variables. The reason it updates two hub variables is so that other apps can subscribe to the event when the fan was turned on or off by the Hue remote. But for some reason I cannot see, the location subscription is not firing here. What am I missing?
Location log:
Hub variables:
def initialize() {
// Subscribe to hub variables
subscribe(location, "roof_fan_changed_by_gv", fetchHubVariables)
subscribe(location, "roof_fan_power_status_gv", fetchHubVariables)
//Define state variables
state.startedRemoteBy = state.startedRemoteBy ?: "No value set"
state.remotePowerStatusInfo = state.remotePowerStatusInfo ?: "No value set"
}
def fetchHubVariables() {
log.debug "fetchHubVariables engaged"
def startedRemoteBy = fetchHubVariable("roof_fan_changed_by_gv")
state.startedRemoteBy = startedRemoteBy
def remotePowerStatusInfo = fetchHubVariable("roof_fan_power_status_gv")
state.remotePowerStatusInfo = state.remotePowerStatusInfo
log.debug "The roof fan was set to ${remotePowerStatusInfo} by the ${startedRemoteBy}"
}
def fetchHubVariable(varName) {
def variable = getGlobalVar(varName)
log.debug "variable: ${variable}"
return variable?.value
Log file:

What are you expecting this to do? It's looking for a location event by the name you specified. That is probably not what you want and is unlikely to happen (though it would be possible for you to create such an event).
If you want to subscribe to changes to a hub varoanke of that name, you'll need to use that event name as documented. You can verify in your location event history, but as documented, this should work:
subscribe(location, "variable:roof_fan_changed_by_gv", "fetchHubVariables")
1 Like
Thanks for your reply.
I was hoping this would subscribe to the event when the hub variable named there was updated. It looked pretty analog to the solution in this post and in this post as well as I could interpret from the documentation I found (subscribe). I also asked Code GPT which suggested what I had done originally. I didn't find the page you are refering to, but with some testing the versions below updates without errors.
subscribe(location, "variable:roof_fan_changed_by_gv.value", "fetchHubVariables")
subscribe(location, "variable:roof_fan_power_status_gv.value", "fetchHubVariables")
But unfortunately nothing else happens either. The local state variables are not updated.

I also tried chaning the handlers to the following:
subscribe(location, "variable:roof_fan_changed_by_gv.value", "receiveChangedByHubVariable")
subscribe(location, "variable:roof_fan_power_status_gv.value", "receivePowerStatusHubVariable")
def receiveChangedByHubVariable(evt) {
def startedBy = evt.value
state.startedRemoteBy = startedBy
log.debug "state.startedRemoteBy: ${state.startedRemoteBy}"
}
def receivePowerStatusHubVariable(evt) {
def powerValue = evt.value
state.remotePowerStatusInfo = powerValue
log.debug "state.remotePowerStatusInfo: ${state.state.remotePowerStatusInfo}"
}
Same outcome:

Any thoughts?
EDIT: I noticed that the remote that updates the variables is registered as using the variables but the app is not registered with the variables. Can that play a part?
This is not going to work either; the text value
is intended to be a literal value if you want to subscribe to only a specific event value. If you want to subscribe to all changes, you want to do what I said above (and as described in the docs). Did you try that?
No. You need to register your app as using the hub variables. See the documentation:
1 Like
Ok, thanks.
Yes, I did. But the first time I used the first handler and that gave some errors which is why I tried the .value version. But with the new handlers wit worked like charm.
Thank you!
I only provided one suggestion, so I'm not sure what the "first" and "new" methods are.
If an error happens again, sharing exactly what that is would be the best way to get some help. The call I provided above should not cause an error on its own.
In any case, glad it is working now!
1 Like
Sorry for the confusion.
When I first used the subsciption syntax you suggested, I had an error in the handler method I was not at the time aware of, causing an error. I corrected the error and then all workd fine. The syntax you suggested worked flawlessly at all times. Again, thank you, very helpful (Is still don't understand how the subscription syntax could work in the two posts I mentioned previously, but that is more academic at this point).
1 Like
From what I see in the other posts, those are subscribing to other location events (i.e., location events that are not specifically hub variable changes). As you can see in the syntax for the subscribe method, you need three pieces of information: the object to subscribe to (location
or a device reference), the event name to subscribe to (e.g., "mode"
in one of th examples above for mode changes; variable:variableName
in your case where variable:
is literally that exact text and variableName
is the name if your variable; beyond the documentation, you can see the actual event name in your location event history, accessible from the tab on the Logs page), and the handler method.
In your case -- or at least the one you posted above -- the event name was incorrect, so the subscription was not catching what you were looking for. In their case, the event name was correct for what they were doing.