Time stamp / get Time

Dear all,

I am trying to get some information from my old drilling machine.
With the help of some ultrasonic sensors I would like to measure the distance of a part and also generate a timestamp so I know how long the part was resting under the drill head.

Therefore I created a code which is unfortunately not working.
Task:
The distance is uploaded every second, so far so good.
If the distance is below 10 cm the part is set to 1 and as long as it is 1 time should be added.

My idea was to set a timestamp when it becomes 1 and another one when it becomes 0 and in the end I will subtract the 1 timestamp from the 0 timestamp.

Attached you can find my code

let part;
let end;
let start;
let processingTime;

if (msg.payload.distancecm < 10) {
part = 1;
} else {
part = 0;
}

if (part === 1) {
start = new Date();
} else {
(part === 0){
end = new Date();
processingTime = end.getTime() - start.getTime();
}
}

msg.payload="[{"Timestamp" :"" + Date() + "","distance" :"" + msg.payload.distancecm + "","part" :"" + part + "","end" :"" + end + "","start" :"" + start + "","processingTime" :"" + processingTime + ""}]"
return msg;

Thank you very much in advance for your help.

Greetings

One thing, you have an if...else... but you have a conditional after the else so I think you want this

if (part === 1) {
start = new Date();
} else {
(part === 0){
end = new Date();
processingTime = end.getTime() - start.getTime();
}

to be

if (part === 1) {
start = new Date();
} else if (part === 0){
end = new Date();
processingTime = end.getTime() - start.getTime();
}

1 Like

Also you will need to save the start time in the node context and pick it up again when you determine the end time. Variables set in a function node are lost at the end of the function unless you save them in the context. Look in the node-red docs on writing functions to find details of how to do that.

1 Like

Hi all, I have a window contact and I want when the window is closed, your timestamp is set.
But the function only works if the function says "opened at" and then "closed at". Swap is it (as I need it) arises an error :frowning:

var date=Date.now();

var seconds=(((date % 3600000) % 60000) / 1000).toFixed(0);
var minutes=((date % 3600000) / 60000).toFixed(0);
var hours=date / 3600000;
var ms=((date % 3600000) % 60000) % 1000;
var days=hours / 24;
var weeks=(days / 7).toFixed(0);
days=(days % 7).toFixed(0);
hours=(hours % 24).toFixed(0);

if (hours<10)
{
hours= "0"+String(hours);
}

if (minutes<10)
{
minutes= "0"+String(minutes);
}

if (seconds<10)
{
seconds= "0"+String(seconds);
}

//convert time:
var time=String(hours) + ":" + String(minutes) + ":" + String(seconds);

var output= flow.get("doorContact1-State")? "opened at ("+time+")": "closed at ("+time+")";
var msgOut= {payload: output};
return msgOut;

I think I would just have to swap true and false

Try

[{"id":"45661fb827a72921","type":"function","z":"f859c7434a67ede7","name":"function 3","func":"const date = new Date()\nconst doorContact1_State = msg.payload\n\n//convert time:\nconst time = date.toLocaleString()\n\nconst output= (doorContact1_State)? \"opened at (\"+time+\")\": \"closed at (\"+time+\")\"\n\nmsg.payload = output\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":740,"y":1060,"wires":[["b7bcecf8b14cacf8"]]},{"id":"058abd63956e9ab7","type":"inject","z":"f859c7434a67ede7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":490,"y":1060,"wires":[["45661fb827a72921"]]},{"id":"b7bcecf8b14cacf8","type":"debug","z":"f859c7434a67ede7","name":"debug 23","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":980,"y":1060,"wires":[]}]

the whole function can be reduced to (msg.payload is taking the place of your flow context)

const date = new Date()
const doorContact1_State = msg.payload

//convert time:
const time = date.toLocaleString()

const output= (doorContact1_State) ? "opened at (" + time + ")":  "closed at (" + time + ")"

msg.payload = output

return msg

One thing to note, Javascript does not like '-', use '_' instead, also you are mixing numbers and strings which will not work (.toFixed(0) gives a string, NOT a number)

1 Like

Thank you that worked.
Now I just have to configure the date. I do not like it yet

I would like it like this: 26.01.2023 / 22:53

What country format is this?

time = `${date.getDate()}.${date.getMonth() + 1}.${date.getFullYear()} / ${date.getHours()}:${date.getMinutes()}`

gives 27.1.2023 / 0:4

or

const options = {
	day: '2-digit', 
  	month: '2-digit',
  	year: 'numeric', 
  	hour: '2-digit', 
  	minute: '2-digit', 
  	hour12: false

}

time = date.toLocaleString('de-DE', options).replace(', ', ' / ')

gives 27.01.2023 / 00:04

Germany :slight_smile:

Can I just paste that into the function you gave me above?

Yes, like so;

const date = new Date()
const doorContact1_State = msg.payload

const options = {
    day: '2-digit',
    month: '2-digit',
    year: 'numeric',
    hour: '2-digit',
    minute: '2-digit',
    hour12: false

}

//convert time:
const time = date.toLocaleString('de-DE', options).replace(', ', ' / ')

const output = (doorContact1_State) ? "opened at (" + time + ")" : "closed at (" + time + ")"

msg.payload = output

return msg

The standard date/time format for the 'de-DE' locale is 27.01.2023, 15:39 so if you want to stick to that just use const time = date.toLocaleString('de-DE', options)