Background flashing on change

I wanted to call attention to a label when the value it is displaying changes, so I made its background flash red a couple of times.

For that I used 2 variables. One is bound to the same device that provides the label content, and has the following script on ValueChanged:

App.SetDeviceAttribute(“Color1”,"#ffb3b3");
sleep(1000);
App.SetDeviceAttribute(“Color1”,"#FFC0C0C0");
sleep(1000);
App.SetDeviceAttribute(“Color1”,"#ffb3b3");
sleep(1000);
App.SetDeviceAttribute(“Color1”,"#FFC0C0C0");

Then had the Background of the label bound to Color1.

Is there a better way to do this?
Can a ValueChanged script be attached to an actual device, or a label? Or only to a variable inside a virtual device?

The other thing I’ve found is that the ValueChanged script runs once when starting the app. I guess because the value changes from null to DefaultValue.

EDIT: as a possible add-on, I’m thinking of using a 3rd variable to hold the old value. And then compare it with the current to decide if flashing in red or green depending if it went up or down (for energy monitoring).

Please try to avoid using ValueChanged events. It’s not safe to use those. They should only be visible for Legacy projects. I plan on hiding those in a future release.

Create a Virtual device in a Plugin & add a custom Color1 attribute to it. Run this script in there instead. You won’t have access to actual devices from other plugin or integrations so you probably won’t be able to accomplish what you are trying to do. At least in the current version anyway.

So the only way to currently do it is being phased out?

Correct, but aren’t you already using a Plugin? Can’t you just copy this code into that Plugin?

You should be able to do exactly what you are doing on the VirtualDevice with a Plugin Virtual Device. Why can’t you do that?

At the moment i’m not using a plugin for this. The script is attached to the ValueChanged of the virtual variable. I’m testing this flashing feature on a device read directly from Homeseer (alarm zones open), not with my energy readings (which I do read through a plugin you made for me).
I got confused when you said “you probably won’t be able to accomplish what you are trying to do”.

OK. I see. There isn’t really a good way to do this right now. Will probably require a new animation feature.

This seems to do the trick:

function onPoll() {
console.log(“polling”);
var flashColor = “”;
var response = http.get(“http://XXX.XXX/index.php/powerdisplay/getmainwatts”);
var json = response.data;
var mainDevice = plugin.Devices[“1”];

if ( XML.parse(json.mainWatt).text > mainDevice.MainWatt) flashColor = "#FFC00000";
else  if ( XML.parse(json.mainWatt).text < mainDevice.MainWatt) flashColor = "#FF96D157";

mainDevice.MainWatt = XML.parse(json.mainWatt).text;
mainDevice.PowerUsed = XML.parse(json.powerUsed).text;   
mainDevice.TotalCost = XML.parse(json.totalCost).text;   

if (flashColor == "") mainDevice.Background = "#FFC0C0C0";
else {
	mainDevice.Background = flashColor;
	sleep(1000);
	mainDevice.Background = "#FFC0C0C0";
}      

}

This method works for plugin-based devices. It would be interesting to see a solution for non-plugin ones down the road.

Thanks!