Ok, that seems to find the item. Now it throws another error
(I’m testing the script by updating the temperature value manually in MQTT explorer)
Script execution of rule with UID 'thermostat' failed: org.graalvm.polyglot.PolyglotException: TypeError: invokeMember (getItem) on org.openhab.core.internal.items.ItemRegistryImpl@1a5a5b63 failed due to: Cannot convert '[object Object]'(language: JavaScript, type: O) to Java type 'java.lang.String': Invalid or lossy primitive coercion.
EDIT:
I added a few lines of logging, and the script reads the state correctly.
That means they are correctly passed onto the second script.
// Log the state of KNX items
var valveState = heatingValve.state.toString();
var contactState = heatingContact.state.toString();
// Log the states
console.log("Heating Valve State: " + valveState);
console.log("Heating Contact State: " + contactState);
INFO org.openhab.automation.script.ui.thermostat Heating Valve State: OFF
INFO org.openhab.automation.script.ui.thermostat Heating Contact State: OFF
if (hvacMode == "COMFORT") {
if (temperature < setpointComfort){
items.getItem(heatingValve).sendCommand("ON");
items.getItem(heatingContact).sendCommand("ON");
console.log("Temperature is below the comfort level. Heating valve turned ON.");
}
else if (temperature > (setpointComfort + hysteresis)) {
items.getItem(heatingValve).sendCommand("OFF");
items.getItem(heatingContact).sendCommand("OFF");
console.log("Temperature is above the comfort range. Heating valve turned OFF.");
}
}
else if (hvacMode == "STANDBY") {
if (temperature < setpointStandby){
items.getItem(heatingValve).sendCommand("ON");
items.getItem(heatingContact).sendCommand("ON");
console.log("Temperature is below the standby level. Heating valve turned ON.");
}
else if (temperature > (setpointStandby + hysteresis)) {
items.getItem(heatingValve).sendCommand("OFF");
items.getItem(heatingContact).sendCommand("OFF");
console.log("Temperature is above the standby range. Heating valve turned OFF.");
}
}
else {}
Sorry for all my questions i’m still getting used to the language.
As mentioned, in your script var heatingValve should already by defined, although I prefer to access keys via ctx[‘key’] so I keep track of where they’re coming from. In this case, you’re overwriting var heatingValve with a non existent key
I integrated the script and the rules into my production environment, but there is something wrong.
The script keeps sending “ON” and “OFF” to the knx devices, even when the state is already on or off.
Also i can’t access my rules/scripts/schedule or console log from the UI…
It sounds like it is in a loop and slowing the server down.
I have done that before as well…annoying.
I restart the openhab and quickly disable the rule.
You have to be quick. You can do it via the web page or the console-cli by running:
rules:disable thermostat
You probably can get to the console-cli but it might be really slow (depending on what you are running it on).
The script didn’t appear to be in a loop. The load on the Raspberry pi was only 20-30% CPU usage. Things and Items still worked and console was accessible.
The pages became avaiIable again after i deleted everything in the cache and tmp folder, and restarted the docker container. It seems like there’s a bug somewhere
I adjusted the script, and now it’s sending the command only on state change:
//console.log("running thermostat script")
var setPoint = 18;
if (hvacMode == "COMFORT") {
setPoint = setpointComfort;
}
else if (hvacMode == "STANDBY") {
setPoint = setpointStandby;
}
else {}
if (temperature < setPoint){
if(heatingValve.state == "OFF"){
heatingValve.sendCommand('ON');
}
if(heatingContact.state =="OFF"){
heatingContact.sendCommand('ON');
}
//console.log("Temperature is below the comfort level. Heating valve turned ON.");
}
else if (temperature > (setPoint + hysteresis)) {
if(heatingValve.state == "ON"){
heatingValve.sendCommand('OFF');
}
if(heatingContact.state =="ON"){
heatingContact.sendCommand('OFF');
}
//console.log("Temperature is above the comfort range. Heating valve turned OFF.");
}
That is indeed much cleaner
I adjusted all the rules and the script:
var temp = parseFloat(items["HvacLiving_LivingTemperature"].state);
var mode = items["HvacLiving_LivingHVACmode"].state.toString();
var standbytemp = parseFloat(items["HvacLiving_LivingStandbyTemperature"].state);
var comforttemp = parseFloat(items["HvacLiving_LivingComfortTemperature"].state);
var valve = "KNX_Device_DO3_cv_valve_ground_floor";
var contact = "KNX_Device_DO3_cv_heat";
var parameter = {
'temperature': temp,
'hvacMode': mode,
'setpointComfort': comforttemp,
'setpointStandby': standbytemp,
'heatingValve': valve,
'heatingContact': contact,
'hysteresis': 0.3
};
rules.runRule('thermostat', parameter);
var setPoint = 18;
if (hvacMode == "COMFORT") {
setPoint = setpointComfort;
}
else if (hvacMode == "STANDBY") {
setPoint = setpointStandby;
}
else {}
var sNewState = (temperature < setPoint)?"ON":"";
if (temperature > (setPoint + hysteresis)) {
sNewState = "OFF";
}
if (sNewState != "") {
items[heatingValve].sendCommandIfDifferent(sNewState);
items[heatingContact].sendCommandIfDifferent(sNewState);
}
I also found a new flaw in my plan i think.
The “CV heat contact” shouldn’t be turned off if another room is still warming up.
I think i’ll remove it in Openhab for now and connect it to all the valves in ETS.