Need example of regulating temperature in OH3

Need some direction on writing a rule or script to regulate the water temperature of my pool. I saw some tips from @rikoshak and other s but I’m unclear on the scripting language to use and how to incorporate rules I already created. I have used rules in openhab 2.5 but don’t know the difference between the two versions. I know I’m totally off but this is what I have so far

var threashold = 70
var hysteresis = 1
if (HomieMQTTDevice_Pool_2Dtemp_Temperature.getStateAs(QuantityType) > (threshold + hysteresis){
HomieMQTTDevice_Pool_2Dheater_Switch.sendCommand(OFF);
LowSpeedFeatureItem.sendCommand(ON);
}
else if (HomieMQTTDevice_Pool_2Dtemp_Temperature.getStateAs(QuantityType) <= threshold{
HomieMQTTDevice_Pool_2Dheater_Switch.sendCommand(ON);
HeaterMinSpeed_HeatModeMinSpeed.sendCommand(ON);
}

and this is the error I’m getting

2021-01-25 23:27:14.492 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘SetPoolTemp’ failed: :3:95 Expected ) but found {

if (HomieMQTTDevice_Pool_2Dtemp_Temperature.getStateAs(QuantityType) > (threshold + hysteresis){

                                                                                           ^ in <eval> at line number 3 at column number 95

Thanks,

Tony

Check your parentheses:

if (HomieMQTTDevice_Pool_2Dtemp_Temperature.getStateAs(QuantityType) > (threshold + hysteresis){
...
if (HomieMQTTDevice_Pool_2Dtemp_Temperature.getStateAs(QuantityType) > (threshold + hysteresis){

Three opening parentheses, only two closing parentheses on both statements.

Yes, you’re right, I’m missing a right parenthesis. I fixed it but I still getting a similar error saying my item is not defined.

var threashold = 70
var hysteresis = 1
if (HomieMQTTDevice_Pool_2Dtemp_Temperature.getStateAs(QuantityType) > (threshold + hysteresis))
{
HomieMQTTDevice_Pool_2Dheater_Switch.sendCommand(OFF);
LowSpeedFeatureItem.sendCommand(ON);
}
else if (HomieMQTTDevice_Pool_2Dtemp_Temperature.getStateAs(QuantityType) <= threshold)
{
HomieMQTTDevice_Pool_2Dheater_Switch.sendCommand(ON);
HeaterMinSpeed_HeatModeMinSpeed.sendCommand(ON);
}

2021-01-26 05:08:11.372 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘SetPoolTemp’ failed: ReferenceError: “HomieMQTTDevice_Pool_2Dtemp_Temperature” is not defined in at line number 3

Please show where and how you did define your item?

The copied script is all of it. As for the item, there are several channels in the HomieMQTTDevice. This is the item associated with the pool temperature. I’ve used the relay items in Rules and used this item in pages.

I did a comparable rule with Blockly that I followed from another post which ran.

Totally different code

var current_temp;

current_temp = itemRegistry.getItem(‘HomieMQTTDevice_Pool_2Dtemp_Temperature’).getState();
if (current_temp >= 72) {
events.sendCommand(‘HomieMQTTDevice_Pool_2Dheater_Switch’, ‘OFF’);
} else if (current_temp < 70) {
events.sendCommand(‘HomieMQTTDevice_Pool_2Dheater_Switch’, ‘ON’);
events.sendCommand(‘HeaterMinSpeed_HeatModeMinSpeed’, ‘ON’);
}

Got it working now with the scheduling. Thanks for your help.

Just to provide some clarification:

  • Rules DSL, the rules most people wrote in versions of openHAB prior to OH 3 still work and still work just like they alway have in .rules files (with a few minor changes).

  • By default Blockly and JavaScript are now options to choose from.

  • Additionally Python and Groovy can be installed as add-ons.

  • There are differences though in how rules are created and what you can do when creating Rules from the UI instead of text files. The biggest difference is you don’t define the rule in code. All you have to code is the stuff that is between the “then” and “end” in an old .rules file. But that means you can’t define global variables because there is no “outside” the rule to put them.

  • Any supported language can be written in text files or used in the UI (except Blockly which is UI only).

  • Blockly is really just a wrapper around JavaScript. The actual code that is created is JavaScript.

This is a good choice for a Blockly rule. But you could even back up further and not have any code to implement this. It’ll take two rules though.

First rule:

  • when Item event changes for HomieMQTTDevice_Pool_2Dtemp_Temperature
    • add trigger -> Item Event -> select HomieMQTTDevice_Pool_2Dtemp_Temperature -> select changed
  • but only if HomieMQTTDevice_Pool_2Dtemp_Temperature >= 72
    • add condition -> Item Condition -> select HomieMQTTDevice_Pool_2Dtemp_Temperature -> select “is greater than equal to” and enter 72
  • then HomieMQTTDevice_Pool_2Dheater_Switch command to OFF
    • add action -> Item Action -> select HomieMQTTDevice_Pool_2Dheater_Switch -> select “send a command to” and select “OFF”

Second rule:

  • when HomieMQTTDevice_Pool_2Dtemp_Temperature changes
  • but only if HomieMQTTDevice_Pool_2Dtemp_Temperatuer < 70
  • then HomieMQTTDevice_Pool_2Dheater_Switch command to ON
  • then HeaterMinSpeed_HeatModeMinSpeed commanded to ON

I have started to involve my son in creating rules with Blockly. I think it is a great way to get started with basic coding when the outcome is something useful such as the lighting in his room.
Many of my DSL rules start with defining conditions that result in exiting the rule such as

if (item1.state == "ON")
return

Is there a way to do this in Blockly?
Thanks!

In Blockly, create a variable and then set item.state to that variable. Then you can do conditional statements such as below

image

So far I have this (a few more else if need to be added). Can I just leave the first “do” blank?

Yes, you can but I would reconsider the logic. In order to access the second if statement, all conditions in the first loop need to be false. If that is the case, then I would use AND statement and the opposite values of each variable in order to enter the loop. You can then do another loop for the second condition.

Thanks, that makes a lot of sense. It is working now.