title: Count minutes/hours when a specific value is higher than a treshold
body: <p>i have this JSON that is contained in &quot;data&quot; column, the table also have: insert time, pk and some fk as reported below:</p>
  <pre><code>PK  INSERT_TIME                     DATA   SENSOR
  1   2020-01-04 15:39:44.19364+00    {}       7
  </code></pre>
  <p>The table is populated with an external application that, about every minute, pushes new data, this is the JSON that i have typically in the table:</p>
  <pre><code>{
     &quot;dev&quot;:11,
     &quot;kw&quot;:43.0,
     &quot;lux&quot;:200.2,
     &quot;temp&quot;:20.3,
  }
  </code></pre>
  <p>My goal is to build a query that, having all rows in the table is able to count how many hours in total and hours per day the device was up and running and producing kW, so, when the kW value is greater than zero (those measurements comes from a solar inverter system)</p>
  <p>This is the query that i've built, actually i'm able to select only the rows that have the kw value greater than 1 and group them by month, but i'm not able to figure out how to calculate the production hours:</p>
  <pre><code>SELECT mt.insert_time, 
         date_part('month', mt.insert_time) as mon
  FROM mytable mt 
  WHERE sensor = 7 
    AND date_part('month', mt.insert_time) = 5 
    AND (mt.data-&gt;'kw')::int &gt; 1
  </code></pre>
  <p>What i'm expecting:</p>
  <p>I need to get the exact amount of working hour of my solar panel production system, i'm assuming that the solar panel &quot;works&quot; only when the kW value is greater than zero.</p>
  <p>I want to see, day by day, and month by month the working hours of my panel.</p>
  <p>Here guys i'm reporting an example of my data rows</p>
  <pre><code>+-----------------------------------------------------------------------------------------------------+
  |                                     pk,insert_time,data,sensor                                      |
  +-----------------------------------------------------------------------------------------------------+
  | 3003711,2020-10-03 09:55:54.271738+00,&quot;{&quot;&quot;dev&quot;&quot;: 12, &quot;&quot;kw&quot;&quot;: 0, &quot;&quot;temp&quot;&quot;: 20.8, &quot;&quot;lux&quot;&quot;: 2.0}&quot;,12   |
  | 3003692,2020-10-03 09:54:54.289131+00,&quot;{&quot;&quot;dev&quot;&quot;: 12, &quot;&quot;kw&quot;&quot;: 0, &quot;&quot;temp&quot;&quot;: 20.6, &quot;&quot;lux&quot;&quot;: 2.0}&quot;,12   |
  | 3003681,2020-10-03 09:53:54.287502+00,&quot;{&quot;&quot;dev&quot;&quot;: 12, &quot;&quot;kw&quot;&quot;: 9.0, &quot;&quot;temp&quot;&quot;: 20.5, &quot;&quot;lux&quot;&quot;: 2.0}&quot;,12 |
  | 3003670,2020-10-03 09:52:54.284262+00,&quot;{&quot;&quot;dev&quot;&quot;: 12, &quot;&quot;kw&quot;&quot;: 9.0, &quot;&quot;temp&quot;&quot;: 20.5, &quot;&quot;lux&quot;&quot;: 2.0}&quot;,12 |
  | 3003659,2020-10-03 09:51:56.382746+00,&quot;{&quot;&quot;dev&quot;&quot;: 12, &quot;&quot;kw&quot;&quot;: 12, &quot;&quot;temp&quot;&quot;: 20.5, &quot;&quot;lux&quot;&quot;: 2.0}&quot;,12  |
  | 3003648,2020-10-03 09:50:54.279558+00,&quot;{&quot;&quot;dev&quot;&quot;: 12, &quot;&quot;kw&quot;&quot;: 9.0, &quot;&quot;temp&quot;&quot;: 20.5, &quot;&quot;lux&quot;&quot;: 2.0}&quot;,12 |
  | 3003637,2020-10-03 09:49:56.377988+00,&quot;{&quot;&quot;dev&quot;&quot;: 12, &quot;&quot;kw&quot;&quot;: 9.0, &quot;&quot;temp&quot;&quot;: 20.5, &quot;&quot;lux&quot;&quot;: 2.0}&quot;,12 |
  +-----------------------------------------------------------------------------------------------------+
  </code></pre>
  <p>The final result may produce a query that (starting from a specific day of month, or specific month, or specific year) will give me the number of minutes and hours that the system was powered up and producing kW.</p>
  <p>Like, for example:</p>
  <ul>
  <li>Selecting the date of 10/08/2020 i need to get the total production minutes and respective hours count</li>
  <li>Selecting the month of july (7) i need to get the total production minutes and respective hours count</li>
  <li>Selecting the year (2020) i need to get the total production minutes and respective hours count</li>
  </ul>