sensor:
  - platform: sql
    db_url: !secret db_url
    scan_interval: 600
    queries:
      - name: Coste Ayer TDH
        query: "SELECT ayer, MAX(avr_day_0_12) as avr_1, MAX(avr_day_12_22) as avr_2, MAX(avr_day_22_23) as avr_3, (MAX(avr_day_0_12)+MAX(avr_day_12_22)+MAX(avr_day_22_23)) as total, MAX(avr_day_full) as TotalFull
FROM 
((SELECT DATE(created) as ayer, (AVG(CAST ( state AS numeric ))*12*0.082/1000) AS avr_day_0_12,0 as avr_day_12_22,0 as avr_day_22_23, 0 as avr_day_full
                FROM states
                WHERE entity_id = 'sensor.consumo_general'
                AND state != 'unknown'
                AND state != ''
                AND CAST(state AS numeric) >= 0
                AND CAST(state AS numeric) <= 7500
                AND DATE(created) = current_date -1
                AND EXTRACT(HOUR from created) between '00' and '11'
                GROUP BY DATE(created)
                ORDER BY DATE(created))
UNION
(SELECT DATE(created),0,(AVG(CAST ( state AS numeric ))*10*0.161/1000) AS avr_day_12_22,0,0
                FROM states
                WHERE entity_id = 'sensor.consumo_general'
                AND state != 'unknown'
                AND state != ''
                AND CAST(state AS numeric) >= 0
                AND CAST(state AS numeric) <= 7500
                AND DATE(created) = current_date -1
                AND EXTRACT(HOUR from created) between '12' and '21'
                GROUP BY DATE(created)
                ORDER BY DATE(created))
UNION
(SELECT DATE(created),0,0, (AVG(CAST ( state AS numeric ))*2*0.082/1000) AS avr_day_23_00, 0
                FROM states
                WHERE entity_id = 'sensor.consumo_general'
                AND state != 'unknown'
                AND state != ''
                AND CAST(state AS numeric) >= 0
                AND CAST(state AS numeric) <= 7500
                AND DATE(created) = current_date -1
                AND EXTRACT(HOUR from created) between '22' and '23'
                GROUP BY DATE(created)
                ORDER BY DATE(created))
UNION
(SELECT DATE(created),0,0,0, (AVG(CAST ( state AS numeric ))*24*0.139/1000) AS avr_day_full
                FROM states
                WHERE entity_id = 'sensor.consumo_general'
                AND state != 'unknown'
                AND state != ''
                AND CAST(state AS numeric) >= 0
                AND CAST(state AS numeric) <= 7500
                AND DATE(created) = current_date -1
                GROUP BY DATE(created)
                ORDER BY DATE(created))
) as a 
group by ayer;"
        column: "total"
        unit_of_measurement: €
        value_template: "{{ value | round(2) }}"
      - name: Coste Ayer TSDH
        query: "SELECT DATE(created), (AVG(CAST ( state AS numeric ))*24*0.139/1000) AS total
                FROM states
                WHERE entity_id = 'sensor.consumo_general'
                AND state != 'unknown'
                AND state != ''
                AND CAST(state AS numeric) >= 0
                AND CAST(state AS numeric) <= 7500
                AND DATE(created) = current_date -1
                GROUP BY DATE(created)
                ORDER BY DATE(created);"
        column: "total"
        unit_of_measurement: €
        value_template: "{{ value | round(2) }}"
