[SOLVED] Help with a rule

Hey. Im trying to make a rule, but Im stuck :frowning:, pretty new to this game :stuck_out_tongue:
It is supposed to turn on oven if temperature is under or over a certain value, but only if a virtual switch is pressed. What am I doing wrong?

This is the rule

rule " OVN Gjesterom PÅ / AV INGEN GJEST"

when

Item GJEST_BESOK.state == OFF

then

if (SENSORGjesterom_ActualTemperature.state < 5.0) {

Item DEVICEOvnGjesterom_Switch.sendCommand(ON)

}

if (SENSORGjesterom_ActualTemperature.state > 12.0) {

Item DEVICEOvnGjesterom_Switch.sendCommand(OFF)

}

end

rule " OVN Gjesterom PÅ / AV GJEST"

when

Item GJEST_BESOK.state == ON

then

if (SENSORGjesterom_ActualTemperature.state < 18.0) {

Item DEVICEOvnGjesterom_Switch.sendCommand(ON)

}

if (SENSORGjesterom_ActualTemperature.state > 22.0) {

Item DEVICEOvnGjesterom_Switch.sendCommand(OFF)

}

end

First please use the code fences when posting code…

Second, install VSCode with the openHABextension to validate your code
Third, read: https://www.openhab.org/docs/configuration/rules-dsl.html

rule " OVN Gjesterom PÅ / AV INGEN GJEST"
when
    Item GJEST_BESOK changed
then
    var switchCommand = "OFF"
    val temperature = SENSORGjesterom_ActualTemperature.state as Number

    if (GJEST_BESOK.state == NULL) return; // Do Nothing if NULL

    if (GJEST_BESOK.state == OFF) {
        if (temperature < 5.0) {
           switchCommand = "ON"
        }
        if (temperature > 12.0) {
           switchCommand = "OFF"
        }
    if (GJEST_BESOK.state == ON) {
        if (temperature < 18.0) {
           switchCommand = "ON"
        }
        if (temperature > 22.0) {
           switchCommand = "OFF"
        }
    }
    DEVICEOvnGjesterom_Switch.sendCommand(switchCommand)

end

Thank you. :slight_smile: And Thanks for the tips. Like I said Im new and still learning :smiley:

One more tip: