With Blockly, I made a rule to control my water heater, when I have excessive electrical power.
When consuming from the grid, it should switch off. Setpoint OGwHzg is actual 20W.
When the solar power is better than -350W, (UGwHzg), it should switch the heater on. Actually, the rule switches the heater off as desired, but switches on already at -10W.
TestPowerItem is a Smartmeter dummy. A negative value injects to the grid, a positive consumes power.
Version is 4.1.1 running on a raspi4.
Where is the bug?
if (items.getItem('TestPowerItem').state <= items.getItem('UGwHzg').state) {
console.log(items.getItem('TestPowerItem').state);
console.log(items.getItem('UGwHzg').state);
console.log('muss anschalten ');
if (items.getItem('DummySwitch').state == 'OFF') {
console.log('schalte jetzt AN ');
items.getItem('DummySwitch').sendCommand('ON');
}
} else if (items.getItem('TestPowerItem').state >= items.getItem('OGwHzg').state) {
console.log(items.getItem('TestPowerItem').state);
console.log(items.getItem('OGwHzg').state);
console.log('muss ausschalten');
if (items.getItem('DummySwitch').state == 'ON') {
console.log('schalte jetzt AUS ');
items.getItem('DummySwitch').sendCommand('OFF');
}
}
If you want to compare numerical values, you cannot use .state, you need to use .numericState
If the item is setup as a UoM item you need to look into quantityState
Minor flaw: you are using both comparisons, <= and >= within the same if else condition.
The logging frontend fails to install. So I will describe the result: Start: DummySwitch is OFF, UGwHzg is -350W. TestPowerItem changes from 0W to -10W. Then the 1st “if” hits. at the end the DummySwitch goes ON.
@Oliver2 : All values have the Unit W.
.numericState and .quantityState are not available in Blockly. So I tried to modify the script without the Blockly source, but openHAB prevents me from saving.
In If and Else I compare to different setpoints.(OGwHzg and UGwHzg)
2024-02-28 21:56:01.120 [INFO ] [penhab.automation.script.ui.Ruletest] - -10 W (TestPowerItem)
2024-02-28 21:56:01.121 [INFO ] [penhab.automation.script.ui.Ruletest] - -60 W (UgwHzg)
2024-02-28 21:56:01.123 [INFO ] [penhab.automation.script.ui.Ruletest] - muss anschalten
2024-02-28 21:56:01.125 [INFO ] [penhab.automation.script.ui.Ruletest] - schalte jetzt AN