# Last triggered is more the 2 days before current date

**URL:** <https://discourse.nodered.org/t/last-triggered-is-more-the-2-days-before-current-date/28374>\
**Category:** General\
**Created:** [15 June 2020 21:27 UTC](https://discourse.nodered.org/t/last-triggered-is-more-the-2-days-before-current-date/28374 "2020-06-15T21:27:09Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![fxxer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fxxer/32/24018_2.png) [@fxxer](https://discourse.nodered.org/u/fxxer)\
**Post date:** [15 June 2020 21:27 UTC](https://discourse.nodered.org/t/last-triggered-is-more-the-2-days-before-current-date/28374/1 "2020-06-15T21:27:09Z")

</div>

Hi, I am trying to calculate time difference between last\_triggered time of a HA script with current date. I am stuck how to do it. Can anyone help me?

I want to flow to continue only if the last triggered time to current time is more then 2 days.

This is the script value I choose for testing this flow.

```auto
{"attributes":{"can_cancel":true,"friendly_name":"cover (living room ALL) - down","last_triggered":"2020-06-14T18:07:51.819657+00:00"},"context":{"id":"835360fa78f44028a467841b420631e1","parent_id":null,"user_id":null},"entity_id":"script.cover_living_room_covers_down","last_changed":"2020-06-14T21:23:53.427000+00:00","last_updated":"2020-06-14T21:23:53.427000+00:00","state":"off"}

```

```auto
[{"id":"3b8a4ccb.a80d44","type":"inject","z":"a384315.94b895","name":"Go","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":2140,"wires":[["25c953fa.344e94"]]},{"id":"25c953fa.344e94","type":"api-get-history","z":"a384315.94b895","name":"","server":"1cf948dc.a805f7","startdate":"","enddate":"","entityid":"script.cover_living_room_covers_down","entityidtype":"is","useRelativeTime":false,"relativeTime":"1d","flatten":true,"output_type":"split","output_location_type":"msg","output_location":"payload","x":370,"y":2140,"wires":[["8d9bcb00.f6e75"]]},{"id":"8d9bcb00.f6e75","type":"change","z":"a384315.94b895","name":"","rules":[{"t":"move","p":"payload.attributes.last_triggered","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":610,"y":2020,"wires":[["88a45274.831198"]]},{"id":"88a45274.831198","type":"function","z":"a384315.94b895","name":"Calc difference ms","func":"flow.set(\"startTime\");\nreturn msg;","outputs":1,"noerr":0,"x":910,"y":2020,"wires":[["d109c417.d561e","da4e1215.f45948"]]},{"id":"d109c417.d561e","type":"debug","z":"a384315.94b895","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1110,"y":2080,"wires":[]},{"id":"da4e1215.f45948","type":"function","z":"a384315.94b895","name":"Calc difference ms","func":"var now = new Date();\nmsg.payload = now - flow.get(\"startTime\");\nreturn msg;","outputs":1,"noerr":0,"x":910,"y":2120,"wires":[["2847c817.8dfab8"]]},{"id":"2847c817.8dfab8","type":"debug","z":"a384315.94b895","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1110,"y":2120,"wires":[]},{"id":"1cf948dc.a805f7","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [15 June 2020 22:06 UTC](https://discourse.nodered.org/t/last-triggered-is-more-the-2-days-before-current-date/28374/2 "2020-06-15T22:06:15Z")

</div>

You didn't post the code correctly for the second part.

Though the first part was.

When ready to post the code, look at the top of the window and look for the `</>` button.  
Click it and paste the code.

---

<div class="post-metadata">

**Author:** ![fxxer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fxxer/32/24018_2.png) [@fxxer](https://discourse.nodered.org/u/fxxer)\
**Post date:** [16 June 2020 04:27 UTC](https://discourse.nodered.org/t/last-triggered-is-more-the-2-days-before-current-date/28374/3 "2020-06-16T04:27:31Z")

</div>

Thanks, I fixed that

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [16 June 2020 04:29 UTC](https://discourse.nodered.org/t/last-triggered-is-more-the-2-days-before-current-date/28374/4 "2020-06-16T04:29:53Z")

</div>

Um, all I get when I import the other bit is one `inject` node.

(What am I missing?)

---

<div class="post-metadata">

**Author:** ![fxxer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fxxer/32/24018_2.png) [@fxxer](https://discourse.nodered.org/u/fxxer)\
**Post date:** [16 June 2020 07:00 UTC](https://discourse.nodered.org/t/last-triggered-is-more-the-2-days-before-current-date/28374/5 "2020-06-16T07:00:02Z")

</div>

I copied only first node, it's edited in my first post. Thanks

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [16 June 2020 08:34 UTC](https://discourse.nodered.org/t/last-triggered-is-more-the-2-days-before-current-date/28374/6 "2020-06-16T08:34:37Z")

</div>

You will need to paste a bit more than an `inject` node.

How about the node, a node which simulates what _HA_ sends, and the node which you are using to compare the two?

And maybe a screen shot of the nodes - cropped to only them for the sake of keeping people focused on the part on which you are working.

---

<div class="post-metadata">

**Author:** ![fxxer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fxxer/32/24018_2.png) [@fxxer](https://discourse.nodered.org/u/fxxer)\
**Post date:** [16 June 2020 16:16 UTC](https://discourse.nodered.org/t/last-triggered-is-more-the-2-days-before-current-date/28374/7 "2020-06-16T16:16:15Z")

</div>

The whole flow is there in original post. The flow is here

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/b/abf67c528dd3034937852eb5a386806183157297.jpeg)

---

<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:** [16 June 2020 16:41 UTC](https://discourse.nodered.org/t/last-triggered-is-more-the-2-days-before-current-date/28374/8 "2020-06-16T16:41:23Z")

</div>

Add a debug node showing what is coming out of the history node and post it here so we can see the format of the time.

---

<div class="post-metadata">

**Author:** ![fxxer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fxxer/32/24018_2.png) [@fxxer](https://discourse.nodered.org/u/fxxer)\
**Post date:** [16 June 2020 16:54 UTC](https://discourse.nodered.org/t/last-triggered-is-more-the-2-days-before-current-date/28374/9 "2020-06-16T16:54:31Z")

</div>

Here it is. Last\_triggered is the time I would like to compare

```auto
{"attributes":{"can_cancel":true,"friendly_name":"cover (living room ALL) - down","last_triggered":"2020-06-14T18:07:51.819657+00:00"},"context":{"id":"835360fa78f44028a467841b420631e1","parent_id":null,"user_id":null},"entity_id":"script.cover_living_room_covers_down","last_changed":"2020-06-15T16:52:57.344000+00:00","last_updated":"2020-06-15T16:52:57.344000+00:00","state":"off"}

```

---

<div class="post-metadata">

**Author:** ![franksmicro](https://avatars.discourse-cdn.com/v4/letter/f/e8c25b/32.png) [@franksmicro](https://discourse.nodered.org/u/franksmicro)\
**Post date:** [16 June 2020 17:53 UTC](https://discourse.nodered.org/t/last-triggered-is-more-the-2-days-before-current-date/28374/10 "2020-06-16T17:53:43Z")

</div>

Node allows arithmetic on dates. Results are in milliseconds. You could divide by the number of milliseconds in a day to get the number of days. Could you do something like this in your function node?

```auto
var curdate = new Date();
var daysdiff = (curdate - last_triggered) / (1000 * 60 * 60 * 24);
if(daysdiff > 2) {
your logic here}

```

---

<div class="post-metadata">

**Author:** ![fxxer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fxxer/32/24018_2.png) [@fxxer](https://discourse.nodered.org/u/fxxer)\
**Post date:** [16 June 2020 19:33 UTC](https://discourse.nodered.org/t/last-triggered-is-more-the-2-days-before-current-date/28374/11 "2020-06-16T19:33:34Z")

</div>

Sorry, I am beginner. How do I extract the last\_triggered value from the msg.payload?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [16 June 2020 20:10 UTC](https://discourse.nodered.org/t/last-triggered-is-more-the-2-days-before-current-date/28374/12 "2020-06-16T20:10:04Z")

</div>

try msg.payload.attributes.last\_triggered

Note, if you hover over the attribute name you will see some icons appear on the right.  
 ![Screen Shot 2020-06-16 at 4.10.44 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/9/599506403a98fa157cadd032a600c8b6a6551525.png)

Hover over the one that looks like theis `>_` and you will be able to copy the path to that element.

---

<div class="post-metadata">

**Author:** ![fxxer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fxxer/32/24018_2.png) [@fxxer](https://discourse.nodered.org/u/fxxer)\
**Post date:** [16 June 2020 20:35 UTC](https://discourse.nodered.org/t/last-triggered-is-more-the-2-days-before-current-date/28374/13 "2020-06-16T20:35:27Z")

</div>

I figured this out, thanks. I want the difference as a message to other node. Is this correct? Because I get NaN response.

```auto
var now = new Date();
msg.payload = now - msg.payload.attributes.last_triggered;
return msg;

```

---

<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:** [16 June 2020 20:55 UTC](https://discourse.nodered.org/t/last-triggered-is-more-the-2-days-before-current-date/28374/14 "2020-06-16T20:55:48Z")

</div>

If last\_triggered is a string you need to make a Date from it. Probably you can do  
`var last = new Date( msg.payload.attributes.last_triggered)`  
and subtract that from `now`. I say probably because it does depend on the string format, but I think the format you showed should be ok.

---

<div class="post-metadata">

**Author:** ![fxxer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fxxer/32/24018_2.png) [@fxxer](https://discourse.nodered.org/u/fxxer)\
**Post date:** [17 June 2020 04:30 UTC](https://discourse.nodered.org/t/last-triggered-is-more-the-2-days-before-current-date/28374/15 "2020-06-17T04:30:26Z")

</div>

That’s it 🙂 thank you all for your help

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [1 July 2020 04:30 UTC](https://discourse.nodered.org/t/last-triggered-is-more-the-2-days-before-current-date/28374/16 "2020-07-01T04:30:26Z")

</div>

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