Calculate with custom.js
Moderators: leecollings, htilburgs, robgeerts
-
- Posts: 9
- Joined: Thursday 09 April 2020 11:18
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Calculate with custom.js
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....
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....
-
- Posts: 2262
- Joined: Monday 29 August 2016 22:40
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Calculate with custom.js
Example for a P1 smart meter.
First switch to the Dashticz beta branch.
My P1 smart meter has domoticz ID 43. My column definition is as follows:
This will show subdevice 1 of the P1 smart meter, which is the actual energy usage.
Then add the following to custom.js:
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:
This will give a green border to the block.
First switch to the Dashticz beta branch.
Code: Select all
git checkout beta
git pull
Code: Select all
columns[4]['blocks'] = ['43_1', ]
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 = ''
}
}
You can define the deliver css class in custom.css:
Code: Select all
.mh.deliver {
border-color: green
}
-
- Posts: 9
- Joined: Thursday 09 April 2020 11:18
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Calculate with custom.js
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.
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.
-
- Posts: 2262
- Joined: Monday 29 August 2016 22:40
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Calculate with custom.js
You have some js coding skills?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.
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 ...'
}
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} );
}
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.
-
- Posts: 9
- Joined: Thursday 09 April 2020 11:18
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Calculate with custom.js
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'); }
}
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'); }
}
-
- Posts: 9
- Joined: Thursday 09 April 2020 11:18
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Calculate with custom.js
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)
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)
-
- Posts: 2262
- Joined: Monday 29 August 2016 22:40
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Calculate with custom.js
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 ...)
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
}
-
- Posts: 9
- Joined: Thursday 09 April 2020 11:18
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Calculate with custom.js
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';
Is there a possibility to change title with text and values?
example: block.title = 'Temp is' valueX 'degrees';
Re: Calculate with custom.js
Yes, it is possible:
block.title = 'Temp is: ' + valueX + ' degrees'
block.title = 'Temp is: ' + valueX + ' degrees'
-
- Posts: 2262
- Joined: Monday 29 August 2016 22:40
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Calculate with custom.js
Try: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';
Code: Select all
function getStatus_178(block) {
var device=block.device;
block.title = 'Temp is ' + device.Temp + ' degrees';
}
Who is online
Users browsing this forum: No registered users and 1 guest