# Cycle time calculation

**URL:** <https://discourse.nodered.org/t/cycle-time-calculation/69227>\
**Category:** General\
**Created:** [19 October 2022 04:00 UTC](https://discourse.nodered.org/t/cycle-time-calculation/69227 "2022-10-19T04:00:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vik](https://avatars.discourse-cdn.com/v4/letter/v/c67d28/32.png) [@Vik](https://discourse.nodered.org/u/Vik)\
**Post date:** [19 October 2022 04:00 UTC](https://discourse.nodered.org/t/cycle-time-calculation/69227/1 "2022-10-19T04:00:59Z")

</div>

@Frida @Steve-Mcl

Continuing the discussion from [Flow to get output from a logical sequence of events](https://discourse.nodered.org/t/flow-to-get-output-from-a-logical-sequence-of-events/68447/5):

I am working on this flow to get the duration of each state. I need to get:  
A) the duration when D2 is 1,  
B) the duration when D1 is 1 (during the state when D2 was 1).

This will give me the cycle time of D1 and D2. When D2 is 0 and again the cycle starts, i should get the new cycle time. Each cycle should have an id no. could be an index no.

I am able to get D2 cycle time, but not D1.

Thanks

---

<div class="post-metadata">

**Author:** ![Vik](https://avatars.discourse-cdn.com/v4/letter/v/c67d28/32.png) [@Vik](https://discourse.nodered.org/u/Vik)\
**Post date:** [28 October 2022 05:47 UTC](https://discourse.nodered.org/t/cycle-time-calculation/69227/2 "2022-10-28T05:47:12Z")

</div>

Hi,  
Wrote the following flow to get the cycle time:

```auto
[{"id":"2bd354d0022b63b2","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"895d26a54c81d0f9","type":"inject","z":"2bd354d0022b63b2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"D1","payload":"true","payloadType":"bool","x":50,"y":140,"wires":[["d5371e5971150080","b961e3d970b8185e"]]},{"id":"a5cde9c22b0beac7","type":"inject","z":"2bd354d0022b63b2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"D1","payload":"false","payloadType":"bool","x":60,"y":180,"wires":[["d5371e5971150080","b961e3d970b8185e"]]},{"id":"9082e55b518bec00","type":"inject","z":"2bd354d0022b63b2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"D2","payload":"true","payloadType":"bool","x":50,"y":240,"wires":[["d5371e5971150080"]]},{"id":"bdb8d7a7782e5dab","type":"inject","z":"2bd354d0022b63b2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"D2","payload":"false","payloadType":"bool","x":60,"y":280,"wires":[["d5371e5971150080"]]},{"id":"d5371e5971150080","type":"join","z":"2bd354d0022b63b2","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":250,"y":100,"wires":[["736f7796fc3bdc02"]]},{"id":"736f7796fc3bdc02","type":"change","z":"2bd354d0022b63b2","name":"both same?","rules":[{"t":"set","p":"payload1","pt":"msg","to":"payload.D1 = payload.D2 ? payload.D1 : null","tot":"jsonata"},{"t":"set","p":"topic","pt":"msg","to":"result","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":100,"wires":[["565358b383647fec"]]},{"id":"565358b383647fec","type":"switch","z":"2bd354d0022b63b2","name":"both on \\n both off \\n different","property":"payload1","propertyType":"msg","rules":[{"t":"true"},{"t":"false"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":580,"y":100,"wires":[["9394164428a24d05"],["9394164428a24d05"],[]]},{"id":"9394164428a24d05","type":"rbe","z":"2bd354d0022b63b2","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload1","topi":"topic","x":730,"y":100,"wires":[["b961e3d970b8185e"]]},{"id":"b961e3d970b8185e","type":"function","z":"2bd354d0022b63b2","name":"Cycle_Start_Check","func":"if (msg.payload1 == true) {\n flow.set(\"counter\",0);\n}\nflow.set(\"status\",msg.payload);","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":220,"wires":[["54f474031f5afa99"]]},{"id":"563e09e958d0b868","type":"inject","z":"2bd354d0022b63b2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"0.5","topic":"","payload":"1","payloadType":"num","x":530,"y":300,"wires":[["54f474031f5afa99"]]},{"id":"54f474031f5afa99","type":"function","z":"2bd354d0022b63b2","name":"Cycle_Time","func":"var status = flow.get(\"status\") || false;\nvar count = flow.get(\"counter\") || 0;\n\nif (status == true){\n count = count + 10;\n}\n\nflow.set(\"counter\",count);\n\nmsg.payload = count;\nmsg.payload2 = flow.get(\"start_value\") - 1;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":690,"y":220,"wires":[[]]}]

```

this problem is solved.

---

<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:** [11 November 2022 05:47 UTC](https://discourse.nodered.org/t/cycle-time-calculation/69227/3 "2022-11-11T05:47:37Z")

</div>

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