[SOLVED] How to save a value and use it in a rule

Hello

Im trying to set up a Raspberry Pi with a few 1w-sensors (onboard 1w) and analog inputs.
I have made one bash-script for fetching the temp from the 1w and another python-script for the analog sensors. Both working and showing values in habpanel.

But how can I use my values with rules? I want to control the heating with a script comparing actual temp with a set_tempValue (I made a list-widget with a few temperature values).

I guess I have to write both values to two variables to be able to compare them but how do I do that?

Got it to work with this rule, found another post that helped me, I missed the ā€œ.state.toStringā€-part. Now I just need to fine-tune it a bit :slight_smile:

rule "temperature"
when
        Item onewiretemp1Value received update
then
    if (onewiretemp1Value.state.toString < set_tempValue.state.toString) {
    channel4.sendCommand(ON)
    }
else {
    if(onewiretemp1Value.state.toString >= set_tempValue.state.toString) {
    channel4.sendCommand(OFF)
     }
    }

end

You are comparing strings and that may cause errorsā€¦
Do this:

rule "temperature"
when
    Item onewiretemp1Value received update
then
    var onewiretemp = onewiretemp1.state as Number
    var set_temp = set_tempValue.state as Number
    if (onewiretemp < set_temp) {
        channel4.sendCommand(ON)
    } else {
        channel4.sendCommand(OFF)
    }
end

You donā€™t need a second condition because if itā€™s not < then it must be >=

Did not work, got an error.
2018-05-03 00:38:27.939 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ā€˜temperatureā€™: Could not cast 21 to java.lang.Number; line 5, column 23, length 33

This is strange, it works for me
Ok, letā€™s add the toString bits, then:

rule "temperature"
when
    Item onewiretemp1Value received update
then
    val onewiretemp = onewiretemp1.state.toString as Number
    val set_temp = set_tempValue.state.toString as Number
    if (onewiretemp < set_temp) {
        channel4.sendCommand(ON)
    } else {
        channel4.sendCommand(OFF)
    }
end

Or you can try this :

rule "temperature"
when
    Item onewiretemp1Value received update
then
    val onewiretemp = (onewiretemp1.state as DecimalType).intValue
    val set_temp = (set_tempValue.state as DecimalType).intValue
    if (onewiretemp < set_temp) {
        channel4.sendCommand(ON)
    } else {
        channel4.sendCommand(OFF)
    }
end

Still no luck with other than my script with string. I only get an error message that it canā€™t write 21 (the set_tempValue): Could not cast 21 to java.lang.Number

I changed the missing Value in onewiretemp1Value.state so it isnā€™t that.
Maybe it is because Iā€™m fetching the temperature with a bash-script and my item is a String.

String onewiretemp1Value ā€œIndoor temp is [%s Ā°C]ā€ {channel=ā€œexec:command:onewiretemp1:outputā€}

The system isnā€™t going to be big, only a small caravan :slight_smile: so I donā€™t think I will run into any trouble.

Another way:

rule "temperature"
when
    Item onewiretemp1Value received update
then
    val onewiretemp = Float.valueOf(onewiretemp1Value.state.toString)
    val set_temp = Float.valueOf(set_tempValue.state.toString)
    if (onewiretemp < set_temp) {
        channel4.sendCommand(ON)
    } else {
        channel4.sendCommand(OFF)
    }
end
1 Like

This one works :smiley: Just need to change (onewiretemp1.state.toString) to (onewiretemp1Value.state.toString) :fu:

rule "temperature"
when
    Item onewiretemp1Value received update
then
    val onewiretemp = Float.valueOf(onewiretemp1Value.state.toString)
    val set_temp = Float.valueOf(set_tempValue.state.toString)
    if (onewiretemp < set_temp) {
        channel4.sendCommand(ON)
    } else {
        channel4.sendCommand(OFF)
    }
end

I added the else statement again but only with (onewiretemp > set_temp) to avoid start/stop to often.

rule "temperature"
when
    Item onewiretemp1Value received update
then
    val onewiretemp = Float.valueOf(onewiretemp1Value.state.toString)
    val set_temp = Float.valueOf(set_tempValue.state.toString)
    if (onewiretemp < set_temp) {
        channel4.sendCommand(ON)
    } else if (onewiretemp > set_temp){
        channel4.sendCommand(OFF)
    }
end

You can add a little offset to prevent that:
The temperature will be within 1c
You can make the offset smaller if you want but I recommend you have one
or you will damage your equipment especially if itā€™s a boiler

rule "temperature"
when
    Item onewiretemp1Value received update
then
    val onewiretemp = Float.valueOf(onewiretemp1Value.state.toString)
    val set_temp = Float.valueOf(set_tempValue.state.toString)
    if (onewiretemp < (set_temp - 0.5)) {
        channel4.sendCommand(ON)
    } else if (onewiretemp > (set_temp + 0.5)){
        channel4.sendCommand(OFF)
    }
end

That what I did (or thought I did :stuck_out_tongue: ) with Ā±1 degree?

With set_temp + 0.5 I will have Ā±1,5 degrees?

It is a combination of gas or electric boiler (water) Im not sure what is minimum runtime for the gas boiler but maybe I need 1,5-2 degrees but the problem is that will be 3-4 degree difference and maybe not comfortable? I will try Ā±1 to start with, if runtime is to short I will add 0,5, if thats not enough I might try a ā€œtimerā€ in my rules.

Thanks for the help! :slight_smile: