[solved] using bag variable in transition

I am trying to fire a transition depending on the value a bag variable.
This is the transition code

code and (bag[“motion”] <= 3)
[/code]
I am aware that I need a device variable to change, so I am using the tripped variable of a motion detector.
My problem is that the transition never fires, even when the motion untrips.

[quote=“pls90, post:1, topic:197579”]I am trying to fire a transition depending on the value a bag variable.
This is the transition code

code and (bag[“motion”] <= 3)
[/code]
I am aware that I need a device variable to change, so I am using the tripped variable of a motion detector.
My problem is that the transition never fires, even when the motion untrips.[/quote]

did you try toumber(bag[“motion”]) to make sure you are not comparing a string with a number ?

yep, I did that. Just tried again to be sure, no change.

did you use the proper upper case : Bag[“valeur”] ?

otherwise set debug mode in, reload luup and try to capture in the log the trace of the workflow evaluating it while the triggers happens, then we can see in the logs

OK, I did use lower case characters for “bag”. Changing that didn’t have any effect.
So I enabled debug mode and captures this

5011/07/17 10:42:51.577luup_log:309: ALTUI: debug: _internalVariableWatchCB(0-145,urn:micasaverde-com:serviceId:SecuritySensor1,Tripped,old:'1',new:'0') - (Wkflow) <0x7051e520> 5011/07/17 10:42:51.578luup_log:309: ALTUI: debug: findWatch(0-145,urn:micasaverde-com:serviceId:SecuritySensor1,Tripped) <0x7051e520> 5011/07/17 10:42:51.585luup_log:309: ALTUI: debug: registeredWatches: { "0-10": { "urn:micasaverde-com:serviceId:SecuritySensor1": { "Tripped": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-4" } ] } } } }, "0-179": { "urn:rts-services-com:serviceId:DayTime": { "Status": { "LastUpdate": 1510047580, "LastOld": "0", "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-2" }, { "WorkflowAltuiID": "0-3" }, { "WorkflowAltuiID": "0-4" }, { "WorkflowAltuiID": "0-6" }, { "WorkflowAltuiID": "0-7" } ] }, "LastNew": "1" } } }, "0-96": { "urn:upnp-org:serviceId:SwitchPower1": { "Status": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-4" } ] } } } }, "0-145": { "urn:micasaverde-com:serviceId:SecuritySensor1": { "Tripped": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-8" } ] } } } }, "0-160": { "urn:micasaverde-com:serviceId:SecuritySensor1": { "Tripped": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-7" } ] } } } }, "0-181": { "urn:micasaverde-com:serviceId:SecuritySensor1": { "Tripped": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-7" } ] } } } }, "0-76": { "urn:micasaverde-com:serviceId:HouseModes1": { "HMode": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-2" }, { "WorkflowAltuiID": "0-3" }, { "WorkflowAltuiID": "0-4" }, { "WorkflowAltuiID": "0-6" }, { "WorkflowAltuiID": "0-7" } ] } } } }, "0-115": { "urn:upnp-org:serviceId:SwitchPower1": { "Status": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-7" } ] } } } }, "0-101": { "urn:micasaverde-com:serviceId:SecuritySensor1": { "Tripped": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-3" } ] } } } }, "0-290": { "urn:upnp-org:serviceId:SwitchPower1": { "Status": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-6" } ] } } } }, "0-98": { "urn:upnp-org:serviceId:SwitchPower1": { "Status": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-3" } ] } } } }, "0-13": { "urn:micasaverde-com:serviceId:SecuritySensor1": { "Tripped": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-2" } ] } } } }, "0-43": { "urn:upnp-org:serviceId:SwitchPower1": { "Status": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-2" } ] } } } }, "0-289": { "urn:upnp-org:serviceId:SwitchPower1": { "Status": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-6" } ] } } } }, "0-152": { "urn:micasaverde-com:serviceId:SecuritySensor1": { "Tripped": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-2" } ] } } } } } <0x7051e520> 5011/07/17 10:42:51.587luup_log:309: ALTUI: debug: -----> evaluateExpression() { "workflow": [ { "WorkflowAltuiID": "0-8" } ] } <0x7051e520> 5011/07/17 10:42:51.588luup_log:309: ALTUI: debug: Wkflow - executeWorkflows(309 , { "device": "0-145", "service": "urn:micasaverde-com:serviceId:SecuritySensor1", "watch": { "LastUpdate": 1510047771, "LastOld": "1", "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-8" } ] }, "LastNew": "0" }, "variable": "Tripped" }) <0x7051e520> 5011/07/17 10:42:51.589luup_log:309: ALTUI: debug: Wkflow - executeWorkflows limited to specific workflows <0x7051e520> 5011/07/17 10:42:51.589luup_log:309: ALTUI: debug: Wkflow - evalWorkflowState(309, 8), workflow:Clone of lights_table <0x7051e520> 5011/07/17 10:42:51.590luup_log:309: ALTUI: debug: Wkflow - findStartState(8) <0x7051e520> 5011/07/17 10:42:51.590luup_log:309: ALTUI: debug: Wkflow - findStartState(8) returns acfcf9e5-19b7-47b2-adb5-564c9731e21d <0x7051e520> 5011/07/17 10:42:51.590luup_log:309: ALTUI: debug: Wkflow - active state:0b6db066-cf16-466e-804b-b9d1d2a39ddf, auto on <0x7051e520> 5011/07/17 10:42:51.591luup_log:309: ALTUI: debug: Wkflow - evaluateStateTransition(309,Start,8) <0x7051e520> 5011/07/17 10:42:51.591luup_log:309: ALTUI: debug: Wkflow - link props:{ "onEnterLua": "", "onEnter": [ ], "onEnterScenes": [ ], "stateinfo": { "bStart": true }, "expired": false, "onExit": [ ], "onExitScenes": [ ], "onExitLua": "" } <0x7051e520> 5011/07/17 10:42:51.592luup_log:309: ALTUI: debug: Wkflow - link has a timer. <0x7051e520> 5011/07/17 10:42:51.592luup_log:309: ALTUI: debug: Wkflow - blocked:, paused: <0x7051e520> 5011/07/17 10:42:51.592luup_log:309: ALTUI: debug: Wkflow - getStateTransitions(309) <0x7051e520> 5011/07/17 10:42:51.593luup_log:309: ALTUI: debug: Wkflow - getStateTransitions(309) returns 2 transitions <0x7051e520> 5011/07/17 10:42:51.593luup_log:309: ALTUI: debug: Wkflow - evaluateStateTransition(309,low_motion_count,8) <0x7051e520> 5011/07/17 10:42:51.594luup_log:309: ALTUI: debug: Wkflow - link props:{ "workflows": [ ], "comment": "", "timer": "", "conditions": [ { "luaexpr": "(new==\"0\") AND (tonumber(Bag[\"motion\"]) <= 3)", "triggeronly": true, "device": "0-145", "variable": "Tripped", "service": "urn:micasaverde-com:serviceId:SecuritySensor1" } ], "duration": "", "smooth": true } <0x7051e520> 5011/07/17 10:42:51.596luup_log:309: ALTUI: debug: Wkflow - Condition (new=="0") AND (tonumber(Bag["motion"]) <= 3) is TriggerOnly and watchevent:{ "device": "0-145", "service": "urn:micasaverde-com:serviceId:SecuritySensor1", "watch": { "LastUpdate": 1510047771, "LastOld": "1", "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-8" } ] }, "LastNew": "0" }, "variable": "Tripped" } <0x7051e520> 5011/07/17 10:42:51.596luup_log:309: ALTUI: debug: _evaluateUserExpression(309,145,urn:micasaverde-com:serviceId:SecuritySensor1,Tripped,1,0,1510047771,(new=="0") AND (tonumber(Bag["motion"]) <= 3)) <0x7051e520> 0111/07/17 10:42:51.597luup_log:309: ALTUI: error: loadstring return function(devid, lul_service, lul_variable, expr) local old='1' local new='0' local lastupdate=1510047771 local now=os.time() local results= {(new=="0") AND (tonumber(Bag["motion"]) <= 3)}-- eventually returns 2 results, cond and delay return results end failed to compile, msg=[string "ALTUI - evaluateUserExpression"]:6: '}' expected near 'AND' <0x7051e520> 5011/07/17 10:42:51.597luup_log:309: ALTUI: debug: Wkflow - evaluateStateTransition(309,high_motion_count,8) <0x7051e520> 5011/07/17 10:42:51.599luup_log:309: ALTUI: debug: Wkflow - link props:{ "workflows": [ ], "comment": "", "timer": "", "conditions": [ { "luaexpr": "(new==\"0\") and (bag[\"motion\"] > 3)", "triggeronly": false, "device": "0-145", "variable": "Tripped", "service": "urn:micasaverde-com:serviceId:SecuritySensor1" } ], "duration": "", "smooth": true } <0x7051e520> 5011/07/17 10:42:51.599luup_log:309: ALTUI: debug: Wkflow - Condition (new=="0") and (bag["motion"] > 3) is evaluated in real time - no TriggerOnly <0x7051e520> 5011/07/17 10:42:51.600luup_log:309: ALTUI: debug: _evaluateUserExpression(309,145,urn:micasaverde-com:serviceId:SecuritySensor1,Tripped,,0,1510047771,(new=="0") and (bag["motion"] > 3)) <0x7051e520> 5011/07/17 10:42:51.600luup_log:309: ALTUI: debug: Exception occured, Err Msg: [string "ALTUI - evaluateUserExpression"]:6: attempt to index global 'bag' (a nil value) <0x7051e520> 5011/07/17 10:42:51.601luup_log:309: ALTUI: debug: Wkflow - No valid transition found <0x7051e520> 5011/07/17 10:42:51.601luup_log:309: ALTUI: debug: -----> DataProviders() null <0x7051e520> 5011/07/17 10:42:51.608luup_log:309: ALTUI: debug: registeredWatches: { "0-10": { "urn:micasaverde-com:serviceId:SecuritySensor1": { "Tripped": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-4" } ] } } } }, "0-179": { "urn:rts-services-com:serviceId:DayTime": { "Status": { "LastUpdate": 1510047580, "LastOld": "0", "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-2" }, { "WorkflowAltuiID": "0-3" }, { "WorkflowAltuiID": "0-4" }, { "WorkflowAltuiID": "0-6" }, { "WorkflowAltuiID": "0-7" } ] }, "LastNew": "1" } } }, "0-96": { "urn:upnp-org:serviceId:SwitchPower1": { "Status": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-4" } ] } } } }, "0-145": { "urn:micasaverde-com:serviceId:SecuritySensor1": { "Tripped": { "LastUpdate": 1510047771, "LastOld": "1", "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-8" } ] }, "LastNew": "0" } } }, "0-160": { "urn:micasaverde-com:serviceId:SecuritySensor1": { "Tripped": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-7" } ] } } } }, "0-181": { "urn:micasaverde-com:serviceId:SecuritySensor1": { "Tripped": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-7" } ] } } } }, "0-76": { "urn:micasaverde-com:serviceId:HouseModes1": { "HMode": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-2" }, { "WorkflowAltuiID": "0-3" }, { "WorkflowAltuiID": "0-4" }, { "WorkflowAltuiID": "0-6" }, { "WorkflowAltuiID": "0-7" } ] } } } }, "0-115": { "urn:upnp-org:serviceId:SwitchPower1": { "Status": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-7" } ] } } } }, "0-101": { "urn:micasaverde-com:serviceId:SecuritySensor1": { "Tripped": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-3" } ] } } } }, "0-290": { "urn:upnp-org:serviceId:SwitchPower1": { "Status": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-6" } ] } } } }, "0-98": { "urn:upnp-org:serviceId:SwitchPower1": { "Status": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-3" } ] } } } }, "0-13": { "urn:micasaverde-com:serviceId:SecuritySensor1": { "Tripped": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-2" } ] } } } }, "0-43": { "urn:upnp-org:serviceId:SwitchPower1": { "Status": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-2" } ] } } } }, "0-289": { "urn:upnp-org:serviceId:SwitchPower1": { "Status": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-6" } ] } } } }, "0-152": { "urn:micasaverde-com:serviceId:SecuritySensor1": { "Tripped": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-2" } ] } } } } } <0x7051e520> 1011/07/17 10:42:51.609AlarmManager::AddRelativeAlarm current time 1510047771 delay 0 type 45 <0x7051e520> 1011/07/17 10:42:51.609AlarmManager::AddAbsoluteAlarm alarm 0xf82a98 entry 0x1fe1b00 id 266 type 45 param=(nil) ent

I realized one other thing: on the latest version of AltUI the “trigger only” icon is NOT displayed any more, you only see that if you open the editor.
See screenshot attached.

EDIT:
I got one transition working. It seems that spaces in the evaluation and upper/lower case do make a difference. Sorry for my ignorance on that part.
Still there is something odd.
This is working fine in one transition

(new=="0") and (Bag["motion"]<=3)

This is NOT working in an other transition

(new=="0") and (Bag["motion"]>3)

EDIT2
this is capture of transition not working

Line 13057: 5011/07/17 12:15:58.064luup_log:309: ALTUI: debug: _internalVariableWatchCB(0-145,urn:micasaverde-com:serviceId:SecuritySensor1,Tripped,old:'1',new:'0') - (Wkflow) <0x70ea0520> Line 13058: 5011/07/17 12:15:58.065luup_log:309: ALTUI: debug: findWatch(0-145,urn:micasaverde-com:serviceId:SecuritySensor1,Tripped) <0x70ea0520> Line 13059: 5011/07/17 12:15:58.075luup_log:309: ALTUI: debug: registeredWatches: { "0-160": { "urn:micasaverde-com:serviceId:SecuritySensor1": { "Tripped": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-7" } ] } } } }, "0-10": { "urn:micasaverde-com:serviceId:SecuritySensor1": { "Tripped": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-4" } ] } } } }, "0-179": { "urn:rts-services-com:serviceId:DayTime": { "Status": { "LastUpdate": 1510049863, "LastOld": "1", "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-2" }, { "WorkflowAltuiID": "0-3" }, { "WorkflowAltuiID": "0-4" }, { "WorkflowAltuiID": "0-6" }, { "WorkflowAltuiID": "0-7" }, { "WorkflowAltuiID": "0-8" } ] }, "LastNew": "0" } } }, "0-96": { "urn:upnp-org:serviceId:SwitchPower1": { "Status": { "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-4" } ] } } } }, "0-145": { "urn:micasaverde-com:serviceId:SecuritySensor1": { "Tripped": { "LastUpdate": 1510052155, "LastOld": "0", "Expressions": { "workflow": [ { "WorkflowA... Line 13061: 5011/07/17 12:15:58.078luup_log:309: ALTUI: debug: -----> evaluateExpression() { "workflow": [ { "WorkflowAltuiID": "0-8" } ] } <0x70ea0520> Line 13062: 5011/07/17 12:15:58.079luup_log:309: ALTUI: debug: Wkflow - executeWorkflows(309 , { "device": "0-145", "service": "urn:micasaverde-com:serviceId:SecuritySensor1", "watch": { "LastUpdate": 1510053358, "LastOld": "1", "Expressions": { "workflow": [ { "WorkflowAltuiID": "0-8" } ] }, "LastNew": "0" }, "variable": "Tripped" }) <0x70ea0520> Line 13063: 5011/07/17 12:15:58.080luup_log:309: ALTUI: debug: Wkflow - executeWorkflows limited to specific workflows <0x70ea0520> Line 13064: 5011/07/17 12:15:58.080luup_log:309: ALTUI: debug: Wkflow - evalWorkflowState(309, 8), workflow:Clone of lights_table <0x70ea0520> Line 13065: 5011/07/17 12:15:58.081luup_log:309: ALTUI: debug: Wkflow - findStartState(8) <0x70ea0520> Line 13066: 5011/07/17 12:15:58.081luup_log:309: ALTUI: debug: Wkflow - findStartState(8) returns acfcf9e5-19b7-47b2-adb5-564c9731e21d <0x70ea0520> Line 13067: 5011/07/17 12:15:58.081luup_log:309: ALTUI: debug: Wkflow - active state:0b6db066-cf16-466e-804b-b9d1d2a39ddf, auto on <0x70ea0520> Line 13068: 5011/07/17 12:15:58.082luup_log:309: ALTUI: debug: Wkflow - evaluateStateTransition(309,Start,8) <0x70ea0520> Line 13069: 5011/07/17 12:15:58.083luup_log:309: ALTUI: debug: Wkflow - link props:{ "onExitLua": "", "onEnter": [ ], "onEnterScenes": [ ], "stateinfo": { "bStart": true }, "expired": false, "onExit": [ ], "onExitScenes": [ ], "onEnterLua": "" } <0x70ea0520> Line 13070: 5011/07/17 12:15:58.083luup_log:309: ALTUI: debug: Wkflow - link has a timer. <0x70ea0520> Line 13071: 5011/07/17 12:15:58.084luup_log:309: ALTUI: debug: Wkflow - blocked:, paused: <0x70ea0520> Line 13072: 5011/07/17 12:15:58.084luup_log:309: ALTUI: debug: Wkflow - getStateTransitions(309) <0x70ea0520> Line 13073: 5011/07/17 12:15:58.084luup_log:309: ALTUI: debug: Wkflow - getStateTransitions(309) returns 2 transitions <0x70ea0520> Line 13074: 5011/07/17 12:15:58.085luup_log:309: ALTUI: debug: Wkflow - evaluateStateTransition(309,low_motion_count,8) <0x70ea0520> Line 13075: 5011/07/17 12:15:58.086luup_log:309: ALTUI: debug: Wkflow - link props:{ "workflows": [ ], "comment": "", "timer": "", "conditions": [ { "luaexpr": "(new==\"0\") and (Bag[\"motion\"]<=3)", "triggeronly": false, "device": "0-145", "variable": "Tripped", "service": "urn:micasaverde-com:serviceId:SecuritySensor1" } ], "duration": "", "smooth": true } <0x70ea0520> Line 13076: 5011/07/17 12:15:58.087luup_log:309: ALTUI: debug: Wkflow - Condition (new=="0") and (Bag["motion"]<=3) is evaluated in real time - no TriggerOnly <0x70ea0520> Line 13077: 5011/07/17 12:15:58.087luup_log:309: ALTUI: debug: _evaluateUserExpression(309,145,urn:micasaverde-com:serviceId:SecuritySensor1,Tripped,,0,1510053358,(new=="0") and (Bag["motion"]<=3)) <0x70ea0520> Line 13084: 5011/07/17 12:15:58.090luup_log:309: ALTUI: debug: setVariableIfChanged(urn:upnp-org:serviceId:altui1,WorkflowsVariableBag,{ "0-2": [ ], "0-3": [ ], "0-8": { "counter": 0, "motion": 4 }, "0-1": [ ], "0-4": [ ], "0-5": [ ], "0-6": [ ], "0-7": [ ] },309) <0x70ea0520> Line 13085: 5011/07/17 12:15:58.100luup_log:309: ALTUI: debug: Evaluation of user watch expression returned: [ false ] <0x70ea0520> Line 13086: 5011/07/17 12:15:58.101luup_log:309: ALTUI: debug: Wkflow - evaluateStateTransition(309,high_motion_count,8) <0x70ea0520> Line 13087: 5011/07/17 12:15:58.102luup_log:309: ALTUI: debug: Wkflow - link props:{ "workflows": [ ], "comment": "", "timer": "", "conditions": [ { "luaexpr": "(new==\"0\") and (bag[\"motion\"] > 3)", "triggeronly": false, "device": "0-145", "variable": "Tripped", "service": "urn:micasaverde-com:serviceId:SecuritySensor1" } ], "duration": "", "smooth": true } <0x70ea0520> Line 13088: 5011/07/17 12:15:58.103luup_log:309: ALTUI: debug: Wkflow - Condition (new=="0") and (bag["motion"] > 3) is evaluated in real time - no TriggerOnly <0x70ea0520> Line 13089: 5011/07/17 12:15:58.103luup_log:309: ALTUI: debug: _evaluateUserExpression(309,145,urn:micasaverde-com:serviceId:SecuritySensor1,Tripped,,0,1510053358,(new=="0") and (bag["motion"] > 3)) <0x70ea0520> Line 13090: 5011/07/17 12:15:58.104luup_log:309: ALTUI: debug: Exception occured, Err Msg: [string "ALTUI - evaluateUserExpression"]:6: attempt to index global 'bag' (a nil value) <0x70ea0520> Line 13091: 5011/07/17 12:15:58.105luup_log:309: ALTUI: debug: Wkflow - No valid transition found <0x70ea0520>

in the first log the issue is the capital letter “AND” keyword. in lua it is “and”

[code]>>local results= {(new==“0”) AND (tonumber(Bag[“motion”]) <= 3)}-- eventually returns 2 results, cond and delay

failed to compile, msg=[string “ALTUI - evaluateUserExpression”]:6: ‘}’ expected near ‘AND’ <0x7051e520>
[/code]
in the second log the issue is the lower case bag word. it should be Bag

[code]>>Line 13089: 5011/07/17 12:15:58.103luup_log:309: ALTUI: debug: _evaluateUserExpression(309,145,urn:micasaverde-com:serviceId:SecuritySensor1,Tripped,0,1510053358,(new==“0”) and (bag[“motion”] > 3)) <0x70ea0520>

Line 13090: 5011/07/17 12:15:58.104luup_log:309: ALTUI: debug: Exception occured, Err Msg: [string “ALTUI - evaluateUserExpression”]:6: attempt to index global ‘bag’ (a nil value) <0x70ea0520>
[/code]

thx for the trigger icon notice, it is a genuine bug

Hm, I have no ideo why bag is written in small letters in the log. It sure is written “Bag” in the transition editor.
I even cloned the working transition and only changed the “<=3” into “>3” so this time it is not about lower/upper case, please see screenshot.

Could it be that Bag[“motion”] is a string? If so, you might find that you need to use tonumber() with it. I.e., tonumber(Bag[“motion”]) < 3 as the expression.

I don’t think it’s a string as one transition works perfectly. The other one is a clone of the working one, only the condtion part was altered (‘<=3’ changed to ‘>3’).
But I’ll give it a try again.

edit
I can confirm it is NOT a string, so no need for tonumer().
I deleted the transition that did not work, cloned it again and changed the condition part.
NOW, after deleting the transition, not only overwriting the condition, it started working.

@amg, thank you!