Script to write Back actual State from HM device

Hi !

I wrote a script to switch between HM-CC-RT-DN modes (AUTO / MANU /BOOST) via Webgui.
The Point is when you change mode direct at HM device my variable stays the same… (one rule for all switches because
(my initial scrip is a variation from here
So i started working on a script to change this.
my only Prolem seems to get the send value…
The part to constrcut the switchitem seems to work because wenn i enter in the last line instead of the “switchitem” a “MODE_MANU” my item whicht sets mode change to MANUAL
here is my script

rule “get modes”
when
Member of gGetHeatMode changed
then
val tmode = triggeringItem

if (triggeringItem.state.toString.contains ("AUTO-MODE")){
val setmode = "AUTO_MODE"
}
else if (triggeringItem.state.toString.contains ("MANU-MODE")){
val setmode = "MANU_MODE"
}
else if (triggeringItem.state.toString.contains ("BOOST-MODE")){
val setmode = "BOOST_MODE"
}
val switchitem = ScriptServiceUtil.getItemRegistry.getItem(tmode.name.split("_").get(0) + "_MODE_OH")

switchitem.postUpdate(setmode)

end

Between the { } braces is like a function block, an execution context. When the block is finished, anything newly created in that context - like your setmode variable - evaporates.

var setmode = "ERROR"
if (triggeringItem.state.toString.contains ("AUTO-MODE")){
setmode = "AUTO_MODE"
}
else if (triggeringItem.state.toString.contains ("MANU-MODE")){
setmode = "MANU_MODE"
}
// etc.

This variable is created outside the { } context, so it gets passed in and out

Hi !
OK one step far… but not finished :slight_smile: when rule is finished it seems that var switchs back to default
How can i find out whoch value is var setmode at the end of the rule ?

When your rule is finished, variable setmode is destroyed.
Next time the rule runs, a new one is created.

Who cares, you posted that value to an Item? What are you trying to do?

1 Like

Hi !

I have a script for HM-CC-RT-DN which controls Mode via GUI. (and it can be used with all simmilar HM-CC-RT-DNs) so only “Prefix” (in thiscase TKUE will change but the rest is in eacht HM-CC-RT-DN the same)
There for i have the following items
TKUE_MODE_OH => Dummy cant set with Values AUTO_MODE, MANU_MODE and BOOST_MODE
TKUE_AUTO_MODE => switch to set auto mode
TKUE_MANU_MODE => switch to set manual mode
TKUE_BOOST_MODE => switch to set Boost mode
In this script when State of *_MODE_OH item change i take the first part of the item Name (in this case TKUE and put the values of the MODE_OH (in this case TKUE) together to an item name and send to thos item the on Command (TKUE_AUTO_MODE and TKUE_BOOST_MODE ) or the actual set Temperature (TKUE_MANU_MODE). This script is working, but when you change mode direct at HM-CC-RT-DN the Value in TKUE_MODE_OH stays on the old value.
With the “new” script i try to do this…
Therefore i use also item TKUE_IST_MODE…
From Item TKUE_IST_MODE i get on change AUTO-MODE, MANU-MODE or BOOST-MODE as state/value.
Now i want to Take the first part (in this case TKUE) from item and put _MODE_OH to “create” item name. Then depending on state of TKUE_IST_MODE and update TKUE_MODE_OH
e.g. if TKUE_IST_MODE changes to MODE-AUTO , TKUE_MODE_OH will be set to MODE_AUTO.
Then i have one (small) rule for all HM-CC-RT-DN
This is my plan :slight_smile: so the first part is running, but i’m still stuck on the second part.

Ciao Gerd

Okay. We cannot see what your “second” rule looks like now.
Are these *_IST_MODE Items members of group gGetHeatMode?
Did you mean to check the existing state of the target *_MODE_OH Item before updating it?

Hi !
*_IST_MODE is a Member of groug gGetHeatMode.
An *_MODE_OH should be updated with a value depending on value of the *_IST_MODE item
value AUTO-MODE of *_IST_MODE should go as value AUTO_MODE to *_MODE_OH…
also with BOOST-MODE and MANU-MODE.
And i think its not nessesary to check if i maybe update to the same value because in the other rule i check against a change and ot and update or command.

Ciao Gerd

I’m sure you’ll show us your current version of the offending rule soon.

Hi !
Ok here we go… set_modes is working like charm…
get modes is my “problem”
I need to compare the value of *_IST_MODE and send/update a value (choosen with this comparison) to
*_MODE_OH

Ciao Gerd

get_mode.txt (651 Bytes) set_mode.txt (757 Bytes)

Please post simple rules directly, using code fences

import org.eclipse.smarthome.model.script.ScriptServiceUtil

rule "get modes"
when
      Member of gGetHeatMode changed 
then
    val tmode = triggeringItem
    var setmode ="ERROR"
    if (triggeringItem.state.toString.contains ("AUTO-MODE")){
    var setmode = "AUTO_MODE"
    }
    else if (triggeringItem.state.toString.contains ("MANU-MODE")){
    var setmode = "MANU_MODE"
    }
    else if (triggeringItem.state.toString.contains ("BOOST-MODE")){
    var setmode = "BOOST_MODE"
    }
   val switchitem = ScriptServiceUtil.getItemRegistry.getItem(tmode.name.split("_").get(0) + "_MODE_OH")
   switchitem.postUpdate(setmode)

end

Okay, here’s your problem. You didn’t do as shown

    var setmode ="ERROR"
    if (triggeringItem.state.toString.contains ("AUTO-MODE")){
    var setmode = "AUTO_MODE"
    }

Remember, between the braces {} is like a new context or environment.

First, you create a variable setmode and set it to “ERROR”
Next, in the {} part of the if(), you create another new variable called setmode and set it to “AUTO_MODE”
Every time you refer to setmode in this {} context, you will get that one. You cannot refer to the one you made earlier, because you have stolen its name temporarily. You’ve no way to call it by name.

At the closing }, your new variable is discarded. You cannot call it any more.
If you do call setmode, you will now get the “old” one. That is still set to “ERROR”.

Here’s the fix

    var setmode ="ERROR"
    if (triggeringItem.state.toString.contains ("AUTO-MODE")){
       setmode = "AUTO_MODE"
    }

Now, when you refer to setmode inside the {} it will use the already existing one from outside of the {}.
If you change that, the change will survive the closing } because that variable lives outside of the {}

Ok… works … many thanks
i was a little confused about the difference of var and val

Ciao Gerd