Last triggered is more the 2 days before current date

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.

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

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.

Thanks, I fixed that

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

(What am I missing?)

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

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.

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

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.

Here it is. Last_triggered is the time I would like to compare

{"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"}

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?

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

Sorry, I am beginner. How do I extract the last_triggered value from the msg.payload?

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

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

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

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

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.

1 Like

That’s it :slight_smile: thank you all for your help

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