Add Text Item content form DSL Rule

Dear Colleagues, looks stupid but I can’t get it work. I need to add time then rule execute (i’m getting time valur) to a textual Item (KegeratorTime), but I tried:

postupdate(KegeratorTime, “Power ON: " + hour + “:” + minute”)
sendCommand(KegeratorTime, “Power ON: " + hour + “:” + minute”)
KegeratorTime = "Power ON: " + hour + “:” + minute

However it didn’t update my item to print it on my sitemap. Entire Rule below
rule “Kegerator Check”

when

System started or

Time cron "0 0/30 * 1/1 * ? *" or 

Item SonoffKegerator_TempKeg received update or

Item Kegerator_SetPoint received update or

Item SonoffKegerator_PowerKegerator received update

then

//var Number set_keg = (SonoffKegerator_TempKeg.state as DecimalType).intValue  

var Number set_keg = Kegerator_SetPoint.state as DecimalType    

//var Number keg = SonoffKegerator_TempKeg.state as DecimalType

var Number keg = (SonoffKegerator_TempKeg.state as QuantityType<Number>).doubleValue

//get time last change

var hour = now.getHour

var minute = now.getMinute

//logInfo("Kegerator", "Validation:" + keg)    

if (keg >= set_keg ) {

    //logInfo("Kegerator", "keg>set" + set_keg +"-" + keg )

    if (SonoffKegerator_PowerKegerator.state == OFF){

        sendCommand(SonoffKegerator_PowerKegerator, ON)

        //sendCommand(KegeratorTime, "Power ON: " + hour + ":" + minute )    

        KegeratorTime = "Power ON: " + hour + ":" + minute 

        logInfo("Kegerator", "Temperature < set - power on" + keg)

    }

}else if (keg < (set_keg -1)) {

    //logInfo("Kegerator", "keg<set"  + set_keg +"-" + keg )

    if (SonoffKegerator_PowerKegerator.state == ON){

        sendCommand(SonoffKegerator_PowerKegerator, OFF)

        //sendCommand(KegeratorTime, "Power OFF: " + hour + ":" + minute )

        KegeratorTime = "Power OFF: " + hour + ":" + minute 

        logInfo("Kegerator", "Temperature < set - power on" + keg)

        

    }

}

end

Please mark code as code, otherwise code will be truncated or maybe changed slightly.as seen in your posting…

And it’s more readably without that much commented out lines…

rule “Kegerator Check”
when
    System started or
    Time cron "0 0/30 * * * ?" or 
    Item SonoffKegerator_TempKeg received update or
    Item Kegerator_SetPoint received update or
    Item SonoffKegerator_PowerKegerator received update
then
    var Number set_keg = Kegerator_SetPoint.state as DecimalType
    var Number keg = (SonoffKegerator_TempKeg.state as QuantityType<Number>).doubleValue
    var hour = now.getHour
    var minute = now.getMinute
    if (keg >= set_keg ) {
        if (SonoffKegerator_PowerKegerator.state == OFF){
            sendCommand(SonoffKegerator_PowerKegerator, ON)
            KegeratorTime = "Power ON: " + hour + ":" + minute 
            logInfo("Kegerator", "Temperature < set - power on" + keg)
        }
    }else if (keg < (set_keg -1)) {
        if (SonoffKegerator_PowerKegerator.state == ON){
            sendCommand(SonoffKegerator_PowerKegerator, OFF)
            KegeratorTime = "Power OFF: " + hour + ":" + minute 
            logInfo("Kegerator", "Temperature < set - power on" + keg)
        }
    }
end

Anyway…

The two vars minute and hour are of type Integer, as this is the type of .getHour and .getMinute. If you want to use them as string, you have to use now.getHour.toString and now.getMinute.toString

You also use KegeratorTime as a simple var, but it isn’t. As it’s an Item, you have to use postUpdate instead.

Please be aware that the code can be optimized:

rule “Kegerator Check”
when
    // No need of System started, as any change will trigger the rule
    Time cron "0 0/30 * * * ?" or // even this is waste...
    Item SonoffKegerator_TempKeg changed or // changed will suffice
    Item Kegerator_SetPoint changed or // changed will suffice
    Item SonoffKegerator_PowerKegerator changed // changed will suffice
then
    val Number set_keg = Kegerator_SetPoint.state as DecimalType
    val Number keg = (SonoffKegerator_TempKeg.state as QuantityType<Number>).doubleValue
    val strTime = ": " + now.getHour.toString + ":" + now.getMinute.toString

    var Switch myState = NULL // use a var to minimize code duplication
    if (keg >= set_keg)
        myState = ON
    else if(keg < set_keg - 1)
        myState = OFF

    if(myState != NULL && SonoffKegerator_PowerKegerator.state != myState) {
        SonoffKegerator_PowerKegerator.sendCommand(myState.toString)
        val strText = "Power " + myState.toString + strTime 
        KegeratorTime.postUpdate(strText)
        logInfo("Kegerator", "Temperature ({}) {} set ({}) - power {} "keg,if(myState == ON) ">" else "<" ,set_keg,myState)
    }
end

Hi Udo,

Thanks so much for your support, I’m not very programmatic guys but now I learn good tricks. I’m getting bellow error, may you help me to find the issue?

Script execution of rule with UID ‘kegeretor-1’ failed: Unknown variable or command ‘!==’; line 19, column 8, length 16 in kegeretor

I tried to add != and !== and didn’t work, its for below validation:
var Switch myState = NULL
if(myState !== NULL && SonoffKegerator_PowerKegerator.state !== myState) {

There is no Switch variable type.
Don’t muddle Items with in-rule variables.

Oh, sorry, should be var OnOffType

rule “Kegerator Check”
when
    // No need of System started, as any change will trigger the rule
    Time cron "0 0/30 * * * ?" or // even this is waste...
    Item SonoffKegerator_TempKeg changed or // changed will suffice
    Item Kegerator_SetPoint changed or // changed will suffice
    Item SonoffKegerator_PowerKegerator changed // changed will suffice
then
    val Number set_keg = Kegerator_SetPoint.state as DecimalType
    val Number keg = (SonoffKegerator_TempKeg.state as QuantityType<Number>).doubleValue
    val strTime = ": " + now.getHour.toString + ":" + now.getMinute.toString

    var OnOffType myState = null // use a var to minimize code duplication
    if (keg >= set_keg)
        myState = ON
    else if(keg < set_keg - 1)
        myState = OFF

    if(myState !== null && SonoffKegerator_PowerKegerator.state != myState) {
        SonoffKegerator_PowerKegerator.sendCommand(myState.toString)
        val strText = "Power " + myState.toString + strTime 
        KegeratorTime.postUpdate(strText)
        logInfo("Kegerator", "Temperature ({}) {} set ({}) - power {} "keg,if(myState == ON) ">" else "<" ,set_keg,myState)
    }
end

Please be aware of the additional changes about myState (!== null instead of != NULL)

1 Like