input_number:
  brita_remaining_liters:
    name: Remaining liters
    unit_of_measurement: "liters"
    mode: box
    min: 0
    max: 150
    step: 0.5
    icon: mdi:water

input_datetime:
  brita_next_change:
    name: Filter next change
    has_date: true
    has_time: false

binary_sensor:
  - platform: template
    sensors:
      brita_should_change_filter:
        friendly_name: Should change Brita filter
        device_class: problem
        value_template: >-

          {% set liters_check = states('input_number.brita_remaining_liters')|int < 3 %}

          {% set next_change = states('input_datetime.brita_next_change') %}
          {% set time_delta = (as_timestamp(next_change) - as_timestamp(now())) %}
          {% set date_check = time_delta / (3600*24) | round(0) < 3 %}

          {{ liters_check or date_check }}

script:
  brita_pour_water:
    alias: Brita · Pour water
    icon: mdi:cup-water
    sequence:
      - service: input_number.set_value
        entity_id: input_number.brita_remaining_liters
        data_template:
          value: "{{ states('input_number.brita_remaining_liters') | float - 1 }}"

  brita_new_filter:
    alias: Brita · New filter
    icon: mdi:filter-plus-outline
    sequence:
      - service: input_number.set_value
        entity_id: input_number.brita_remaining_liters
        data:
          value: 100
      - service: input_datetime.set_datetime
        entity_id: input_datetime.brita_next_change
        data_template:
          date: "{{ (as_timestamp(now()) + 3600*24*60) | timestamp_custom('%Y-%m-%d') }}"
