utility_meter:
  periodo_consumo_diario_general:
    source: sensor.consumo_integrado_general
    cycle: daily
    tariffs:
      - punta
      - valle
      - P1
      - P2
      - P3
      - P4
      - P5
      - P6
  periodo_consumo_mensual_general:
    source: sensor.consumo_integrado_general
    cycle: monthly
    offset:
      days: 17
      hours: 0
      minutes: 0
    tariffs:
      - punta
      - valle
      - P1
      - P2
      - P3
      - P4
      - P5
      - P6

sensor:
  - platform: integration
    source: sensor.consumo_general
    name: Consumo Integrado General
    unit_prefix: k
    method: left
    round: 2

  - platform: statistics
    entity_id: sensor.consumo_general
    name: maximetro
    sampling_size: 1000
    max_age:
      minutes: 14

  - platform: template
    sensors:
      consumo_general:
        friendly_name: Consumo General
        unit_of_measurement: W
        icon_template: mdi:electron-framework
        value_template: >-
           {% if states('sensor.consumo_general_original')|float >= 0 and states('sensor.consumo_general_original')|float <= 10000 -%}
                {{ states('sensor.consumo_general_original') }}
           {%- endif %}
      consumo_diario_general:
        friendly_name: Consumo Diario General
        unit_of_measurement: kWh
        icon_template: mdi:calendar-blank
        value_template: "{{ (states('sensor.periodo_consumo_diario_general_punta')|float + states('sensor.periodo_consumo_diario_general_valle')|float + states('sensor.periodo_consumo_diario_general_p1')|float + states('sensor.periodo_consumo_diario_general_p2')|float + states('sensor.periodo_consumo_diario_general_p3')|float + states('sensor.periodo_consumo_diario_general_p4')|float + states('sensor.periodo_consumo_diario_general_p5')|float + states('sensor.periodo_consumo_diario_general_p6')|float)|round(2) }}"
      consumo_mensual_general:
        friendly_name: Consumo Mensual General
        unit_of_measurement: kWh
        icon_template: mdi:calendar-blank-multiple
        value_template: "{{ (states('sensor.periodo_consumo_mensual_general_punta')|float + states('sensor.periodo_consumo_mensual_general_valle')|float + states('sensor.periodo_consumo_mensual_general_p1')|float + states('sensor.periodo_consumo_mensual_general_p2')|float + states('sensor.periodo_consumo_mensual_general_p3')|float + states('sensor.periodo_consumo_mensual_general_p4')|float + states('sensor.periodo_consumo_mensual_general_p5')|float + states('sensor.periodo_consumo_mensual_general_p6')|float)|round(2) }}"

      coste_consumo_diario:
        friendly_name: Coste Consumo Diario
        unit_of_measurement: €
        icon_template: mdi:currency-eur
        value_template: "{{ (((states('sensor.periodo_consumo_diario_general_punta')|float * 0.161 ) + (states('sensor.periodo_diario_general_valle')|float * 0.082)) * 1.21 )|round(2) }}"

      coste_consumo_mensual:
        friendly_name: Coste Consumo Mensual
        unit_of_measurement: €
        icon_template: mdi:currency-eur
        value_template: "{{ ((((3*38.043426*32)/365) + (states('sensor.periodo_consumo_mensual_general_punta')|float * 0.161 ) + (states('sensor.periodo_consumo_mensual_general_valle')|float * 0.082 ) + (32 * 0.02) + (44.72 * 0.0511269) + (32 * 0.02663)) * 1.21)|round(2) }}"
