set color based on value
Moderators: leecollings, htilburgs, robgeerts
-
- Posts: 40
- Joined: Sunday 15 April 2018 13:00
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 3.8153
- Contact:
set color based on value
I searched the forum, but cannot find the right answer.
I want to change the color of a icon when a value of the sensor is between 2 values.
For example, if the value is above 85 it must be green, if it is between 75 and 85 orange, and below 75 it must be red.
I can find some examples with on-off states, but not multiple colors on pre-defined values and ranges.
Some example would be nice.
I want to change the color of a icon when a value of the sensor is between 2 values.
For example, if the value is above 85 it must be green, if it is between 75 and 85 orange, and below 75 it must be red.
I can find some examples with on-off states, but not multiple colors on pre-defined values and ranges.
Some example would be nice.
-
- Posts: 2287
- Joined: Monday 29 August 2016 22:40
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: set color based on value
Below an example of a combined temperature/humidity device with index 56.
Define the styles you want to apply in custom.css:
In custom.js define the function afterGetDevices() as follows:
If you have just a single value device, you can use block_56 instead of block_56_1.
Depending on the kind of device you are using you may change alldevices[56].Temp to something applicable for your device.
Define the styles you want to apply in custom.css:
Code: Select all
.warningred {
color: red !important;
}
.warningorange {
color: orange !important;
}
.warninggreen {
color: green !important;
}
Code: Select all
function afterGetDevices(){
if ((alldevices[56].Temp>=75) && (alldevices[56].Temp<85)) {
$('.block_56_1 .col-icon').addClass('warningorange');
}
if (alldevices[56].Temp<75) {
$('.block_56_1 .col-icon').addClass('warninggreen');
}
if (alldevices[56].Temp>=85) {
$('.block_56_1 .col-icon').addClass('warningred');
}
}
Depending on the kind of device you are using you may change alldevices[56].Temp to something applicable for your device.
- HansieNL
- Posts: 964
- Joined: Monday 28 September 2015 15:13
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: set color based on value
I have same kind of question. I have a text block and want to change it depending on message. This is what I have done so far:
In custom.js:
In custom.css:
The title and value text change color, but the icon color (png image) does not change.
Please advice how to solve this.
Thanks in advance.
In custom.js:
Code: Select all
//Buienradar
function getStatus_345(idx,value,device){
if(device['Data']=="Voorlopig droog") {
$('div.block_345').addClass('off-stat');
$('.block_345 .col-icon').addClass('off-ico');
}
else {
$('div.block_345').removeClass('off-stat');
$('.block_345 .col-icon').removeClass('off-ico');
}
}
Code: Select all
.off-stat {
color: #6A6A6A;
}
.off-ico {
opacity: 0.4 !important;
}
Please advice how to solve this.
Thanks in advance.
Blah blah blah
-
- Posts: 2287
- Joined: Monday 29 August 2016 22:40
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: set color based on value
Dashticz regenerates the icon (and data part) of a block after calling the getStatus function. As a side-effect the off-ico class you applied disappears as well. (block_345 itself doesnt get recreated so the off-stat class persists)HansieNL wrote: ↑Sunday 19 May 2019 16:20 I have same kind of question. I have a text block and want to change it depending on message. This is what I have done so far:
In custom.js:In custom.css:Code: Select all
//Buienradar function getStatus_345(idx,value,device){ if(device['Data']=="Voorlopig droog") { $('div.block_345').addClass('off-stat'); $('.block_345 .col-icon').addClass('off-ico'); } else { $('div.block_345').removeClass('off-stat'); $('.block_345 .col-icon').removeClass('off-ico'); } }
The title and value text change color, but the icon color (png image) does not change.Code: Select all
.off-stat { color: #6A6A6A; } .off-ico { opacity: 0.4 !important; }
Please advice how to solve this.
Thanks in advance.
The afterGetDevices function gets called after creating the icon part, so you should use that one if you want to change the icon. If you need an example let me know.
- HansieNL
- Posts: 964
- Joined: Monday 28 September 2015 15:13
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: set color based on value
@Lokonli: Thanks for the help. This is code is working ok:Lokonli wrote: ↑Sunday 19 May 2019 16:48 Dashticz regenerates the icon (and data part) of a block after calling the getStatus function. As a side-effect the off-ico class you applied disappears as well. (block_345 itself doesnt get recreated so the off-stat class persists)
The afterGetDevices function gets called after creating the icon part, so you should use that one if you want to change the icon. If you need an example let me know.
Code: Select all
//Buienradar
function afterGetDevices(){
if (alldevices[345].Data == 'Voorlopig droog') {
$('.block_345 .col-icon').addClass('off-ico');
$('.block_345 .col-data').addClass('off-stat');
}
else {
$('.block_345 .col-icon').removeClass('off-ico');
$('.block_345 .col-data').removeClass('off-stat');
}
}
Blah blah blah
- HansieNL
- Posts: 964
- Joined: Monday 28 September 2015 15:13
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: set color based on value
@Lokonli: I wanna change the off color for the blocks Kodi Media Server and StreamPlayer too if possible. I don't have an idea how to fix this. Can you help to fix this please. Thanks in advance.

@Lokonli: Kodi Media Server off color already fixed by using a virtual on/of switch.
Would be great if StreamPlayer can have off color when not playing.

@Lokonli: Kodi Media Server off color already fixed by using a virtual on/of switch.
Would be great if StreamPlayer can have off color when not playing.
Blah blah blah
- HansieNL
- Posts: 964
- Joined: Monday 28 September 2015 15:13
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: set color based on value
Someone who knows how to set Streamplayer off color?
This is what I already tried, but that doesn't work:
This is what I already tried, but that doesn't work:
Code: Select all
if (alldevices[streamplayer].audio != 'play') {
$('div.streamplayer').addClass('pause-stat');
}
else {
$('div.streamplayer').removeClass('pause-stat');
}
Blah blah blah
-
- Posts: 40
- Joined: Sunday 15 April 2018 13:00
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 3.8153
- Contact:
Re: set color based on value
Hi Lokonli,Lokonli wrote: ↑Saturday 18 May 2019 17:08 Below an example of a combined temperature/humidity device with index 56.
Define the styles you want to apply in custom.css:In custom.js define the function afterGetDevices() as follows:Code: Select all
.warningred { color: red !important; } .warningorange { color: orange !important; } .warninggreen { color: green !important; }
If you have just a single value device, you can use block_56 instead of block_56_1.Code: Select all
function afterGetDevices(){ if ((alldevices[56].Temp>=75) && (alldevices[56].Temp<85)) { $('.block_56_1 .col-icon').addClass('warningorange'); } if (alldevices[56].Temp<75) { $('.block_56_1 .col-icon').addClass('warninggreen'); } if (alldevices[56].Temp>=85) { $('.block_56_1 .col-icon').addClass('warningred'); } }
Depending on the kind of device you are using you may change alldevices[56].Temp to something applicable for your device.
Thanks for the code. I did not find the time yet to implement and test it. But would like to let you know I I've seen it and will try it asap.
-
- Posts: 2287
- Joined: Monday 29 August 2016 22:40
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: set color based on value
The streamplayer block is not a Domoticz device, but functionality from Dashticz. Changing the appearance of the block via custom.js requires quite some hacking.HansieNL wrote: ↑Saturday 25 May 2019 20:55 Someone who knows how to set Streamplayer off color?
This is what I already tried, but that doesn't work:Code: Select all
if (alldevices[streamplayer].audio != 'play') { $('div.streamplayer').addClass('pause-stat'); } else { $('div.streamplayer').removeClass('pause-stat'); }
A cleaner solution is to add the playing state as a class to the streamplayer block. Then the styling can be changed easily via custom.css.
I will make the change. It will be merged in the next beta version of Dashticz.
- HansieNL
- Posts: 964
- Joined: Monday 28 September 2015 15:13
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
-
- Posts: 59
- Joined: Sunday 28 July 2019 22:59
- Target OS: NAS (Synology & others)
- Domoticz version:
- Contact:
Re: set color based on value
Hi all,
I want a UV index alert sensor to get a color according to its value. For instance, if the UV is >= 6 I want the title of the block to be red. I tried several things but have not succeeded yet. Here's the code I added to the custom.js:
In the CSS file I added:
Domoticz gives me the following info about the sensor:
Any ideas where I'm going wrong?
I want a UV index alert sensor to get a color according to its value. For instance, if the UV is >= 6 I want the title of the block to be red. I tried several things but have not succeeded yet. Here's the code I added to the custom.js:
Code: Select all
function afterGetDevices(){
// Warnings UV index
if (alldevices[165].Data>=6) {
$('.block_165' .title).addClass('warningred');
}
if ((alldevices[165].Data >= 5) && (alldevices[165].Data<6)) {
$('.block_165 .title').addClass('warningorange');
}
if ((alldevices[165].Data >= 4) && (alldevices[165].Data<5)) {
$('.block_165 .title').addClass('warningyellow');
}
if (alldevices[165].Data <4) {
$('.block_165 .title').addClass('warninggreen');
}
}
Code: Select all
.warningred {
color: red !important;
}
.warningorange {
color: orange !important;
}
.warningyellow {
color: yellow !important;
}
.warninggreen {
color: green !important;
}
Code: Select all
{
"AddjMulti" : 1.0,
"AddjMulti2" : 1.0,
"AddjValue" : 0.0,
"AddjValue2" : 0.0,
"BatteryLevel" : 255,
"CustomImage" : 0,
"Data" : "7.4 UVI",
"Description" : "",
"Favorite" : 0,
"HardwareID" : 28,
"HardwareName" : "UV index",
"HardwareType" : "OpenUV",
"HardwareTypeVal" : 94,
"HaveTimeout" : false,
"ID" : "001C0003",
"LastUpdate" : "2020-05-22 08:32:53",
"Level" : 2,
"Name" : "UV index - UV Max",
"Notifications" : "false",
"PlanID" : "0",
"PlanIDs" :
[
0
],
"Protected" : false,
"ShowNotifications" : true,
"SignalLevel" : "-",
"SubType" : "Alert",
"Timers" : "false",
"Type" : "General",
"TypeImg" : "Alert",
"Unit" : 3,
"Used" : 1,
"XOffset" : "0",
"YOffset" : "0",
"idx" : "165"
},
-
- Posts: 2287
- Joined: Monday 29 August 2016 22:40
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: set color based on value
There is a small typo in the beginning:
should be:
Further, Data is a string (=text). You first have to create a number from it via:
and then in the if statements you can use:
The only disadvantage is that this is not very efficient: the code gets called at every device update. (but probably not really a problem)
You could rewrite it to a getStatus_165() function. See some examples in the documentation (beta version!)
Code: Select all
$('.block_165' .title).addClass('warningred');
Code: Select all
$('.block_165 .title').addClass('warningred');
Code: Select all
var value = parseFloat(alldevices[165].Data);
Code: Select all
if (value>=6) {
You could rewrite it to a getStatus_165() function. See some examples in the documentation (beta version!)
-
- Posts: 59
- Joined: Sunday 28 July 2019 22:59
- Target OS: NAS (Synology & others)
- Domoticz version:
- Contact:
Re: set color based on value
Ouch! A typo...that's painfulLokonli wrote: ↑Saturday 23 May 2020 23:02 There is a small typo in the beginning:should be:Code: Select all
$('.block_165' .title).addClass('warningred');
Further, Data is a string (=text). You first have to create a number from it via:Code: Select all
$('.block_165 .title').addClass('warningred');
and then in the if statements you can use:Code: Select all
var value = parseFloat(alldevices[165].Data);
The only disadvantage is that this is not very efficient: the code gets called at every device update. (but probably not really a problem)Code: Select all
if (value>=6) {
You could rewrite it to a getStatus_165() function. See some examples in the documentation (beta version!)


-
- Posts: 89
- Joined: Friday 09 October 2015 17:40
- Target OS: Linux
- Domoticz version:
- Contact:
Re: set color based on value
What if I want to change the background of the block based on value?
Do I define those in custom.css and if how?
(On)
(Off)
Do I define those in custom.css and if how?
Code: Select all
div.mh.transbg.block_275.col-xs-4.on
Code: Select all
div.mh.transbg.block_275.col-xs-4.off
-
- Posts: 2287
- Joined: Monday 29 August 2016 22:40
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: set color based on value
yes, in custom.css. Try:
Code: Select all
.block_275.on {
background-color: red !important
}
.block_275.off {
background-color: green !important
}
-
- Posts: 2287
- Joined: Monday 29 August 2016 22:40
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: set color based on value
This is only for the on/off state of switches.
If you want to change the styling based on the value (like temperature) you have to use custom.js
See the documentation for some examples:
https://dashticz.readthedocs.io/en/beta ... -idx-block
If you want to change the styling based on the value (like temperature) you have to use custom.js
See the documentation for some examples:
https://dashticz.readthedocs.io/en/beta ... -idx-block
-
- Posts: 89
- Joined: Friday 09 October 2015 17:40
- Target OS: Linux
- Domoticz version:
- Contact:
Re: set color based on value
The custom.css code did the trick! Small minor question: Can it be done for all blocks in custom.css instead of defining it for each individual block?
-
- Posts: 2287
- Joined: Monday 29 August 2016 22:40
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: set color based on value
'.block_275' is indeed a specific block selector.Ierlandfan wrote: ↑Wednesday 27 May 2020 17:40 The custom.css code did the trick! Small minor question: Can it be done for all blocks in custom.css instead of defining it for each individual block?
Luckily all Domoticz block also have the class '.mh' attached, so the following should work if you want to apply the styling to all blocks at once:
Code: Select all
.mh.on {
background-color: red !important
}
.mh.off {
background-color: green !important
}
-
- Posts: 30
- Joined: Sunday 12 January 2020 19:06
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: set color based on value
I want to change the color of IDX 462 based on the status of IDX 460
but
does not work, how should this be programmed in custom.js?
but
Code: Select all
function getStatus_460(idx,value,device){
if(device['Data']=='Off'){
$('div.block_462').removeClass('warningred');
}
else {
$('div.block_462').addClass('warningred');
}
}
-
- Posts: 2287
- Joined: Monday 29 August 2016 22:40
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: set color based on value
https://dashticz.readthedocs.io/en/mast ... -idx-blockVondee wrote: ↑Tuesday 17 November 2020 20:18 I want to change the color of IDX 462 based on the status of IDX 460
butdoes not work, how should this be programmed in custom.js?Code: Select all
function getStatus_460(idx,value,device){ if(device['Data']=='Off'){ $('div.block_462').removeClass('warningred'); } else { $('div.block_462').addClass('warningred'); } }
https://dashticz.readthedocs.io/en/mast ... ther-block
Who is online
Users browsing this forum: No registered users and 1 guest