title: influxdb: calculating duration of boolean events?
body: <p>I have data in an influxdb database from a door sensor.  This is a boolean sensor (either the door is open (value is <code>false</code>) or it is closed (value is <code>true</code>)), and the table looks like:</p>
  
  <pre><code>name: door
  --------------
  time            value
  1506026143659488953 true
  1506026183699139512 false
  1506026751433484237 true
  1506026761473122666 false
  1506043848850764808 true
  1506043887602743375 false
  </code></pre>
  
  <p>I would like to calculate how long the door was open in a given period of time.  The <a href="https://docs.influxdata.com/influxdb/v1.3/query_language/functions/#elapsed" rel="noreferrer">ELAPSED</a> function gets me close, but I'm not sure how to either (a) restrict it to only those intervals for which the intitial value is <code>false</code>, or (b) identify "open" intervals from the output of something like <code>select elapsed(value, 1s) from door</code>.</p>
  
  <p>I was hoping I could do something like:</p>
  
  <pre><code>select elapsed(value, 1s), first(value) from door
  </code></pre>
  
  <p>But that doesn't get me anything useful:</p>
  
  <pre><code>name: door
  --------------
  time            elapsed first
  0               true
  1506026183699139512 40  
  1506026751433484237 567 
  1506026761473122666 10  
  1506043848850764808 17087   
  1506043887602743375 38  
  </code></pre>
  
  <p>I was hoping for something more along the lines of:</p>
  
  <pre><code>name: door
  --------------
  time            elapsed first
  1506026183699139512 40  true
  1506026751433484237 567 false
  1506026761473122666 10  true
  1506043848850764808 17087   false
  1506043887602743375 38  true
  </code></pre>
  
  <p>Short of extracting the data myself and processing it in e.g. python, is there any way to do this via an influxdb query?</p>