# How to get total duration of state?

**URL:** <https://discourse.nodered.org/t/how-to-get-total-duration-of-state/77245>\
**Category:** Industrial\
**Tags:** database\
**Created:** [4 April 2023 16:35 UTC](https://discourse.nodered.org/t/how-to-get-total-duration-of-state/77245 "2023-04-04T16:35:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ferdoo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ferdoo/32/63690_2.png) [@ferdoo](https://discourse.nodered.org/u/ferdoo)\
**Post date:** [4 April 2023 16:35 UTC](https://discourse.nodered.org/t/how-to-get-total-duration-of-state/77245/1 "2023-04-04T16:35:37Z")

</div>

Hi,  
I want to calculate machine running hours based on his state stored in influx DB.  
Question is how to retrive total duration of a value from influxDB with flux query?  
The grafana can nice show the state timeline \> [grafana state timeline](https://grafana.com/docs/grafana/latest/panels-visualizations/visualizations/state-timeline/)  
I look for the sum of the time duration where the value is equal == "Run" od only equal to number == 2.  
Can you help me please.  
Thanks.

---

<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:** [4 April 2023 20:26 UTC](https://discourse.nodered.org/t/how-to-get-total-duration-of-state/77245/2 "2023-04-04T20:26:57Z")

</div>

You should be able to use the integral function. What values does the run field have?

---

<div class="post-metadata">

**Author:** ![ferdoo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ferdoo/32/63690_2.png) [@ferdoo](https://discourse.nodered.org/u/ferdoo)\
**Post date:** [5 April 2023 08:13 UTC](https://discourse.nodered.org/t/how-to-get-total-duration-of-state/77245/3 "2023-04-05T08:13:56Z")

</div>

Hi Colin.

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

Value 2 = Run  
Value 1 = Stup

This are manual pushed data for testing.

In grafana is posible to make nice graph with state history.  
But what I need is to have the sum of each state.

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

---

<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:** [5 April 2023 08:53 UTC](https://discourse.nodered.org/t/how-to-get-total-duration-of-state/77245/4 "2023-04-05T08:53:32Z")

</div>

I assumed that you were storing the state regularly, in which case I think it is fairly easy, but it looks as if you are only saving it on change. Is that correct? If so then I don't know how to do that direct via a query. You could do it in node-red of course by fetching the data and calculating the answer.

---

<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:** [4 June 2023 08:54 UTC](https://discourse.nodered.org/t/how-to-get-total-duration-of-state/77245/5 "2023-06-04T08:54:31Z")

</div>

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