Calculate with custom.js

Dashticz, alternative dashboard based on HTML, CSS, jQuery

Moderators: leecollings, htilburgs, robgeerts

Post Reply
KillerBENL
Posts: 9
Joined: Thursday 09 April 2020 11:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Calculate with custom.js

Post by KillerBENL »

Hello all,

I want to know if it's possible to calculate with different block values and after that change the color of a block.
For example, if the value returned to grid(solar) is higher then used from grid, I want to change the color of the block "returned to grid" green. If I'm using more from the grid then solar is delivering I want to change the color of the block "used from grid" red.

Hope someone can help me in this....
Lokonli
Posts: 2262
Joined: Monday 29 August 2016 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Calculate with custom.js

Post by Lokonli »

Example for a P1 smart meter.

First switch to the Dashticz beta branch.

Code: Select all

git checkout beta
git pull
My P1 smart meter has domoticz ID 43. My column definition is as follows:

Code: Select all

columns[4]['blocks'] = ['43_1', ]
This will show subdevice 1 of the P1 smart meter, which is the actual energy usage.

Then add the following to custom.js:

Code: Select all

 function getStatus_43_1(block) {
     var deliv = block.device.UsageDeliv;
     if (parseFloat(deliv) > 0) {
         block.icon = 'fas fa-sun'
         block.addClass = 'deliver'
     } else {
         block.icon = 'fas fa-plug';
         block.addClass = ''
     }
 }
The previous example changes the icon and applies a custom css class ('deliver') in case you deliver power to the grid.

You can define the deliver css class in custom.css:

Code: Select all

.mh.deliver {
    border-color: green
}
This will give a green border to the block.
Annotation 2020-04-21 101826.jpg
Annotation 2020-04-21 101826.jpg (7.51 KiB) Viewed 617 times
KillerBENL
Posts: 9
Joined: Thursday 09 April 2020 11:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Calculate with custom.js

Post by KillerBENL »

Lokonli,

Thanks for the quick response. I'm already using this option.
I want to go a step further and use 2 block values to compare, the result then used to change a block.
Lokonli
Posts: 2262
Joined: Monday 29 August 2016 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Calculate with custom.js

Post by Lokonli »

KillerBENL wrote: Tuesday 21 April 2020 10:27 Lokonli,

Thanks for the quick response. I'm already using this option.
I want to go a step further and use 2 block values to compare, the result then used to change a block.
You have some js coding skills?

The function Domoticz.getAllDevices() return an array with all Domoticz devices.
With the function Dashticz.setBlock() you can update certain block settings.

You could add some code to the afterGetDevices function in custom.js, which will be called at every device update.
You have to do some debugging yourself, but it could look as follows:

Button example in CONFIG.js:

Code: Select all

blocks['custom'] = {
	title: 'not available yet ...'
}
and custom.js:

Code: Select all

function afterGetDevices() {

     var devices = Domoticz.getAllDevices();
     var device1=devices[123];
     var device2=devices[124];
     var value1=parseFloat(device1.Data);  //maybe you want to use a different field 
     var value2=parseFloat(device2.Data); //maybe you want to use a different field 
     var diff = value2 - value1;
     var newClass = '';
     if (diff>0) newClass = 'danger';
     Dashticz.setBlock('custom', { title: diff, addClass: newClass} );
 }
 
This will change the title for button 'custom' and applies a new class in case the difference between the two values is positive.

Again: I did not test this.
Let me know if you have something working, and then I'll add it as example to the documentation.
KillerBENL
Posts: 9
Joined: Thursday 09 April 2020 11:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Calculate with custom.js

Post by KillerBENL »

Thanx. It's working now

function afterGetDevices(){
var devices = Domoticz.getAllDevices();
var power=devices[108];
var value1=parseFloat(power.CounterDelivToday);
var value2=parseFloat(power.CounterToday);
var diff = value2 - value1;

if (diff > 0) { $('.block_108_2').addClass('warningred'); }
else { $('.block_108_5').addClass('warninggreen'); }
}
KillerBENL
Posts: 9
Joined: Thursday 09 April 2020 11:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Calculate with custom.js

Post by KillerBENL »

I have another question about custom.js.

Is it possible to change the value of a block? My temperature device is 0.5 degrees off because it's hanging in the wrong place. Can I correct the value somewhere in the custom.js ?
I tried "Dashticz.setBlock(178, { value, correct} );" but this does not change the data (correct is the new value)
Lokonli
Posts: 2262
Joined: Monday 29 August 2016 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Calculate with custom.js

Post by Lokonli »

Maybe it's better to adjust the temperature in Domoticz: For most temperature devices you can fill in a temperature offset in Domoticz.

In Dashticz you could also do some tricks.

If you add the getStatus_178 function to custom.js, then this function is called when device 178 is updated. This function receives 'block' as parameter, which contains 'device'.
You can modify the device data.

So this is not officially supported, may change in the future, but I think the following will work (untested ...)

Code: Select all

function getStatus_178(block) {
   var device=block.device;
    device.Temp = device.Temp+ 0.5
}
KillerBENL
Posts: 9
Joined: Thursday 09 April 2020 11:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Calculate with custom.js

Post by KillerBENL »

Thanx. I'm almost there. I've another question. (sry)
Is there a possibility to change title with text and values?
example: block.title = 'Temp is' valueX 'degrees';
lzwfkv
Posts: 37
Joined: Sunday 11 August 2019 16:54
Target OS: -
Domoticz version:
Contact:

Re: Calculate with custom.js

Post by lzwfkv »

Yes, it is possible:
block.title = 'Temp is: ' + valueX + ' degrees'
Lokonli
Posts: 2262
Joined: Monday 29 August 2016 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Calculate with custom.js

Post by Lokonli »

KillerBENL wrote: Thursday 23 April 2020 7:25 Thanx. I'm almost there. I've another question. (sry)
Is there a possibility to change title with text and values?
example: block.title = 'Temp is' valueX 'degrees';
Try:

Code: Select all

function getStatus_178(block) {
   var device=block.device;
    block.title = 'Temp is ' + device.Temp + ' degrees';
}
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest