custom.js Status of other device?

Dashticz, alternative dashboard based on HTML, CSS, jQuery

Moderators: leecollings, htilburgs, robgeerts

Post Reply
TroisSix
Posts: 92
Joined: Monday 11 January 2016 9:29
Target OS: -
Domoticz version: 2022.2
Location: France
Contact:

custom.js Status of other device?

Post 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.
VM Debian - Domoticz stable v2024.7, RFXtrx433, Aeotec Z-Stick Gen5, Two APC
- rts: dio, oregon, somfy
- zwave: fibaro fgs212, aeotec smart switch, vision ZG8101
- IO Homecontrol: Velux Connexoon
- logitech harmony hub, philips hue, Pilot app.
Lokonli
Posts: 2291
Joined: Monday 29 August 2016 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: custom.js Status of other device?

Post 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
TroisSix
Posts: 92
Joined: Monday 11 January 2016 9:29
Target OS: -
Domoticz version: 2022.2
Location: France
Contact:

Re: custom.js Status of other device?

Post 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-)
VM Debian - Domoticz stable v2024.7, RFXtrx433, Aeotec Z-Stick Gen5, Two APC
- rts: dio, oregon, somfy
- zwave: fibaro fgs212, aeotec smart switch, vision ZG8101
- IO Homecontrol: Velux Connexoon
- logitech harmony hub, philips hue, Pilot app.
TroisSix
Posts: 92
Joined: Monday 11 January 2016 9:29
Target OS: -
Domoticz version: 2022.2
Location: France
Contact:

Re: custom.js Status of other device?

Post 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:
VM Debian - Domoticz stable v2024.7, RFXtrx433, Aeotec Z-Stick Gen5, Two APC
- rts: dio, oregon, somfy
- zwave: fibaro fgs212, aeotec smart switch, vision ZG8101
- IO Homecontrol: Velux Connexoon
- logitech harmony hub, philips hue, Pilot app.
Lokonli
Posts: 2291
Joined: Monday 29 August 2016 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: custom.js Status of other device?

Post 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
TroisSix
Posts: 92
Joined: Monday 11 January 2016 9:29
Target OS: -
Domoticz version: 2022.2
Location: France
Contact:

Re: custom.js Status of other device?

Post 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?
VM Debian - Domoticz stable v2024.7, RFXtrx433, Aeotec Z-Stick Gen5, Two APC
- rts: dio, oregon, somfy
- zwave: fibaro fgs212, aeotec smart switch, vision ZG8101
- IO Homecontrol: Velux Connexoon
- logitech harmony hub, philips hue, Pilot app.
Lokonli
Posts: 2291
Joined: Monday 29 August 2016 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: custom.js Status of other device?

Post 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

TroisSix
Posts: 92
Joined: Monday 11 January 2016 9:29
Target OS: -
Domoticz version: 2022.2
Location: France
Contact:

Re: custom.js Status of other device?

Post by TroisSix »

Just tested it, that's simple once we understand how to hack it, so fun, thx :D
VM Debian - Domoticz stable v2024.7, RFXtrx433, Aeotec Z-Stick Gen5, Two APC
- rts: dio, oregon, somfy
- zwave: fibaro fgs212, aeotec smart switch, vision ZG8101
- IO Homecontrol: Velux Connexoon
- logitech harmony hub, philips hue, Pilot app.
Vomera
Posts: 184
Joined: Wednesday 06 September 2017 9:11
Target OS: Linux
Domoticz version:
Contact:

Re: custom.js Status of other device?

Post 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?
Lokonli
Posts: 2291
Joined: Monday 29 August 2016 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: custom.js Status of other device?

Post 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;
}

Vomera
Posts: 184
Joined: Wednesday 06 September 2017 9:11
Target OS: Linux
Domoticz version:
Contact:

Re: custom.js Status of other device?

Post 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!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest