CPU temp. notification - problems with values comparision

(OH3.4, RaspPi 4+)

Hello,
I am trying to write a rule which is notifying me when the CPU temp. is higher than 65deg C (for testing purposes 53) and when it comes back to normal <55degC (51 for test).
The issue is that the temperature item (as Number) of the CPU temp is giving a mixed value back: example: “53.1 °C” instead of pure dec. value and I can not compare it with the expected number.

1.Can someone help me to format this value properly? There is sth wrong because even if temp. is not higher than 53 the rule recognise it as more than 53.
2. Will the hysteresis work? I was following example of this design patterns

This is my script:

rule "RaspPi to Hot"

when
    Item RaspberryPiSystem_CPU_Temperature changed
then
   
    if ((RaspberryPiSystem_CPU_Temperature.state as Number) > 60) {
        sendNotification("me@email.com", "CPU Temp. HIGH: " + RaspberryPiSystem_CPU_Temperature.state)
        logInfo("Alert Temperature", "CPU Temp. > 53 VALUE: " + (RaspberryPiSystem_CPU_Temperature.state as Number))
    }
    else if  ((RaspberryPiSystem_CPU_Temperature.state as Number) <= 55) {
        sendNotification("me@email.com", "CPU Temp. back to normal " + RaspberryPiSystem_CPU_Temperature.state)
        logInfo("Alert Temperature", "CPU temp back to normal. VALUE: " + (RaspberryPiSystem_CPU_Temperature.state as Number))
    }
end 

Logfile for 53 and 51deg for testing purposes.

==> /var/log/openhab/openhab.log <==
2023-10-28 19:00:14.103 [INFO ] [.core.model.script.Alert Temperature] - CPU Temp. > 53 VALUE: 51.6 °C
==> /var/log/openhab/events.log <==
2023-10-28 19:00:14.096 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'RaspberryPiSystem_CPU_Temperature' changed from 54 °C to 51.6 °C
1 Like

I think UoM comparison should work like

if ( temperature.state > 23 | "°C"  )  {

seems to be working! Many thanks!
BTW what UoM means?

Unit of Measurement, see https://www.openhab.org/docs/concepts/units-of-measurement.html

thx.
Just noticed that the hysteresis does not work.
Every time the temp goes above min. defined value (my test case 51deg) and comes back I have notification. I think I need to make some kind of mark to be set when the temp. crosses the alert value.
than in else if it will be checked an cleared. sth like:

var int flag=0

rule "RaspPi to Hot"

when
    Item RaspberryPiSystem_CPU_Temperature changed
then

    ((RaspberryPiSystem_CPU_Temperature.state > 53 | "°C") && (flag == 0)) {
        flag = 1
        //sendNotification("jdebs@wp.pl", "CPU temp. HIGH: " + RaspberryPiSystem_CPU_Temperature.state, "temperature", "high" )
        logInfo("Alert Temperature", "CPU temp. HIGH: " + RaspberryPiSystem_CPU_Temperature.state)
    }
    else if  ((RaspberryPiSystem_CPU_Temperature.state  <= 51 | "°C") && (flag==1)) {
        //sendNotification("jdebs@wp.pl", "CPU temp. back to normal: " + RaspberryPiSystem_CPU_Temperature.state, "temperature", "low")
        logInfo("Alert Temperature", "CPU temp back to normal: " + RaspberryPiSystem_CPU_Temperature.state)
        flag = 0
    }
    
end 

Do you thing it is properly written? Have no idea about the proper variables. It should not only work but be also efficient. I think…

I have something like this that includes a hysteresis. It evaluates the light measured by a hue motion sensor, and then enables or disables it accordingly. The a_li_hlup_swi.state == OFF makes sure the rule acts only on the natural light, not when the local light is on.

   if ( s_ms_hlup_enab.state == ON ) {
      if ( s_ms_hlup_illu.state > 3 | "lx" && a_li_hlup_swi.state == OFF) {
	 logInfo("up_common.rules", "hlup: Disable MS (bright enough)")
         s_ms_hlup_enab.sendCommand(OFF)
      }
   } else {
      if ( s_ms_hlup_illu.state < 2 | "lx" ) {
         logInfo("up_common.rules", "hlup: Enable MS (low light)" )
         s_ms_hlup_enab.sendCommand(ON)      
      }
   }

Oh, wow: I just had some fun with chatGPT, and it came back with this one. It’ll need some refinement for the UoM, but in principle this should also work:

write me an openHAB heating rule that contains hysteresis between 15 and 20 degrees Celsius
ChatGPT

Certainly, here's an example of an openHAB rule that controls a heating system with hysteresis between 15 and 20 degrees Celsius:


import org.eclipse.smarthome.model.script.ScriptServiceUtil
import org.eclipse.smarthome.core.library.types.OnOffType

rule "Heating Control"
when
    Item TemperatureSensor changed // Replace with the actual temperature sensor item name
then
    val Number targetTemperature = 20 // Target temperature in degrees Celsius
    val Number hysteresis = 2 // Hysteresis in degrees Celsius

    val Number currentTemperature = TemperatureSensor.state as Number

    if (currentTemperature < targetTemperature - hysteresis) {
        // Temperature is below the lower threshold, turn on the heating
        HeatingItem.sendCommand(OnOffType.ON) // Replace with your heating item name
    } else if (currentTemperature > targetTemperature + hysteresis) {
        // Temperature is above the upper threshold, turn off the heating
        HeatingItem.sendCommand(OnOffType.OFF) // Replace with your heating item name
    }
end

In this example:

    Replace "TemperatureSensor" with the name of your actual temperature sensor item.
    Set the "targetTemperature" to your desired target temperature (in this case, 20 degrees Celsius).
    Set the "hysteresis" to the desired hysteresis value (in this case, 2 degrees Celsius). This means the heating will turn on when the temperature drops 2 degrees below the target and turn off when it's 2 degrees above the target.
    Replace "HeatingItem" with the name of the item that controls your heating system.

This rule will trigger whenever the temperature sensor's value changes. It will check the current temperature and compare it to the target temperature with hysteresis. If the temperature is outside the hysteresis range, it will either turn on or off the heating system accordingly. Adjust the target temperature and hysteresis values as needed for your specific setup.

Wow. Thx. I have never thought o ask chat GPT about that. Let me try this out. Shall be back with the feedback next week.
Thanks again

there is a hysteresis profile that will do this for you without a rule

coming back to the rule…
I am still struggling with the CPU temperature of the System Info Binding.

RaspberryPiSystem_CPU_Temperature.state as Number

displays the flowing value: “51.6 °C” I was rather expecting 51.6 or simple 51. This causes issues when i want to compare numbers latter on. Why it is like that? is “as Number” really not as the number?
Multiplying this value by 1 gives 324.75 ? Why? How to convert the CPU temp in this format: xxx.y °C to the number I could easy compare with?

rule "Temperature Alert"
when
    Item RaspberryPiSystem_CPU_Temperature changed
then
    val setpoint = 51 // Set your desired temperature threshold
    val hysteresis = 2 // Set your hysteresis value (e.g., 1°C)

    if (RaspberryPiSystem_CPU_Temperature.state instanceof Number) {
        val currentTemperature = RaspberryPiSystem_CPU_Temperature.state as Number * 1
        logInfo("Alert Temperature", "currentTemperature: " + currentTemperature)
        
        if (currentTemperature > (setpoint + hysteresis)) {
            // Send an email notification
            logInfo("Alert Temperature", "CPU temp. HIGH: " + RaspberryPiSystem_CPU_Temperature.state)
            //sendMail("youremail@gmail.com", "Temperature Alert", "Temperature is above the threshold. Current temperature: " + currentTemperature.toString + "°C")
        }
    }
end
2023-11-05 19:14:08.788 [INFO ] [.core.model.script.Alert Temperature] - currentTemperature: 324.75
2023-11-05 19:14:08.792 [INFO ] [.core.model.script.Alert Temperature] - CPU temp. HIGH: 51.6 °C

What profile do you mean?

I think I found the answer after few long minutes of reserach in older posts.
Thank you @ rossko57 for this nice explenation!

I converted my line to:

val currentTemperature =  (RaspberryPiSystem_CPU_Temperature.state as QuantityType<Number>).toBigDecimal

and my comparison works. Let me post my ready rule when it is finished.

Items | openHAB, third row in the table.

Finally I made my rule for the push notification in case of high CPU temperature.
Notification is being sent at 60°C about to high CPU temperature and one more time when the temp. comes back to normal (52°C).
Set temp = 56°C
Hysteresis = 4°C

var temp_flag = 0

rule "Temperature Alert"
when
    Item RaspberryPiSystem_CPU_Temperature changed
then
    val setpoint = 56 // Set your desired temperature threshold
    val hysteresis = 4 // Set your hysteresis value (e.g., 4°C)
    

    if (RaspberryPiSystem_CPU_Temperature.state instanceof Number) {
        val currentTemperature =  (RaspberryPiSystem_CPU_Temperature.state as QuantityType<Number>).toBigDecimal
        //logInfo("Alert Temperature", "currentTemperature: " + currentTemperature)
        
        if ((currentTemperature > (setpoint + hysteresis)) && (temp_flag != 1)) {
            temp_flag = 1
            logInfo("Alert Temperature", "CPU temp. HIGH: " + RaspberryPiSystem_CPU_Temperature.state + ", Variable flag set:" + temp_flag)
            sendNotification("email@domain.com", "CPU temp. HIGH!: " + RaspberryPiSystem_CPU_Temperature.state, "alarm", "high")
            //sendMail("youremail@gmail.com", "Temperature Alert", "Temperature is above the threshold. Current temperature: " + currentTemperature.toString + "°C")
        }
/*        
// for debugging purposes. Just to observe what is the CPU temp.
        if (currentTemperature <= setpoint){
            logInfo("Alert Temperature", "CPU temp.: " + RaspberryPiSystem_CPU_Temperature.state +", Variable flag:" + temp_flag)
        }
*/
       if ((currentTemperature <= setpoint) && (temp_flag == 1)){
            temp_flag = 0
            logInfo("Alert Temperature", "CPU temp. COOL: " + RaspberryPiSystem_CPU_Temperature.state + ", Variable flag clear:" + temp_flag)
            sendNotification("email@domain.com", "CPU temp. NORMAL: " + RaspberryPiSystem_CPU_Temperature.state, "temperature", "low")
            //sendMail("youremail@gmail.com", "Temperature Alert", "Temperature is above the threshold. Current temperature: " + currentTemperature.toString + "°C")
        }
    }
end

What is the purpose of a CPU temperature notification system, and why is it important for users?

When a CPU gets above a certain temperature that indicates a potential problem. If it runs too hot for too long the machine may simply stop running and freeze until it cools down. Efficiency drops, and if the CPU runs hot for long periods of time it can damage the CPU.

If someone is overclocking their machine or it’s put somewhere where heat can be a problem (e.g. a garage or attic) getting an alert when the temp is too high can let the end user take action to prevent damage.