Change string data to time

hi everyone i would like to know if there is any way to change a string or int variable to time, I currently have something like this

Contador = system.tag.read(dateFormat(addSeconds(midnight(now()),"[.]MemTimeDiff","HH:mm:ss"))

You’re mixing Jython and the expression language here. Where are you trying to use this? Also, either way, the conversion from string to time would need to be applied to the tag value, rather than trying to read the value of the the string to time conversion as your code above does.

I want it to save the data in a database

if currentValue.value == False and currentValue.value != previousValue.value and not initialChange:
                    INS = "INSERT INTO PAROS ( Linea, Maquina, TipoParo, TiempoParo, Fecha) VALUES ( ?, ?, ?, ?, ?)"
                                
                    Contador = system.tag.read(dateFormat(addSeconds(midnight(now()),"[.]MemTimeDiff","HH:mm:ss"))
                    Fecha = system.date.now()
                                
                    system.db.runPrepUpdate(INS,[ "Item", "Prueba", "Prueba", Contador.value, Fecha],"TOY")
  1. These are all expression language and will not work here:
    dateFormat(addSeconds(midnight(now(
    You will need to use python functions to manipulate the date.
  2. I don’t believe you can use a relative tag path here–replace [.]MemTimeDiff with the full path to the tag–something like the example in the next point. EDIT: You actually can use this here, as others have pointed out.
  3. If your tag read was working, it would give you a qualified value which isn’t what you want to apply the other time manipulations to. You want to extract the value out of that by adding .value before trying to manipulate it:
    system.tag.read("[default]MemTimeDiff").value
  4. You need to read the tag value (something like previous line), and then manipulate it with scripting language (not expression language). Do not try to manipulate it within the system.tag.read as in your example. You’ll end up with something like the following (untested).
timeDiff = system.tag.read("[default]MemTimeDiff").value
Contador = system.date.format(system.date.addSeconds(system.date.midnight(system.date.now()), timeDiff), "HH:mm:ss")
1 Like

@witman this topic will help at least clarify the relative tagpath (tl;dr It’s a tag event script)

1 Like

You actually can use relative paths in Jython :slight_smile: for example in a tag change script

1 Like

I get an error back

([default]CONTEO DE FALLA/Falla safety fance open 1, valueChanged) Error executing tag event script: Traceback (most recent call last): File "tagevent:valueChanged", line 6, in valueChanged AttributeError: 'com.inductiveautomation.ignition.common.script.Imm' object has no attribute 'data'

com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File "", line 6, in valueChanged AttributeError: 'com.inductiveautomation.ignition.common.script.Imm' object has no attribute 'data'

at org.python.core.Py.AttributeError(Py.java:173)

at org.python.core.PyObject.noAttributeError(PyObject.java:930)

at org.python.core.PyObject.getattr(PyObject.java:925)

at org.python.pycode._pyx97434703.valueChanged$1(:9)

at org.python.pycode._pyx97434703.call_function()

at org.python.core.PyTableCode.call(PyTableCode.java:165)

at org.python.core.PyBaseCode.call(PyBaseCode.java:301)

at org.python.core.PyFunction.function___call__(PyFunction.java:376)

at org.python.core.PyFunction.call(PyFunction.java:371)

at org.python.core.PyFunction.call(PyFunction.java:361)

at org.python.core.PyFunction.call(PyFunction.java:356)

at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:649)

at com.inductiveautomation.ignition.common.sqltags.scripts.TagScriptManager$FunctionInvokerImpl.run(TagScriptManager.java:493)

at com.inductiveautomation.ignition.common.sqltags.scripts.AbstractTagScript.invoke(AbstractTagScript.java:33)

at com.inductiveautomation.ignition.common.sqltags.scripts.TagScriptManager$Task.invoke(TagScriptManager.java:442)

at com.inductiveautomation.ignition.common.sqltags.scripts.TagScriptManager$TagScriptDispatcher.run(TagScriptManager.java:405)

at com.inductiveautomation.ignition.common.execution.impl.BasicExecutionEngine$ThrowableCatchingRunnable.run(BasicExecutionEngine.java:518)

at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)

at java.util.concurrent.FutureTask.run(Unknown Source)

at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(Unknown Source)

at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

Caused by: org.python.core.PyException: Traceback (most recent call last): File "", line 6, in valueChanged AttributeError: 'com.inductiveautomation.ignition.common.script.Imm' object has no attribute 'data'

... 24 common frames omitted

this is the code

if currentValue.value == False and currentValue.value != previousValue.value and not initialChange:
				INS = "INSERT INTO PAROS (Linea, Maquina, TipoParo, TiempoParo, Fecha) VALUES (?, ?, ?, ?, ?)"			
				
				timeDiff = system.tag.read("[.]MemTimeDiff").value
				Contador = system.date.format(system.date.addSeconds(system.date.midnight(system.data.now()), timeDiff), "HH:mm:ss")
				Fecha = system.date.now()	
				
				system.db.runPrepUpdate(INS,["Item","Prueba","Prueba",Contador.value,Fecha],"TOPY_MW")

You have a typo in your code.

You have data when you should have date

if currentValue.value == False and currentValue.value != previousValue.value and not initialChange:
				INS = "INSERT INTO PAROS (Linea, Maquina, TipoParo, TiempoParo, Fecha) VALUES (?, ?, ?, ?, ?)"			
				
				timeDiff = system.tag.read("[.]MemTimeDiff").value
				Contador = system.date.format(system.date.addSeconds(system.date.midnight(system.date.now()), timeDiff), "HH:mm:ss")
				Fecha = system.date.now()	
				
				system.db.runPrepUpdate(INS,["Item","Prueba","Prueba",Contador.value,Fecha],"TOPY_MW")

([default]CONTEO DE FALLA/Falla safety fance open 1,
valueChanged) Error executing tag event script:
Traceback (most recent call last): File
"tagevent:valueChanged", line 6, in valueChanged
TypeError: addSeconds(): 2nd arg can't be coerced
to int

It doesn't like timeDiff, is that tag of type string? You can make the timeDiff tag of type int or maybe use

int(system.tag.read("[.]MemTimeDiff").value)

is already saving time.

only instead of saving 40 min. save me 40 seconds.

	if currentValue.value == False and currentValue.value != previousValue.value and not initialChange:
					INS = "INSERT INTO PAROS (Linea, Maquina, TipoParo, TiempoParo, Fecha) VALUES (?, ?, ?, ?, ?)"			
					
					timeDiff = int(system.tag.read("[.]../MemTimeDiff").value)
					Contador = system.date.format(system.date.addSeconds(system.date.midnight(system.date.now()), timeDiff), "HH:mm:ss")
					Fecha = system.date.now()	
					
					system.db.runPrepUpdate(INS,["Item","Prueba","Prueba",Contador,Fecha],"TOPY_MW")

Is [.]../MemTimeDiff minutes? If so, you need to add minutes, not seconds:

Contador = system.date.format(system.date.addMinutes(system.date.midnight(system.date.now()), timeDiff), "HH:mm:ss")
1 Like