Perspective - How to initialize a datetime input from script

I’m searching for a way to initialize datetime inputs on session startup.

I tried the following code in a containers startup event:

	now = system.date.now()
	self.getChild('dt-ende').props.value = now.getTime()
	self.getChild('dt-anfang').props.value = system.date.addHours(now, -8).getTime()

This fails, because at that time there is no value property:
AttributeError: 'com.inductiveautomation.perspective.gateway.script' object has no attribute 'value'
The value property appears only after a value has been entered or selected.
Is there something i’m missing?

Another question: What is the best place to put a change script for this component? A change script for the value property is deleted when this property is deleted.

The DateTimeInput Component has no value property before a value has been selected. Try assigning the value you want into one of the formatted values available to you.
A team-member pointed out that you’re attempting to do this in a Session startup script, which is not going to work, as the View may not even technically exist, and even if it does, the Session Startup script would not be the place to do this.

Select your View node, then right-click the View node and select “Configure Events”. Place your code into a VIEW Startup script and it should work, assuming the DateTime Inputs are in the root Container, and not any deeper.

Ok, thank you. I was hoping for a locale independent way to assign a value. Working with the formatted value will make the script more complicated when i have to deal will multiple languages.
Maybe you can add the millisecond time as a feature in one of the next versions.

2 Likes

system.date.fromMillis() and system.date.toMillis()? But do see @cmallonee's edit about what's either an initialization race condition or undefined behavior.

It’s not exactly a race condition per se…

Views do not exist in a session unless they are part of your current “Page’s” Page Configuration in some manner. Right now, that comes down to a child component (Embedded View or the descendant Embedded Views of any Embedded View), a Popup (but only if it’s open), or a Docked View which is attached to the Page Configuration in use (and because of a couple of open bugs, only the *most recently opened Docked View_ if there are more than one on a given side).

If a View exists in your project but it doesn’t meet one of those criteria, then the View does not exist within the session. You may not modify any values for that View or its children components, and no listeners within that View will operate.

It’s also not undefined behavior, because we know exactly what will happen; nothing.

If at any time you need to manipulate something within a View, or refer to some property within a View, then that View needs to be part of your current session or you’re going to run into NoneType references.

This is why parameter-passing and session properties are so important now.

Initialization is also possible in one-shot instances like Chi’s use-case, but that needs to be handled in the View Startup.

1 Like

Thanks for your response. My script was in the startup event of a container inside a view, so i never used a Session startup script.
Info for others reading this: Putting the code in the Views startup event does not work. If the components are in the root container, the script has to be in the root nodes startup event.

My orginal problem was kind of self made. In an earlier version of the script i forgot the .props in the properties path, so accessing self.getChild('dt-ende').value of course caused an error.
Then i changed that script to the version posted above and launched a session but did not click 'Save' in the designer before. I did not realize that 'Tools->Launch perspective' launches the last saved version of the project.

1 Like