title: Lua - Calculate watts being used by a light bulb, based on its brightness level?
body: <p>I was sent the following code as an example of what I could do to calculate the watts being used by a hue light bulb, based on its model number and brightness level..</p>
  <pre><code>if hue_model == &quot;LWB010&quot; and hue_state == true then
     if hue_level &lt;= 0 then watt = 0.4 end
     if (hue_level &gt; 0 and hue_level &lt;= 6) then watt = 1.6 end
     if (hue_level &gt; 6 and hue_level &lt;= 13) then watt = 1.7 end
     if (hue_level &gt; 13 and hue_level &lt;= 20) then watt = 1.8 end
     if (hue_level &gt; 20 and hue_level &lt;= 26) then watt = 1.9 end
     if (hue_level &gt; 26 and hue_level &lt;= 33) then watt = 2.2 end
     if (hue_level &gt; 33 and hue_level &lt;= 40) then watt = 2.4 end
     if (hue_level &gt; 40 and hue_level &lt;= 46) then watt = 2.7 end
     if (hue_level &gt; 46 and hue_level &lt;= 53) then watt = 3.0 end
     if (hue_level &gt; 53 and hue_level &lt;= 60) then watt = 3.5 end
     if (hue_level &gt; 60 and hue_level &lt;= 66) then watt = 3.9 end
     if (hue_level &gt; 66 and hue_level &lt;= 73) then watt = 4.5 end
     if (hue_level &gt; 73 and hue_level &lt;= 80) then watt = 5.1 end
     if (hue_level &gt; 73 and hue_level &lt;= 86) then watt = 5.5 end
     if (hue_level &gt; 86 and hue_level &lt;= 93) then watt = 6.4 end
     if (hue_level &gt; 93) then watt = 6.7 end
     print(watt)
      else
      print(&quot;light is off&quot;)
      end
  </code></pre>
  <p>As the brightness range returned by the Hue API seems to have changed from 0-100 to 0-255, the above needs to be updated. And as part of that work, I’d like to make it more efficient (less code) and also more precise.</p>
  <p>Logic suggests that when the light’s on full (255) it will be using 6.7w, and if it’s at 50% (127), that’ll drop to approx (3.35w)</p>
  <p>Sadly after trying various thing, I keep failing badly, and find myself reverting back to something very long like the original look up type table above. Surely there’s a better/cleaner way ?</p>
  <p>If so, please could someone share what they would do.</p>
  <p>Many thanks..</p>