a-p-z / datetime-card

A minimalistic card for Home Assistant Lovelace UI which shows how many days it has been between any input_datetime and today.
21 stars 1 forks source link

FR: option to show only expired items #25

Closed erkr closed 2 months ago

erkr commented 5 months ago

Hi, Please consider an option to show the expired items only. If that option could be Templatable would be even better 🫠 Like the card!!! Best Eric

erkr commented 4 months ago

I found a way to populate the card using auto entities. Now I have full control on what is shown in the card. As all configured knowledge is now in the server (instead of the card), I can also generate notifications without the need to scrape lovelace views to get the configured date time helpers and their max values (i wrote a script earlier to do that, not needed anymore).

Example populating the card using auto entities. The configuration is looked up from attributes in a template binary sensor on the first two lines of the template:

    type: custom:auto-entities
    filter:
      template: >
        {% set entities=state_attr('binary_sensor.maintenance','entity_id') %}
        {% set max_days=state_attr('binary_sensor.maintenance','max_days') %}
        {%- for entity in entities -%}
         {%- set entry = {
            'id': entity,
            'max': max_days[loop.index-1] } -%}
         {{ entry }},
        {%- endfor -%} 
    card:
      type: custom:datetime-card
      flex_direction: row
      image: ''
      show_names: true
      title: Maintenance

The next example is a selective template to populate the card with expired items only:


      template: >
        {% set entities=state_attr('binary_sensor.maintenance','entity_id') %}
        {% set max_days=state_attr('binary_sensor.maintenance','max_days') %} 
        {% set remains=state_attr('binary_sensor.maintenance','remaining') %} 
        {%- for entity in entities -%}
         {%- if remains[loop.index-1] <= 0 -%}
         {%- set entry = {
            'id': entity,
            'max': max_days[loop.index-1] } -%}
         {{ entry }},
         {%- endif -%}
        {%- endfor -%} 

The next example is the template sensor I used to maintain the configuration in a single place.

It additionally calculates the remaining days per input_datetime helper. That is not needed to populate the card with all datetime items, but can be used to selectivity populate the card (ie all expired, or what will expire tomorrow etc) The remaining days is also very convenient to send notifications (this binary sensor can be used as input for I.e. Alerts):

# Maintenance group
- trigger:
  - platform: time
    at: "00:00:05"
    variables: &my_variables_maintenance
      entities: &triggered_by_maintenance
      - input_datetime.barrista_cleaning
      - input_datetime.water_filter_barrista
      - input_datetime.brandmelder_batterijen
      - input_datetime.airco_cleaning
      - input_datetime.cv_ketel
      - input_datetime.wasmachine_90_graden_wasje
      max_days:
      - 21
      - 30
      - 365
      - 365
      - 547
      - 31
      remaining: >
            {% set current_date = now().date()  %}
            {% set ns = namespace(remaining=[]) %}
            {%- for entity in entities %}
            {% set next_date = as_datetime(states(entity)).date() + timedelta( days = max_days[loop.index-1]) %}
            {% set ns.remaining =ns.remaining + [(next_date - current_date ).days] %}
            {%- endfor -%} 
            {{ ns.remaining }}
      total: "{{ remaining | reject('gt', 0) | list | count }}"
  - platform: state
    variables: *my_variables_maintenance
    entity_id: *triggered_by_maintenance
 binary_sensor:
  - unique_id: maintenance 
    name: Maintenance
    state: "{{ total > 0 }}"
    device_class: problem
    icon: 'mdi:wrench-clock'
    attributes:
      entity_id: "{{ entities }}"
      max_days: "{{ max_days }}"
      remaining: "{{ remaining }}"

This is how the template binary sensor looks like in DevTools:


entity_id:
  - input_datetime.barrista_cleaning
  - input_datetime.water_filter_barrista
  - input_datetime.brandmelder_batterijen
  - input_datetime.airco_cleaning
  - input_datetime.cv_ketel
  - input_datetime.wasmachine_90_graden_wasje
max_days:
  - 21
  - 30
  - 365
  - 365
  - 547
  - 31
remaining:
  - 14
  - 23
  - 115
  - 79
  - 281
  - 18
device_class: problem
icon: mdi:wrench-clock
friendly_name: Maintenance

Hope this helps others. @a-p-z feel free to use this example in your readme. It obsoletes the scrape script I wrote before. Please remove that link, as I won't maintain it any longer.

Best Eric