Heating setup, onewire read temp and trigger relay

any help please, I am trying to trigger a control relay (mr_brm_ht) when (mr_brm_ht_sw) is changed state by user if temp is outwith set value (mr_brm_ht_set). temp is read via (mr_brm_ht_sr)
I am using HABPanel so no sitemap, where am I going wrong

#items
// heating setpoints
Number mr_brm_ht_set “Main Bedroom Setpoint [%.1f °C]”

// heating modes: 0 - Off, 1 - On
Number mr_brm_ht_sw “mr_brm_ht_sw”

Switch mr_brm_ht “mr_brm_ht” { gpio=“pin:2 activelow:yes initialValue:high”}
String mr_brm_ht_sr “Main Bedroom is [%s °C]” {channel=“exec:command:mr_brm_ht_sr:output”}

#things
exec:command:mr_brm_ht_sr [command=“bash /etc/openhab2/scripts/onewiretemp.sh 28-0417836bfbff”]

#rules
rule “Main Bedroom heating”

when
Item mr_brm_ht_sw changed or
Item mr_brm_ht_set changed or
Item mr_brm_ht_sr changed
then
// 0=“Off”, 1=“On”, 2="Auto"
if (mr_brm_ht_sw.state == 0) {
// heater off
mr_brm_ht.sendCommand(Off)
} else if (mr_brm_ht_sw.state == 1) {
// heater on
mr_brm_ht.sendCommand(On)
} else if (mr_brm_ht_sw.state == 2) {

        // get the current setpoint for the main bedroom
        var Number setpoint = mr_brm_ht_set.state as DecimalType

        // calculate the turn on/off temperatures
        var Number turnOnTemp = setpoint - 0.5
        var Number turnOffTemp = setpoint + 0.5

        // get the current temperature in the main bedroom
        var Number temp = mr_brm_ht_sr.state as DecimalType

    // determine whether we need to turn on/off the heater
        if (temp <= turnOnTemp) {
            // turn on temp has been reached so switch on the heater
            mr_brm_ht.sendCommand(ON)
        } else if (temp >= turnOffTemp) {
            // turn off temp has been reached so switch off the heater
            mr_brm_ht.sendCommand(OFF)
        }
    } else {
        // heater off
        mr_brm_ht.sendCommand(OFF)
    }
}

end

You’ve not really said whether any of it is working or is it just the ‘Auto’ temperature controlled part?

If you just toggle the mr_brm_ht switch ON/OFF does the relay work?

Your first two checks on mr_brm_ht_sw.state equal to 0 and 1 use non standard Off and On case in sendCommand rather than OFF and ON. Can’t say without testing whether OH will complain but consistency is king for ease of reading code so good practice either way.

After that you need to be reading the log files openhab and events and put some logInfo messages in your rule code so you can trace the execution path and see the values of variables like setPoint and temp at runtime to help test your maths and conditions.

Hi Ray, thanks for taking the time to reply. I noticed the capitalisation issue last night and corrected but still same result, if I assign mr_brm_ht to a button and toggle the relay actions, mr_brm_ht_sr shows the current temp in gui and changes state when I subject it to heat changes. the relay doesn’t fire if I change the setpoint to a lower value, I can see in the logs that this is being recorded, it also doesn’t fire if the temp sensor mr_brm_ht_sr falls below setpoint mr_brm_ht_set, its as though the rule isn’t firing

2017-12-06 12:30:00.887 [ItemStateChangedEvent ] - mr_brm_ht changed from ON to OFF
2017-12-06 12:30:01.832 [ItemCommandEvent ] - Item ‘mr_brm_ht_set’ received command 20
2017-12-06 12:30:01.835 [ItemStateChangedEvent ] - mr_brm_ht_set changed from 25 to 20
2017-12-06 12:30:20.014 [ItemStateChangedEvent ] - Wifi_Level changed from 2 to 1
2017-12-06 12:30:26.700 [ItemStateChangedEvent ] - mr_brm_ht_sr changed from 25.312 to 30.625

Add some logging in your Rule to make sure it’s firing.

http://docs.openhab.org/administration/logging.html#create-log-entries-in-rules

I would add one as the very first line in your Rule, so you can be sure the rule itself is firing.

Then, add more log lines as necessary to see the state of variables and calculations you do, to ensure that you end up with the result you expect.

Good to break down the problem and test the parts. So you’re confident the relay fires, the values are there and changing etc so you need to start monitoring the openhab.log and add plenty of log entries to your rule so you can firstly ensure the rule is executing and then trace and validate it’s logic.

sorry what do I type & where do I put it for the logging. unsure of this

thanks

michael

@ Confused posted the appropriate documentation:
http://docs.openhab.org/administration/logging.html#create-log-entries-in-rules

So you put something like this after the initial ‘then’:

logInfo("heating-control.rules", "Enterting Main Bedroom heating...")

then add as many more as like in different parts of your rule to trace the execution and show values like:

} else if (mr_brm_ht_sw.state == 2) {
      // get the current setpoint for the main bedroom
      var Number setpoint = mr_brm_ht_set.state as DecimalType
      logInfo("heating-control.rules", "State=2 setpoint=" + setpoint)

2017-12-07 23:32:03.484 [INFO ] [g.eclipse.smarthome.model.script.htg] - Enterting Main Bedroom heating…
2017-12-07 23:32:03.494 [INFO ] [g.eclipse.smarthome.model.script.htg] - State=2 setpoint=16.0
2017-12-07 23:32:03.500 [ERROR] [.script.engine.ScriptExecutionThread] - Rule ‘Main Bedroom heating’: org.eclipse.smarthome.core.library.types.DecimalType

} else if (mr_brm_ht_sw.state == 1) {
    // heater on
    mr_brm_ht.sendCommand(ON)
    } else if (mr_brm_ht_sw.state == 2) {
        // get the current setpoint for the main bedroom
        var Number setpoint = mr_brm_ht_set.state as DecimalType

logInfo(“htg”, “state=2 setpoint=” + setpoint)
// calculate the turn on/off temperatures
var Number turnOnTemp = setpoint - 0.5
var Number turnOffTemp = setpoint + 0.5
// get the current temperature in the main bedroom
logInfo(“htg”, “state=2 temp=”)
var Number temp = mr_brm_ht_sr.state as DecimalType
// determine whether we need to turn on/off the heater

2017-12-08 01:03:56.475 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'home.rules’
2017-12-08 01:04:09.794 [INFO ] [g.eclipse.smarthome.model.script.htg] - state=2 setpoint=16.0
2017-12-08 01:04:09.797 [INFO ] [g.eclipse.smarthome.model.script.htg] - state=2 temp=
2017-12-08 01:04:09.799 [ERROR] [.script.engine.ScriptExecutionThread] - Rule ‘Main Bedroom heating’: org.eclipse.smarthome.core.library.types.DecimalType

I don’t think the rule is getting the sensor temp mr_brm_ht_sr

This is the problem, mr_brm_ht_sr is not a Number item it’s a String. So try changing this line:

        var Number temp = mr_brm_ht_sr.state as DecimalType

to this to convert the string to your number:

        var Number temp = Double::parseDouble(mr_brm_ht_sr.state.toString)

thanks, your an absolute legend, working now.]
next step…
I’m adding timers, lets see how I get on with that

michael