Can i call the value of a state for use elsewhere?

Hi All

I’m having a problem with a variable or state. I have created a stare named bgC and I am using this as a variable to store a colour in HEX, eg #223C4A. This value changes based on a colour changer tile for a lightbulb.

What I am trying to do is call this HEX value as a label for a tile.

state "colorName", label:getBgHex()

The function code is as follows…

private getBgHex(){
    state?.bgC
    def bgcHex = "String"
    bgcHex = state?.bgC.toString()
    log.debug "Value should be: $bgcHex"
    return bgcHex
}

When I look at the tile it says NULL but I can seen in the log I can see Value should be: #223C4A.

If I change the function to the following it will pass a value to the label but obviously this is not dynamic and will only pass the hard coded value.

private getBgHex(){
    return "#FFFFFF"
}

Its as if the return doesn’t actually include the value from the variable bgcHex!!

Can anyone help me with this problem please?

Thanks

What does your full tile code look like? I don’t think tile labels can run code dynamically. The best practice would be to use an attribute, and it will update when an event updates that attribute.

For color in hex, you can just use the color attribute which is the hex value from the color picker.

You need to use sendEvent() to update the State of an Attribute or Value used in a Tile.

Sorry guys, I just noticed I have put some incorrect information in my original post. I have updated this now. I am using the following to call the function in order to get the value of the state.

state "colorName", label:getBgHex()

As I said before, if I manually set a value in my function, like this,

private getBgHex(){
    return "#FFFFFF"
}

My tile will update to display #FFFFFF as its label, no I need to be able to return the value of a state but when I attempt to use the following code my tile displays the word NULL but I can see in the debug log that the state is set correctly to a HEX value.

private getBgHex(){
    state?.bgC
    def bgcHex = "String"
    bgcHex = state?.bgC.toString()
    log.debug "Value should be: $bgcHex"
    return bgcHex
}

Any ideas please?

Thanks

Hi tgauchat

When I try and use the command sendEvent(name: "bgcHex", value: bgcHex) I get the following error when saving the handler.

java.lang.NullPointerException: Cannot invoke method latestState() on null object

I don’t know where I’m going wrong here.

Thanks

You can’t call a function in the tiles section. The tiles aren’t dynamic. They only load/reload on event changes, which is why you can’t have a tile background color match a dynamic hex value. The second instance probably works because the function effectively is a constant.

If you post your entire device handler code, maybe we can see what you’re trying to do and help you find he best solution available with the current constraints of the API.

1 Like