View.params object passing to script - error while accessing list dictionary item

I have the following view parameter on a view

shiftPattern : [
		{"dayName": "Monday",    "shiftStart1": "08:00", "shiftStart2": "16:30", "shiftStart3": "00:00", "shiftEnable2": true, "shiftEnable3": true, "shiftNextDay3": true,  "dayNumber": 0},
		{"dayName": "Tuesday",   "shiftStart1": "08:00", "shiftStart2": "16:30", "shiftStart3": "00:00", "shiftEnable2": true, "shiftEnable3": true, "shiftNextDay3": true,  "dayNumber": 1},
		{"dayName": "Wednesday", "shiftStart1": "08:00", "shiftStart2": "16:30", "shiftStart3": "00:00", "shiftEnable2": true, "shiftEnable3": true, "shiftNextDay3": true,  "dayNumber": 2},
		{"dayName": "Thursday",  "shiftStart1": "08:00", "shiftStart2": "16:30", "shiftStart3": "00:00", "shiftEnable2": true, "shiftEnable3": true, "shiftNextDay3": true,  "dayNumber": 3},
		{"dayName": "Friday",    "shiftStart1": "08:00", "shiftStart2": "15:30", "shiftStart3": "22:00", "shiftEnable2": true, "shiftEnable3": true, "shiftNextDay3": false, "dayNumber": 4},
		{"dayName": "Saturday",  "shiftStart1": "07:00", "shiftStart2": "19:00", "shiftStart3": "21:00", "shiftEnable2": true, "shiftEnable3": false,"shiftNextDay3": false, "dayNumber": 5},
		{"dayName": "Sunday",    "shiftStart1": "07:00", "shiftStart2": "19:00", "shiftStart3": "21:00", "shiftEnable2": true, "shiftEnable3": false,"shiftNextDay3": false, "dayNumber": 6}
	]

When I pass it into my Project Library function I have a problem.

return shiftPattern seems to give me the whole list.

return shiftPattern[0] gives me what I expect (although without string quotes):
{dayName: Monday, shiftStart1: 08:00, shiftStart2: 16:30, shiftStart3: 00:00, ...}

return shiftPattern[0]['dayName'] gives me
Error_ExpressionEval("Error executing script for runScript() expression:<my script function name>

A similar problem was discussed on Qualified Value in Change Script Perspective. Playing around with the answers in that I have had success with

shiftPattern[0].value['dayName'] returning ‘Monday’.

Can anyone explain what’s going on?

1 Like

I’m sure someone else probably already knows what the problem is. First thing I would do though since I don’t know - what is the type of shiftPattern[0]?

I had a breakthrough while you were writing your answer and I updated my question a bit. I’m out of trouble for now but would like to understand it more, thanks.

type(shiftPattern[0]) returns
class com.inductiveautomation.perspective.comonImmutableQualifiedValue

So you are getting this type of thing back - ImmutableQualifiedValue

Looks like it has a getValue() function. What do you get from trying that?

getValue(shiftPattern[0]) returns
[{dayName: Monday, shiftStart1: 08:00, shiftStart2: 16:30, shiftStart3: 00:00, ...}]

Interesting, so your code was getValue(shiftPattern[0])? I would have thought it would have needed to have been shiftPattern[0].getValue(). Either way, it looks like now you have access to your list of dictionaries and can do what you need to do now yes?

Yes, that was my code.
shiftPattern[0].getValue()['dayName'] also returns ‘Monday’ as does
shiftPattern[0].value['dayName']

1 Like

I think @pturmel mentioned in another thread that any .getProperty() type functions can also be accessed via just .property and in fact the .property method is slightly more performant.

OK. Thanks for the follow up. I should be able to continue with the real work now!

1 Like

Yes, here:

1 Like

Thanks, Phil, but why did this work from the script console,

	startDate = system.date.addDays(system.date.now(), -1)	# Yesterday for testing.
	startDate

	false = False	# Set the JSON value = Python value.
	true  = True	# ... and here.
	shiftPattern = [
		{"dayName": "Monday",    "shiftStart1": "08:00", "shiftStart2": "16:30", "shiftStart3": "00:00", "shiftEnable2": true, "shiftEnable3": true, "shiftNextDay3": true,  "dayNumber": 0},
		{"dayName": "Tuesday",   "shiftStart1": "08:00", "shiftStart2": "16:30", "shiftStart3": "00:00", "shiftEnable2": true, "shiftEnable3": true, "shiftNextDay3": true,  "dayNumber": 1},
		{"dayName": "Wednesday", "shiftStart1": "08:00", "shiftStart2": "16:30", "shiftStart3": "00:00", "shiftEnable2": true, "shiftEnable3": true, "shiftNextDay3": true,  "dayNumber": 2},
		{"dayName": "Thursday",  "shiftStart1": "08:00", "shiftStart2": "16:30", "shiftStart3": "00:00", "shiftEnable2": true, "shiftEnable3": true, "shiftNextDay3": true,  "dayNumber": 3},
		{"dayName": "Friday",    "shiftStart1": "08:00", "shiftStart2": "15:30", "shiftStart3": "22:00", "shiftEnable2": true, "shiftEnable3": true, "shiftNextDay3": false, "dayNumber": 4},
		{"dayName": "Saturday",  "shiftStart1": "07:00", "shiftStart2": "19:00", "shiftStart3": "21:00", "shiftEnable2": true, "shiftEnable3": false,"shiftNextDay3": false, "dayNumber": 5},
		{"dayName": "Sunday",    "shiftStart1": "07:00", "shiftStart2": "19:00", "shiftStart3": "21:00", "shiftEnable2": true, "shiftEnable3": false,"shiftNextDay3": false, "dayNumber": 6}
	]
	
	shiftCalcs.shiftCalcs.shiftGenerator(startDate, shiftPattern)

but not from an expression binding,

runScript(
	"shiftCalcs.shiftCalcs.shiftGenerator", 
	2000,	// 2 secondupdate for debug.
	{session.custom.datePicker},
	{view.params.config.shiftPattern}
)

My guess would be because in the script console shiftPattern is a List of Dictionaries but in the expression it is a JSON string.

2 Likes

That would explain it. Have you any idea how to convert it in Python?

import json
json.loads(shiftPattern[0])

still gives
Error_ExpressionEval("Error executing script for runScript() expression:<my function name>")

i doubt its cuz its "json", QVs come from tags or databases,

are you getting these from a tag/db?

For now the JSON is pasted into the view.params.config.shiftPattern object.

Use system.util.jsonDecode()

In your script, do something like:

shiftPat = system.util.jsonDecode(shiftPattern)
print shiftPat['dayName']

Are you sure because then i get class com.inductiveautomation.perspective.gateway.script.DotReferenceJythonMap

image

its not a json strng tho

Anyways instead of using runScript() why not use the transform script?

It’s for a plant dashboard view with many widgets and on zoom in the views will be replaced with more detailed views. It makes sense in my application to do this in the Project Library, I think, rather than have the script on each widget.

It’s interesting that the script transform can read shiftPattern as a list of dictionaries but the Project Library script can’t. It’s frustrating too!