# How to duration between two status changes?

**URL:** <https://discuss.elastic.co/t/how-to-duration-between-two-status-changes/366895>\
**Category:** Kibana\
**Created:** [September 20, 2024, 10:23pm UTC](https://discuss.elastic.co/t/how-to-duration-between-two-status-changes/366895 "2024-09-20T22:23:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vincenty79](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vincenty79/32/137768_2.png) [@vincenty79](https://discuss.elastic.co/u/vincenty79)\
**Post date:** [September 20, 2024, 10:23pm UTC](https://discuss.elastic.co/t/how-to-duration-between-two-status-changes/366895/1 "2024-09-20T22:23:58Z")

</div>

I'm wondering how I would get the duration how long something ran? For example the time when the hvac ran the ac (cooling) and when it stopped (idle). My goal is to keep them and eventually have weekly, monthly and seasonal history to compare with among other metrics.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/7/3/73535bc63f59f6251a77c69df4716fccaeb64ec8.png)

I just recently starting using elastic and kibana with Home Assistant. Any guidance is appreciated or links to resources or other topics with Home Assistant and Kibana. Thank you.

---

<div class="post-metadata">

**Author:** ![ashishtiwari1993](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashishtiwari1993/32/135241_2.png) [@ashishtiwari1993](https://discuss.elastic.co/u/ashishtiwari1993)\
**Post date:** [September 23, 2024, 12:37pm UTC](https://discuss.elastic.co/t/how-to-duration-between-two-status-changes/366895/2 "2024-09-23T12:37:19Z")

</div>

Hi @vincenty79,

Welcome to the Elastic community. You need to [transform](https://www.elastic.co/guide/en/elasticsearch/reference/current/transforms.html) your data, Which will eventually create a [entity centric index](https://www.elastic.co/videos/entity-centric-indexing-mark-harwood) by calculating elapsed time.

Here is the quick example align to your use case

```auto
# Create an ingest pipeline to add timestamp into documents. Feel free to skip this in your =case.
PUT _ingest/pipeline/add_date
{
  "description" : "This pipeline add the ingest time to the document",
  "processors" : [
    {
      "set" : {
        "field": "received",
        "value": "{{_ingest.timestamp}}"
      }
    }
  ]
}

# Add some documents
POST source_index/_doc?pipeline=add_date
{
  "home_id":1,
  "status": "idle"
}

POST source_index/_doc?pipeline=add_date
{
  "home_id":1,
  "status": "cooling"
}

POST source_index/_doc?pipeline=add_date
{
  "home_id":2,
  "status": "idle"
}

POST source_index/_doc?pipeline=add_date
{
  "home_id":2,
  "status": "cooling"
}

GET source_index/_search

# create a transform
PUT _transform/calc-duration
{ 
  "source": {
    "index": "source_index"
  },
  "pivot": {
    "group_by": {
      "home": {
        "terms": {
          "field": "home_id"
        }
      }
    },
    "aggregations": {
      "idle": {
        "filter": {
          "term": {
            "status": "idle"
          }
        },
        "aggs": {
          "min_time": {
            "min": {
              "field": "received"
            }
          }
        }
      },
      "cooling": {
        "filter": {
          "term": {
            "status": "cooling"
          }
        },
        "aggs": {
          "max_time": {
            "max": {
              "field": "received"
            }
          }
        }
      },
      "duration": {
        "bucket_script": {
          "buckets_path": {
            "min": "idle>min_time",
            "max": "cooling>max_time"
          },
          "script": "(params.max - params.min) / 1000"
        }
      }
    }
  },
  "dest": {
    "index": "source_index_transformed"
  },
  "sync": {
    "time": {
      "field": "received",
      "delay": "10s"
    }
  }
}

# start transform, It will sync in every 10 seconds
POST _transform/calc-duration/_start

# Get your summarize result
GET source_index_transformed/_search

```

---

<div class="post-metadata">

**Author:** ![vincenty79](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vincenty79/32/137768_2.png) [@vincenty79](https://discuss.elastic.co/u/vincenty79)\
**Post date:** [September 23, 2024, 4:17pm UTC](https://discuss.elastic.co/t/how-to-duration-between-two-status-changes/366895/3 "2024-09-23T16:17:01Z")

</div>

Wow thank you so much. It seems overwhelming but this is a perfect opportunity for me to learn more. I appreciate you taking the time to write up an answer and including links to what transform and centric index is.
