# Easy way of adding only mytimes to a msg

**URL:** <https://discourse.nodered.org/t/easy-way-of-adding-only-mytimes-to-a-msg/28683>\
**Category:** General\
**Created:** [21 June 2020 10:52 UTC](https://discourse.nodered.org/t/easy-way-of-adding-only-mytimes-to-a-msg/28683 "2020-06-21T10:52:14Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [21 June 2020 10:52 UTC](https://discourse.nodered.org/t/easy-way-of-adding-only-mytimes-to-a-msg/28683/1 "2020-06-21T10:52:14Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/e/2e7238baa61e546b8088d74913e41fe7f7eb135f.jpeg)

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?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 June 2020 11:06 UTC](https://discourse.nodered.org/t/easy-way-of-adding-only-mytimes-to-a-msg/28683/2 "2020-06-21T11:06:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [21 June 2020 11:08 UTC](https://discourse.nodered.org/t/easy-way-of-adding-only-mytimes-to-a-msg/28683/3 "2020-06-21T11:08:40Z")

</div>

> [@Colin](#):
>
> 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 🙂

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

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [21 June 2020 11:12 UTC](https://discourse.nodered.org/t/easy-way-of-adding-only-mytimes-to-a-msg/28683/4 "2020-06-21T11:12:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cinhcet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cinhcet/32/438_2.png) [@cinhcet](https://discourse.nodered.org/u/cinhcet)\
**Post date:** [21 June 2020 11:39 UTC](https://discourse.nodered.org/t/easy-way-of-adding-only-mytimes-to-a-msg/28683/5 "2020-06-21T11:39:32Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [21 June 2020 12:14 UTC](https://discourse.nodered.org/t/easy-way-of-adding-only-mytimes-to-a-msg/28683/6 "2020-06-21T12:14:44Z")

</div>

> [@cinhcet](#):
>
> Or do you mean for manually writing things in a logfile?

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

> [@cinhcet](#):
>
> 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

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 🙂 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/c/ecd475376ad9ecdfcc0ccc5106e9ed3f12795482.png)

---

<div class="post-metadata">

**Author:** ![cinhcet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cinhcet/32/438_2.png) [@cinhcet](https://discourse.nodered.org/u/cinhcet)\
**Post date:** [21 June 2020 12:19 UTC](https://discourse.nodered.org/t/easy-way-of-adding-only-mytimes-to-a-msg/28683/7 "2020-06-21T12:19:32Z")

</div>

> [@cymplecy](#):
>
> I was hoping someone would know how to delete all my\* properties (having 1st saved mytimes) without having to indiviudally delete them

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

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

```

but don't do it 🙂

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [21 June 2020 12:21 UTC](https://discourse.nodered.org/t/easy-way-of-adding-only-mytimes-to-a-msg/28683/8 "2020-06-21T12:21:30Z")

</div>

Drat missed one off  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/1/91d537699dfb239b8a6d03bbb1c1f8f788bfb9c0.png)

fixed now

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/35f90a0c6ecd3b0d3174e3038bebbaab0e70e9da.jpeg)

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [21 June 2020 13:37 UTC](https://discourse.nodered.org/t/easy-way-of-adding-only-mytimes-to-a-msg/28683/9 "2020-06-21T13:37:41Z")

</div>

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](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](https://try.jsonata.org/2xbKJm0Zg)

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

```

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [21 June 2020 14:23 UTC](https://discourse.nodered.org/t/easy-way-of-adding-only-mytimes-to-a-msg/28683/10 "2020-06-21T14:23:40Z")

</div>

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;

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

```

or even;

```auto
let d = msg.payload;
return {payload:d};

```

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [21 June 2020 14:56 UTC](https://discourse.nodered.org/t/easy-way-of-adding-only-mytimes-to-a-msg/28683/11 "2020-06-21T14:56:05Z")

</div>

> [@Paul-Reed](#):
>
> I assume that you are adding simpletime just before another node which makes use of `mytimes`

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/d/dda4720b3eade3b694f1225c61328de422a21aa4.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/7/2770cebb3238bdec18b02aa9d23e3e5061801fe5.png)  
[edit]  
I don't want to pollute the message by changing the topic [/edit]

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [23 June 2020 12:01 UTC](https://discourse.nodered.org/t/easy-way-of-adding-only-mytimes-to-a-msg/28683/12 "2020-06-23T12:01:14Z")

</div>

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 😬

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/7/f7b5732d35312f9e560ba1a30726477f3dc85e12.png)

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

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

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

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [27 June 2020 15:07 UTC](https://discourse.nodered.org/t/easy-way-of-adding-only-mytimes-to-a-msg/28683/13 "2020-06-27T15:07:45Z")

</div>

Split this topic on 27 June 2020 15:07 UTC

4 posts were split to a new topic: [Node-red-contrib-simpletime enhancement](https://discourse.nodered.org/t/node-red-contrib-simpletime-enhancement/29069)

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [27 June 2020 15:09 UTC](https://discourse.nodered.org/t/easy-way-of-adding-only-mytimes-to-a-msg/28683/14 "2020-06-27T15:09:23Z")

</div>

Conversation split & moved to [Node-red-contrib-simpletime enhancement](https://discourse.nodered.org/t/node-red-contrib-simpletime-enhancement/29069) in `node-development`

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [26 August 2020 15:09 UTC](https://discourse.nodered.org/t/easy-way-of-adding-only-mytimes-to-a-msg/28683/15 "2020-08-26T15:09:27Z")

</div>

Closed on 26 August 2020 15:09 UTC

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