[SOLVED] Variable call other variable to change Zwave parameters wth REST API

Hello, my rules don’t work.

val uid_Motion_timer_chj = "zwave%3Adevice%3Ac3f49cf3%3Anode8"
val uid_Motion_timer_entree = "zwave%3Adevice%3Ac3f49cf3%3Anode5"
val uid_Motion_timer_Couloir = "zwave%3Adevice%3Ac3f49cf3%3Anode16"
val uid_Motion_timer_cuisine = "zwave%3Adevice%3Ac3f49cf3%3Anode10"
val uid_Motion_timer_salon = "zwave%3Adevice%3Ac3f49cf3%3Anode17"
val uid_Motion_timer_sam = "zwave%3Adevice%3Ac3f49cf3%3Anode13"
val uid_Motion_timer_sdj = "zwave%3Adevice%3Ac3f49cf3%3Anode6"
val uid_Motion_timer_chp = "zwave%3Adevice%3Ac3f49cf3%3Anode7"
val uid_Motion_timer_sdb = "zwave%3Adevice%3Ac3f49cf3%3Anode9"


    rule "config timer Motion"
    when
        Member of G_Motion_Timer received command
    then
        var int configTimer = (triggeringItem.state as Number).intValue
        var Number jsonTimer = '{"config_6_2": ' + configTimer + '}'
        var httpPutTimer = 'uid_' + triggeringItem.name
        logInfo("motion.test", "http://localhost:8080/rest/things/" + httpPutTimer + "/config" + jsonTimer)
        sendHttpPutRequest("http://localhost:8080/rest/things/" + httpPutTimer + "/config", "application/json", jsonTimer)
    end

the httpPutTimer variable is egal at uid_Motion_timer_entree when it’s the Motion_timer_entree item who lunch the request. but after impossible to transforme uid_Motion_timer_entree at zwave%3Adevice%3Ac3f49cf3%3Anode5 in the sendHttpPutRequest

An idea ?
Thank you veru much

This does not deal with the case of the triggering Item not having a numeric state e.g.NULL or UNDEF

I’m not sure what the triggering Item state is that you expecting, but don’t forget that commands are asynchronous - if you are expecting the command to have influenced the state by autoupdate etc., it may or may not have happened yet.

This looks a bit weird on two points;
You declare a Number type but assign a string value. I think DSL can deal with that, and ignore your declaration.
configTimer was delcared as an int primitive type, I do not think that has a .toString method that DSL will look for in string building. Type Integer would be a better choice.

The problem isn’t with configTimer , this variable is a number … the problem is here with httpPutTimer who is egal for example at uid_Motion_timer_entree but in sendHttpPutRequest (or the loginfo) isn’t change to zwave%3Adevice%3Ac3f49cf3%3Anode5

for example the log of loginfo is

[INFO ] [e.smarthome.model.script.motion.test] - http://localhost:8080/rest/things/uid_Motion_timer_entree/config{"config_6_2": 300}

but I want :

http://localhost:8080/rest/things/zwave%3Adevice%3Ac3f49cf3%3Anode5/config{"config_6_2": 300}

because : val uid_Motion_timer_entree = zwave%3Adevice%3Ac3f49cf3%3Anode5

Okay, free advice, free to ignore.

Ah right. The rule is doing exactly what you told it do -

making httpPutTimer to be some string.

It appears what you want to do is instead use that string to select a variable.by variable name.
You can’t.

What you can do is select from a data “array” by name.

import java.util.Map

val Map<String, String> myTable = newHashMap(
   "someItemName" -> "my%20text_value",
   "othertemName" -> "some%20text_value")

rule
...
    var magicString = myTable.get(targetItem.name)
2 Likes

it work prefectly thank you very much @rossko57 :heart:

import java.util.Map
 
val Map<String, String> zwaveUid = newHashMap(
 
"uid_Motion_timer_chj" -> "zwave%3Adevice%3Ac3f49cf3%3Anode8",
"uid_Motion_timer_entree" -> "zwave%3Adevice%3Ac3f49cf3%3Anode5",
"uid_Motion_timer_Couloir" -> "zwave%3Adevice%3Ac3f49cf3%3Anode16",
"uid_Motion_timer_cuisine" -> "zwave%3Adevice%3Ac3f49cf3%3Anode10",
"uid_Motion_timer_salon" -> "zwave%3Adevice%3Ac3f49cf3%3Anode17",
"uid_Motion_timer_sam" -> "zwave%3Adevice%3Ac3f49cf3%3Anode13",
"uid_Motion_timer_sdj" -> "zwave%3Adevice%3Ac3f49cf3%3Anode6",
"uid_Motion_timer_chp" -> "zwave%3Adevice%3Ac3f49cf3%3Anode7",
"uid_Motion_timer_sdb" -> "zwave%3Adevice%3Ac3f49cf3%3Anode9",
"uid_Motion_sensibility_chj" -> "zwave%3Adevice%3Ac3f49cf3%3Anode8",
"uid_Motion_sensibility_entree" -> "zwave%3Adevice%3Ac3f49cf3%3Anode5",
"uid_Motion_sensibility_Couloir" -> "zwave%3Adevice%3Ac3f49cf3%3Anode16",
"uid_Motion_sensibility_cuisine" -> "zwave%3Adevice%3Ac3f49cf3%3Anode10",
"uid_Motion_sensibility_salon" -> "zwave%3Adevice%3Ac3f49cf3%3Anode17",
"uid_Motion_sensibility_sam" -> "zwave%3Adevice%3Ac3f49cf3%3Anode13",
"uid_Motion_sensibility_sdj" -> "zwave%3Adevice%3Ac3f49cf3%3Anode6",
"uid_Motion_sensibility_chp" -> "zwave%3Adevice%3Ac3f49cf3%3Anode7",
"uid_Motion_sensibility_sdb" -> "zwave%3Adevice%3Ac3f49cf3%3Anode9") 
 
 // CONFIG TIMER DETECTEUR FIBARO REST API
rule "config timer Motion"
when
     Member of G_Motion_Timer received command
then
     var int configTimer = (triggeringItem.state as Number).intValue
     var jsonTimer = '{"config_6_2": ' + configTimer + '}'
     var httpPutTimer = zwaveUid.get('uid_' + triggeringItem.name)
     logInfo("motion.test", "http://localhost:8080/rest/things/" + httpPutTimer + "/config" + jsonTimer)
     sendHttpPutRequest("http://localhost:8080/rest/things/" + httpPutTimer + "/config", "application/json", jsonTimer)
end
// CONFIG SENSIBILITY DETECTEUR FIBARO REST API
rule "config timer Sensibility"
when
     Member of G_Motion_Sensibility received command
then
     val int configSensi = (triggeringItem.state as Number).intValue
     var jsonSensi = '{"config_1_2": ' + configSensi + '}'
     var httpPutSensi = zwaveUid.get('uid_' + triggeringItem.name)
     logInfo("motion.test", "http://localhost:8080/rest/things/" + httpPutSensi + "/config" + jsonSensi)
     sendHttpPutRequest("http://localhost:8080/rest/things/" + httpPutSensi + "/config", "application/json", jsonSensi)
end

Note that your rule will still fail if that is not a number e.g. NULL

Also the state may or may not be anything like the command, but perhpas that is intended.

This is stiil nonsense, jsonTimer will become a String type

triggeringItem.state --> there is a persistance, so normally never NULL

Yes I understand, I have change by var jsonTimer