If and else to get different color background

Hi

I am using Nest Direct device type to get Nest into v2 hub and it works perfectly
I have changed a few things and added the multiAttributeTile.
Now I am trying to have the color of the thermostat tile depending upon the target temperature. Basically I would like it to be red if the target temperature is higher than the ambient remperature.

Basically I have these 2 tiles

  1. Current Temperature in the house

tiles(scale: 2) {
multiAttributeTile(name:“bigTile”, type:“lighting”, width:6, height:4) {
tileAttribute(“device.temperature”, key: “PRIMARY_CONTROL”) {
attributeState “temperature”, label: ‘${currentValue}°’, icon:“st.nest.nest-home”, backgroundColor:"#007BA7"

}

}

main "bigTile"
details “bigTile”
}

  1. Target Temperature
    valueTile(“heatingSetpoint”, “device.heatingSetpoint”, width: 2, height: 2) {
    state “default”, label:’${currentValue}°’, unit:“Cool”, backgroundColor:"#1e9cbb"
    }

And I would like to do something like

if device.heatingsetpoint is > device.temperature the backgroundcolor of the valuetile heatingsetpoint is red, otherwise is blue.

I’ve tried several options but I can’t get it right…
Can someone pls shed a light for me?

thanks a lot

Hi,

Apologies for bringing this post to your attention again, but I have spent the last two days looking at different device type in order to find a solution without success.
Could someone tell me if this can be done or not?

thanks a lot
fab

I’m unsure at the moment as I haven’t had chance to play much and am very new to Groovy and the API but from what I can see you can use variable binding (e.g. ${currentValue}) it might be possible to make a variable and then whenever the temperature is updated, update the variable…

def tempColour = "#007BA7" // Default colour

tiles(scale: 2) {
    multiAttributeTile(name:"bigTile", type:"lighting", width:6, height:4) {
        tileAttribute("device.temperature", key: "PRIMARY_CONTROL") {
            attributeState "temperature", label: '${currentValue}°', icon:"st.nest.nest-home", backgroundColor:"${tempColour}"
        }
    }
}

...
* temperature update *
if (device.heatingsetpoint == device.temperature) {
    tempColour = "#008000"
} else if (device.heatingsetpoint > device.temperature) {
    tempColour = "#ff0000"
} else {
    tempColour = "#0000ff"
}
....

Just an idea to try and explore. Once I’ve managed to get some time with writing this stuff I’ll have a better idea of what I’m talking about :smiley:

Thanks Chris. Really appreciated!
will give a try tonight and let you know
cheers
fab

Hi Chris

with 2 different temperatures I get tempcolour changing to #008000 therefore there must be something wrong in the test. I have also tried to add .currentvalue to both with no success…

Thanks anyway for helping.
Will do more tests
cheers
fab

I am going to have another play around today. I had something that might work yesterday but the IDE was giving me problems…

I think this is a limitation of the ST platform. It looks like you can’t call any attribute from the metadata section of a devicetype. I did some testing and it looks like every time a devicetype is called it runs the metadata section again after it nulls all attributes.

below are some ways i tried using log.debug and the errors

groovy.lang.MissingPropertyException: No such property: attributename for class: physicalgraph.device.cache.DeviceDTO

  • device.attributename

Returns null

  • attributenameState

java.lang.NullPointerException: Cannot get property ‘attributename’ on null object

  • currentState.attributename
  • currentValue.attributename

groovy.lang.MissingMethodException: No signature of method: script.currentState() is applicable for argument types: (java.lang.String) values: [attributename]

  • device.currentState(“someAttribute”)
  • device.latestState(“someOtherAttribute”)
  • device.currentValue(“someAttribute”)
  • device.latestValue(“someOtherAttribute”)

Yeah this is the conclusion I’m coming to too.

Frustratingly, if you add a method and bind it like so:

backgroundColor: "${getTileColour()}"

It gets evaluated but I think it’s too early, the device hasn’t been populated properly so can’t get any values from it.

Still having a mess around but it’s not looking good.

Yeah I don’t think it’s possible.

The metadata bit is just a descriptor/definition of the device and how the UI is constructed. I believe that the ${currentValue} templates are replaced on the client side. Groovy does support lazy evaluation but only in double quote strings, and obviously this is in single quotes which makes a normal Java string which will likely get returned to the client in that form.

At least that’s what I think I’m seeing :smiley:

Someone will probably come along who’s been using this stuff for a long time and say “Well, duh!”.

I know how to do this for a tile based on an attribute that said what the thermostat was currently doing (heating, cooling, idle) but I don’t know how you’d display the temperature within that tile as you’re not within the correct context.

I have a modified CT100 handler that I’m using that changes the multiAttribute tile color based on an attribute value. It’s not exactly what you’re looking for, but it might be a starting point. Try something like this:

multiAttributeTile(name: "richthermostat", type: "thermostat", width: 6, height: 4){
        	tileAttribute("device.temperature", key: "PRIMARY_CONTROL") {
            	attributeState("temperature", label:'${currentValue}°', backgroundColors:[
					[value: 32, color: "#153591"],
					[value: 44, color: "#1e9cbb"],
					[value: 59, color: "#90d2a7"],
					[value: 74, color: "#44b621"],
					[value: 84, color: "#f1d801"],
					[value: 92, color: "#d04e00"],
					[value: 98, color: "#bc2323"]
				]
			)
        	}

That’s how you would need to do a color gradient. I’m not sure if it can be adapted into something more dynamic like what you’re suggesting, but maybe if you get creative with your use of attributes…

Hi Mitch

thanks for your input.
Unfortunately I cannot change the color depending on the value. I had originally a similar code in place but I only want to be able to visually understand is the thermostat is on.

cheers
f

Not sure if you have already picked this up, but for completeness (and others searching the forums for answers) I think this issue is now resolved in the documentation.

multiAttributeTile(name:“thermostatMulti”, type:“thermostat”, width:6, height:4) {
tileAttribute(“device.temperature”, key: “PRIMARY_CONTROL”) {
attributeState(“default”, label:‘${currentValue}’, unit:“dF”)
}
tileAttribute(“device.temperature”, key: “VALUE_CONTROL”) {
attributeState(“default”, action: “setTemperature”)
}
tileAttribute(“device.humidity”, key: “SECONDARY_CONTROL”) {
attributeState(“default”, label:‘${currentValue}%’, unit:“%”)
}
tileAttribute(“device.thermostatOperatingState”, key: “OPERATING_STATE”) {
attributeState(“idle”, backgroundColor:“#44b621”)
attributeState(“heating”, backgroundColor:“#ffa81e”)
attributeState(“cooling”, backgroundColor:“#269bd2”)

Multiattribute tiles documentation

thanks David, will give it a try tonight and let you know
cheers
fab