CSS - Change specific block backround color depending on block state Topic is solved

Dashticz, alternative dashboard based on HTML, CSS, jQuery

Moderators: leecollings, htilburgs, robgeerts

Post Reply
sammyke007
Posts: 204
Joined: Monday 08 May 2017 20:48
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: Belgium
Contact:

CSS - Change specific block backround color depending on block state

Post by sammyke007 »

Hi

One of my million questions...
I would like to change a specific block background color (or title color) depending on the state it is in.
I have a block that tells me the Domoticz alarm state (Armed Home / Armed Away or Disarmed).

I know that it's possible to change styling, but I don't have enough CSS skills to do it myself.
Maybe the solution would be a nice extra for the wiki!

If block240 tells me "Disarmed", change that block background color, elseif ... but obviuously, that's the wrong language :D

I was thinking about something like this:

Code: Select all

function getStatus_240(block) {
     var status = block.device.data;
     if (status = 'Disarmed') {
         block.background-color = red !important;
     } 
 }
or something like this:

Code: Select all

function afterGetDevices() {

     var devices = Domoticz.getAllDevices();
     var device1=devices[240];
     var value1=(device1.Data); 
     var newClass = '';
     if (value1=Disarmed) newClass = 'danger';
     Dashticz.setBlock('custom', { title: diff, addClass: newClass} );
 }
sammyke007
Posts: 204
Joined: Monday 08 May 2017 20:48
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: Belgium
Contact:

Re: CSS - Change specific block backround color depending on block state

Post by sammyke007 »

After a lot of headache...

custom.js:

Code: Select all

function afterGetDevices(){
    if (alldevices[240].Data == 'Disarmed') {
		$("div.block_240").addClass('warninggreen');
	}
	if (alldevices[240].Data == 'Armed Home') {
		$("div.block_240").addClass('warningorange');
	}
	if (alldevices[240].Data == 'Armed Away') {
		$("div.block_240").addClass('warningred');
	}
}
custom.css:

Code: Select all

.warningred {
  background-color: red !important;
}
.warningorange {
  background-color: orange !important;
}
.warninggreen {
  background-color: green !important;
}
toro
Posts: 47
Joined: Wednesday 09 August 2017 23:05
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: CSS - Change specific block backround color depending on block state

Post by toro »

Yes, that will work.
But the function afterGetDevices is triggered everytime a Domoticz device changes. So whatever device changes, this function runs.
I think it's better to run your script only if the device itself changes.

Therefor you could use

Code: Select all

function getStatus_240(block){
	if (block.device.Data == 'Disarmed') {
		block.addClass = 'warninggreen';
	}
	else if (block.device.Data == 'Armed Home') {
		block.addClass = 'warningorange';
	}
	else {
		block.addClass = 'warningred';
	}
}
User avatar
clinkadink
Posts: 417
Joined: Tuesday 31 December 2019 1:15
Target OS: Linux
Domoticz version: 2020.2
Location: Swindon, UK
Contact:

Re: CSS - Change specific block backround color depending on block state

Post by clinkadink »

Same as Toro, but using ternary syntax ;)

Code: Select all

function getStatus_240(block) {
  var c = block.device.Data === 'Disarmed' ?
      'warninggreen' :
      block.device.Data == 'Armed Home' ?
      'warningorange' :
      'warningred';
  block.addClass = c;
}
"UI is the saddle, the stirrups, & the reins. UX is the feeling you get being able to ride the horse."
sammyke007
Posts: 204
Joined: Monday 08 May 2017 20:48
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: Belgium
Contact:

Re: CSS - Change specific block backround color depending on block state

Post by sammyke007 »

Tnx everyone!
It's working great. The problem is that I can understand what every line does in a script, but writing it myself... It's the same as with my French :-D
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest