Page 1 of 1

custom.js Status of other device?

Posted: Friday 30 April 2021 12:07
by TroisSix
Hello

Could someone help me to get the value/state/data of other device in a function getStatus_Block like :

Code: Select all

function getStatus_BlindTest(block){
    var device = block.device;
    if(device['Data']=='Closed') { /* blind closed */
        Dashticz.setBlock('BlindTest', {textOff: 'blind closed', imageOff: 'perso/blind_closed.png' });
    } else if( device['Data']=='Open' ) { /* blind opened */
        if( blocks['WindowTest']['Data']=='On' ) { /* blind opened and window opened */
            Dashticz.setBlock('BlindTest', {textOn: 'window opened', imageOn: 'perso/window_opened.png' });
        } else { /* blind opened and window closed */
            Dashticz.setBlock('BlindTest', {textOn: 'blind opened', imageOn: 'perso/blind_opened.png' });
        }
    }
}
This device WindowTest is a simple switch on/off, and I tried without success:
- blocks['WindowTest']['Data'], blocks['WindowTest'].Data,
- blocks['WindowTest']['Value'], blocks['WindowTest'].value,
- blocks['WindowTest']['State'], blocks['WindowTest'].state,
- blocks['WindowTest']['Status'], blocks['WindowTest'].status,
but it gives me all the time an 'undefined' :(

I don't think I'm too far cause I can have the blocks['WindowTest'].title and blocks['WindowTest'].idx ;)

Thanks for your help.

Re: custom.js Status of other device?

Posted: Friday 30 April 2021 12:19
by Lokonli
Now the hacking really starts :)

Dashticz caches all device info (and variable info) from Domoticz. As an example you can use the code below to get the info for device 123:

Code: Select all

  var alldevices = Domoticz.getAllDevices();
  var device123 = alldevices[123]; //To get device 123

Re: custom.js Status of other device?

Posted: Friday 30 April 2021 12:50
by TroisSix
Yessss, that's in fact so simple Sander, excellent :D
Share this trick in your documentation, that's really useful for the one who like to hack things this way 8-)

Re: custom.js Status of other device?

Posted: Friday 30 April 2021 14:14
by TroisSix
One last question: why do I have to mandatory display the "other blocks" in the pages if I want to be able to use it in the custom.js?
For example, I don't want to display the blocks['WindowTest'], but if I don't, the custom.js isn't able to get its value :roll:

Re: custom.js Status of other device?

Posted: Friday 30 April 2021 14:18
by Lokonli
In the past you had to add a block to the screen if you want to trigger an action on device change.

Currently it's not really needed anymore. You can trigger an action by using the deviceHook function. See:
https://dashticz.readthedocs.io/en/mast ... evice-hook

Re: custom.js Status of other device?

Posted: Friday 30 April 2021 17:09
by TroisSix
Indeed, nice functionality, "on every device update the device hook is called first", but that's not totally clear for me :lol:

If I hide my device, and do something like:

Code: Select all

function deviceHook(device) {
    if (device.idx==123) {
        device.deviceStatus = device.Data == 'On' ? 'On':'Off';
    }
}
It will awake this device, and my functions in custom.js will be able to see this value?

Re: custom.js Status of other device?

Posted: Friday 30 April 2021 17:50
by Lokonli
Short answer: yes

Longer answer:
If Dashticz receives a device update from Domoticz, then first the device hook is called. You may modify the device data if needed.
After that, all blocks that are subscribed to this device will be updated.
As part of the update process normal blocks call their getStatus function in custom.css, update the block, and then call the getStatus function again.

I hope it's clearer now.

Verstuurd vanaf mijn AC2003 met Tapatalk


Re: custom.js Status of other device?

Posted: Tuesday 04 May 2021 17:55
by TroisSix
Just tested it, that's simple once we understand how to hack it, so fun, thx :D

Re: custom.js Status of other device?

Posted: Saturday 15 May 2021 19:38
by Vomera
I don't know if it's related to this subject, but i have also problem with status of an other object.
When the website loads or when there is a new update from the block the colors are right, but after a whole minute the block will go to its normal state (no colors)

See the screenshots below with also the code:

After website reload / new update from block like 0.1 mm to 0.2 mm.
Image

Image


After the whole minute, maybe the refresh of domoticz, no new update for the block (see screenshot of the block, now 0.0 because there is no rain anymore when i made the screenshot :P)

Image

Image

Image

custom.css

Code: Select all

.nultottwee {
	background: rgba(228, 229, 255,1.0) !important;
    background-clip: padding-box;
	color: black;
}
custom.js
Only to change the block icon

Code: Select all


//  regen
function getStatus_3962(block){
   var idx = block.idx;
   var device = block.device;
   if(device['Data']!="Voorlopig blijft het droog!"){
   block.icon = 'fas fa-cloud-rain'
   
    
   }
   else {
	block.icon = 'fas fa-tint-slash'
 }
}
Get the status of 9450, its a text value in domoticz and change the block to its color

Code: Select all

// geeft regenmelding een kleur op basis van aantal mm per uur
	function getStatus_9450(block){
	var Data = block.device.Data;
	var idx = block.idx;
    var device = block.device;
	
	
   if(parseFloat(Data) > 100){	
     Dashticz.setBlock('3962', {addClass: 'bovendehonderd'});  
      
   }
   else if(parseFloat(Data) >10){
     Dashticz.setBlock('3962', {addClass: 'tientothonderd'});
   }   
   else if(parseFloat(Data) >5){
      Dashticz.setBlock('3962', {addClass: 'vijftottien'});    
   }     
   else if(parseFloat(Data) >2){
        Dashticz.setBlock('3962', {addClass: 'tweetotvijf'});   
   }
   
   else if(parseFloat(Data) >0){
      Dashticz.setBlock('3962', {addClass: 'nultottwee'});   
   }
    
   else {
     Dashticz.setBlock('3962', {addClass: 'none'}); 
   }
}

I put this script also between the "after get devices" code, but that didnt help.

Code: Select all

function afterGetDevices(){
	
	var device9450 = alldevices[9450]; 
	var device3962 = alldevices[3962]; 
	
	
	function getStatus_9450(block){
	var Data = block.device.Data;
	var idx = block.idx;
    var device = block.device;
	
	
   if(parseFloat(Data) > 100){	
     Dashticz.setBlock('3962', {addClass: 'bovendehonderd'});  
      
   }
   else if(parseFloat(Data) >10){
     Dashticz.setBlock('3962', {addClass: 'tientothonderd'});
   }   
   else if(parseFloat(Data) >5){
      Dashticz.setBlock('3962', {addClass: 'vijftottien'});    
   }     
   else if(parseFloat(Data) >2){
        Dashticz.setBlock('3962', {addClass: 'tweetotvijf'});   
   }
   
   else if(parseFloat(Data) >0){
      Dashticz.setBlock('3962', {addClass: 'nultottwee'});   
   }
    
   else {
     Dashticz.setBlock('3962', {addClass: 'none'}); 
   }


}	// einde afterGetDevices





Did i miss something in the code?

Re: custom.js Status of other device?

Posted: Saturday 15 May 2021 20:43
by Lokonli
I expect the addClass setting for device 3962 is reinitialized when device 3962 is updated.

You could request the data for device 9450 from the function getStatus_3962.

I would not put the functions within the afterGetDevices function: The idea is that the getStatus functions get called when the device is updated.

So something like this:

Code: Select all


//  regen
function getStatus_3962(block){
   var idx = block.idx;
   var device = block.device;
   if(device['Data']!="Voorlopig blijft het droog!"){
   block.icon = 'fas fa-cloud-rain'
   
    
   }
   else {
	block.icon = 'fas fa-tint-slash'
 }

// Get the data from device 9450
   var device9450 = Domoticz.getAllDevices()[9450];

  var Data = device9450.Data;
   var addClass;
   if(parseFloat(Data) > 100){	
     addClass= 'bovendehonderd';  
   }
   else if(parseFloat(Data) >10){
     addClass='tientothonderd';
   }   
   else if(parseFloat(Data) >5){
      addClass='vijftottien';    
   }     
   else if(parseFloat(Data) >2){
        addClass='tweetotvijf';   
   }
   else if(parseFloat(Data) >0){
      addClass='nultottwee';   
   }
    
   else {
     addClass='none'; 
   }
   block.addClass = addClass;
}


Re: custom.js Status of other device?

Posted: Sunday 16 May 2021 11:41
by Vomera
Lokonli wrote: Saturday 15 May 2021 20:43 I expect the addClass setting for device 3962 is reinitialized when device 3962 is updated.

You could request the data for device 9450 from the function getStatus_3962.

I would not put the functions within the afterGetDevices function: The idea is that the getStatus functions get called when the device is updated.

So something like this:

Code: Select all


//  regen
function getStatus_3962(block){
   var idx = block.idx;
   var device = block.device;
   if(device['Data']!="Voorlopig blijft het droog!"){
   block.icon = 'fas fa-cloud-rain'
   
    
   }
   else {
	block.icon = 'fas fa-tint-slash'
 }

// Get the data from device 9450
   var device9450 = Domoticz.getAllDevices()[9450];

  var Data = device9450.Data;
   var addClass;
   if(parseFloat(Data) > 100){	
     addClass= 'bovendehonderd';  
   }
   else if(parseFloat(Data) >10){
     addClass='tientothonderd';
   }   
   else if(parseFloat(Data) >5){
      addClass='vijftottien';    
   }     
   else if(parseFloat(Data) >2){
        addClass='tweetotvijf';   
   }
   else if(parseFloat(Data) >0){
      addClass='nultottwee';   
   }
    
   else {
     addClass='none'; 
   }
   block.addClass = addClass;
}

Hmm that makes more sense, I didn't think of that. I change it.. Let the rain come!