title: Query InfluxDB with CASE or IF statement?
body: <p>I am gathering metrics from my philips hue bridge and putting them into an InfluxDB. I'd like to be able to display some of these metrics on a dashboard.</p>
  
  <p>A basic query looks like this:</p>
  
  <pre><code>SELECT mean("state.bri") AS "mean_state.bri" FROM "telegraf_hue"."autogen"."hue" WHERE time &gt; :dashboardTime: AND "lamp"='Couch' GROUP BY time(:interval:) FILL(null)
  </code></pre>
  
  <p>This returns a nice graph that shows the brightness level of the light. </p>
  
  <p><a href="https://i.stack.imgur.com/YA5z5.png" rel="nofollow noreferrer">Basic Query Results</a></p>
  
  <p>The problem with this query is that the light isn't actually on all of that time. The Hue Bridge returns the last brightness level, even if it's off. </p>
  
  <p>I can see if it is on by checking <code>"state.on"</code> for a <code>true</code> or <code>false</code> value. </p>
  
  <pre><code>SELECT mean("state.bri") AS "mean_state.bri" FROM "telegraf_hue"."autogen"."hue" WHERE "state.on" = true AND time &gt; :dashboardTime: AND "lamp"='Couch' GROUP BY time(:interval:) FILL(null)
  </code></pre>
  
  <p>This only returns points where the light is on. </p>
  
  <p><a href="https://i.stack.imgur.com/9UTtw.png" rel="nofollow noreferrer">Query where light is on results</a></p>
  
  <p>What I'd like to do something like a <code>CASE</code> statement like in MySQL:</p>
  
  <pre><code>SELECT CASE WHEN "state.on" = true THEN mean("state.bri") ELSE 0 END FROM "telegraf_hue"."autogen"."hue" WHERE time &gt; :dashboardTime: AND "lamp"='Couch' GROUP BY time(:interval:) FILL(null)
  </code></pre>
  
  <p>This doesn't work. It fails with:</p>
  
  <pre><code>Request failed with status code 400
  </code></pre>
  
  <p>How can I accomplish something similar with InfluxDB? Ultimately, I'll be building the dashboards in Grafana, if that matters.</p>