Easy way of adding only mytimes to a msg

The node-red-contrib-simpletime does what its says on the packet and returns the current (DST corrected) time

Instead of writing my own boiler plate code to add hh:mm:ss to status/logging messages I'd thought I'd just use this node in the flow and just add in mytimes but I'm struggling to come up with a simple way of doing it without adding in all the others as well

Any clever/simple ideas?

Does it matter? Or is it the clutter in the debug you don't like?

Yep
As well as increasing message size unnecessarily

I just feel there is an elegant NR way that I'm missing here :slight_smile:

[edit]
Also - I felt a good solution might have wider application for other things :slight_smile:
[/edit]

I could get my coding hat on and modify the node to have checkboxes and do a PR request to @paul-reed of course :slight_smile:

If you don't want to change the node or create your own function node that does what you want, you can create a subflow that contains two nodes, one node-red-contrib-simpletime and a function or change node which deletes all mesage properties you don't want to have. Then you also just have to insert a single "node"

But doesn't the debug tab already add the time? Or do you mean for manually writing things in a logfile?

1 Like

Yes for status on custom nodes /mqtt logging showing time of last message passing thru them

I was hoping someone would know how to delete all my* properties (having 1st saved mytimes) without having to indiviudally delete them

I'll just have to go for the slog approach :slight_smile: image

You COULD do the following: Take a function node and insert something like

Object.keys(msg).forEach(function(key){
    if(key.includes('my')) delete msg[key];
});

but don't do it :slight_smile:

1 Like

Drat missed one off
image

fixed now

1 Like

There's some JSONata foo for this: Rather than deleting all the my... except mytimes in a change node, walk through the properties on the msg object and check if they start with my, and if so check if it is mytimes otherwise don't include it.

With the $sift function you can step through the keys on an object and only return key/values that match your condition: https://docs.jsonata.org/higher-order-functions#sift
It takes a function where you describe the condition for it to match. In this situation, match msg.mytimes, and all other msg.XXX properties that don't start with my. Here's an example with quickly typed msg object. Keep in mind that in JSONata $$ the full object is that goes in as input, in the case of Node-RED that's the entire msg object. As such you can safely reference the msg object itself as $$ wherever you are in your query.
https://try.jsonata.org/2xbKJm0Zg

$sift($$, function($value, $key) {
    /* Check that key does not start with my... unless it is mytimes */
    $key = 'mytimes' or $substring($key, 0, 2) != 'my'
})
2 Likes

I assume that you are adding simpletime just before another node which makes use of mytimes, maybe to construct an array or something?
If so, could you not limit the flow to just the payload & topic downstream AFTER adding hh:mm:ss into the flow structure by creating a new msg object, such as;

var newMsg = { payload: msg.payload,topic:msg.topic };
return newMsg;

or even;

let d = msg.payload;
return {payload:d};
[{"id":"30c9ac7c.4b79f4","type":"function","z":"c9c4eeb8.bad8e","name":"","func":"let d = msg.payload;\nreturn {payload:d};","outputs":1,"noerr":0,"x":500,"y":2100,"wires":[["c3451a44.53d2f8"]]},{"id":"85cf33e5.af9ff","type":"inject","z":"c9c4eeb8.bad8e","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":2100,"wires":[["ead63f5f.23ec6"]]},{"id":"c3451a44.53d2f8","type":"debug","z":"c9c4eeb8.bad8e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":640,"y":2100,"wires":[]},{"id":"ead63f5f.23ec6","type":"change","z":"c9c4eeb8.bad8e","name":"","rules":[{"t":"set","p":"test1","pt":"msg","to":"test1","tot":"str"},{"t":"set","p":"test2","pt":"msg","to":"test2","tot":"str"},{"t":"set","p":"test3","pt":"msg","to":"test3","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":2100,"wires":[["30c9ac7c.4b79f4"]]},{"id":"ac60cffc.5782d","type":"function","z":"c9c4eeb8.bad8e","name":"","func":"var newMsg = { payload: msg.payload,topic:msg.topic };\nreturn newMsg;","outputs":1,"noerr":0,"x":500,"y":2150,"wires":[["c9517790.2a5338"]]},{"id":"a2c86d57.d06f1","type":"inject","z":"c9c4eeb8.bad8e","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":2150,"wires":[["8629c1db.dbb43"]]},{"id":"c9517790.2a5338","type":"debug","z":"c9c4eeb8.bad8e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":640,"y":2150,"wires":[]},{"id":"8629c1db.dbb43","type":"change","z":"c9c4eeb8.bad8e","name":"","rules":[{"t":"set","p":"test1","pt":"msg","to":"test1","tot":"str"},{"t":"set","p":"test2","pt":"msg","to":"test2","tot":"str"},{"t":"set","p":"test3","pt":"msg","to":"test3","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":2150,"wires":[["ac60cffc.5782d"]]}]

I'm wanting to use mytimes on several node status further along in my flows and for logging nodes that log stuff to MQTT


[edit]
I don't want to pollute the message by changing the topic [/edit]

My concept of just adding HH:MM:SS time at beginning of a flow and using it for logging purposes came crashing down today

It falls apart when you have a gate node and pause messages overnight :grimacing:

I ended up with messages from last night being logged against today's date

Plus I really want the time the message flows thru a point in the flow so the whole idea is pants :laughing:

But it was an interesting exercise and thanks for all the suggestions :slight_smile:

Going back to concept of adding boilerplate HH:MM:SS code at the point its needed :slight_smile:

1 Like

4 posts were split to a new topic: Node-red-contrib-simpletime enhancement

Conversation split & moved to Node-red-contrib-simpletime enhancement in node-development

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.