# Calculating Time / Washing Machine Notification

**URL:** https://discourse.nodered.org/t/calculating-time-washing-machine-notification/53166
**Category:** General
**Created:** [3 November 2021 10:18 UTC](https://discourse.nodered.org/t/calculating-time-washing-machine-notification/53166 "2021-11-03T10:18:17Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![terminus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/terminus/32/38378_2.png) [@terminus](https://discourse.nodered.org/u/terminus)
#### Post date: [3 November 2021 10:18 UTC](https://discourse.nodered.org/t/calculating-time-washing-machine-notification/53166/1 "2021-11-03T10:18:18Z")

</div>

Hey Guys!  
Im trying to get a notification 10 minutes before my washing machine is finishing. There was a remaining time sensor in the past (siemens home connect) but it isn't there anymore. The only sensor left, is a sensor with shows the actual end time of the program. So I put in a current state node and the payload is: `2021-11-03T14:07:53.797900+00:00`. I thought about calculating the time difference between the time now and the programm end time and substract 10 minutes. What do you think? Is there a more convenient way to archive this or how would you do that?

Regards

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [3 November 2021 10:52 UTC](https://discourse.nodered.org/t/calculating-time-washing-machine-notification/53166/2 "2021-11-03T10:52:32Z")

</div>

You can use the Trigger node and pass in a delay in milliseconds. So you need to do the calculation to convert `now - washingEnd` into ms. You can do that in a function node or in a change node using JSONata. Send the resulting delay ms to the Trigger node and it will send a msg when the delay time expires.

If you are comfortable with JavaScript, you could also do it all in a function node using a `setTimeout` function.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [3 November 2021 11:01 UTC](https://discourse.nodered.org/t/calculating-time-washing-machine-notification/53166/3 "2021-11-03T11:01:34Z")

</div>

Or  
A delay node set to use msg.delay as input then use a simle JSONata expression to calculate the delay  
e.g.

```auto
[{"id":"3a357922.18ca1e","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2021-11-03T11:10:00.000000+00:00","payloadType":"str","x":160,"y":4840,"wires":[["586bd900.831e58"]]},{"id":"586bd900.831e58","type":"change","z":"b779de97.b1b46","name":"","rules":[{"t":"set","p":"delay","pt":"msg","to":"$toMillis($$.payload) - $millis() - 600000","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":4860,"wires":[["ff4bd691.5eb148"]]},{"id":"ff4bd691.5eb148","type":"delay","z":"b779de97.b1b46","name":"","pauseType":"delayv","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":580,"y":4840,"wires":[["1e2cb213.f6f5be"]]},{"id":"1e2cb213.f6f5be","type":"debug","z":"b779de97.b1b46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":560,"y":4940,"wires":[]}]

```

```auto
$toMillis($$.payload) - $millis() - 600000

```

set timestamp in inject to test

---

<div class="post-metadata">

### Author: ![terminus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/terminus/32/38378_2.png) [@terminus](https://discourse.nodered.org/u/terminus)
#### Post date: [3 November 2021 11:38 UTC](https://discourse.nodered.org/t/calculating-time-washing-machine-notification/53166/4 "2021-11-03T11:38:56Z")

</div>

I'm not familiar with coding at all. Just took snippets from here and adjusted them to my needs :-).

First question is how can i calculate the time: "payload time" - "now". In minutes or miliseconds? what is easier to do? So I imagine if the washer is finishing at 16.00 and now it is 14.00 then the result should be 120 minutes difference. And then the flow goes and somehow sends a notification if the value is 10?

Thank you

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [3 November 2021 11:43 UTC](https://discourse.nodered.org/t/calculating-time-washing-machine-notification/53166/5 "2021-11-03T11:43:48Z")

</div>

My example calculates how many milliseconds until 10 minutes before stop time. that figure is then used in delay/trigger node to delay message for that set amount of milliseconds.

---

<div class="post-metadata">

### Author: ![terminus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/terminus/32/38378_2.png) [@terminus](https://discourse.nodered.org/u/terminus)
#### Post date: [3 November 2021 11:51 UTC](https://discourse.nodered.org/t/calculating-time-washing-machine-notification/53166/6 "2021-11-03T11:51:05Z")

</div>

Hmm. Okay sorry I didn't fully understand what's going on there. Tried it like this:

 ![Bildschirmfoto 2021-11-03 um 12.49.35](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/7/770487283b56897df44148eb50c965947c9b7703.png)

Okay! the delay value is the value in ms until 10minutes before end? And this value sets the delay node to exact 10min before machine end?

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [3 November 2021 11:54 UTC](https://discourse.nodered.org/t/calculating-time-washing-machine-notification/53166/7 "2021-11-03T11:54:33Z")

</div>

You need to post the exported flow along with a inject node that contains the incoming msg from the HA node. That image is like going to a mechanic and saying my engine is not working and not opening the bonnet(hood). We need to see the expression used and other settings to

---

<div class="post-metadata">

### Author: ![terminus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/terminus/32/38378_2.png) [@terminus](https://discourse.nodered.org/u/terminus)
#### Post date: [3 November 2021 12:06 UTC](https://discourse.nodered.org/t/calculating-time-washing-machine-notification/53166/8 "2021-11-03T12:06:38Z")

</div>

Thank you. Tried to kind of reverse engineered your flow and I think it works :-).

Will try to tinker around a little bit!

---

<div class="post-metadata">

### Author: ![terminus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/terminus/32/38378_2.png) [@terminus](https://discourse.nodered.org/u/terminus)
#### Post date: [3 November 2021 12:10 UTC](https://discourse.nodered.org/t/calculating-time-washing-machine-notification/53166/9 "2021-11-03T12:10:33Z")

</div>

Is `$millis()` the expression for the current time?

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [3 November 2021 12:16 UTC](https://discourse.nodered.org/t/calculating-time-washing-machine-notification/53166/10 "2021-11-03T12:16:29Z")

</div>

Yes in milliseconds

> **[Date/Time functions · JSONata](https://docs.jsonata.org/date-time-functions#millis)**
>
> \## \`$now()\`

---

<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: [17 November 2021 12:16 UTC](https://discourse.nodered.org/t/calculating-time-washing-machine-notification/53166/11 "2021-11-17T12:16:59Z")

</div>

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