xirixiz / homeassistant-afvalwijzer

Provides sensors for some Dutch waste collectors
MIT License
180 stars 69 forks source link

Made a useful addition! #99

Closed neforce closed 4 years ago

neforce commented 4 years ago

Hi Xirixiz,

I made a useful addition to this component. However, i don't know how to add this to the repo, but please take a look on this.

The addition only works (for me, for now) for papier, gft, pmd en rest.

Please see attached screenshot:

trash

CODE SENSORS:

-- config/includes/garbage.yaml --

`- platform: template sensors: first_trash_date: value_template: >-

      {% set selected_item = 0 %}
      {% set gftInDays = state_attr('sensor.afvalwijzer_gft', 'days_until_collection_date') | default(99) %}
      {% set papierInDays = state_attr('sensor.afvalwijzer_papier', 'days_until_collection_date') | default(99) %}
      {% set pmdInDays = state_attr('sensor.afvalwijzer_pmd', 'days_until_collection_date') | default(99) %}
      {% set restAfvalInDays = state_attr('sensor.afvalwijzer_restafval', 'days_until_collection_date') | default(99) %}

      {% set daysDutch = ['Zondag', 'Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag', 'Zaterdag'] %}
      {% set todayDateTime = as_timestamp(now())|int %}
      {% set nextInDays = [gftInDays, papierInDays, pmdInDays, restAfvalInDays] %}
      {% set sortedNextInDays = nextInDays|sort %}

      {% if sortedNextInDays[ selected_item ] == gftInDays %}
        {% if (gftInDays <= 2) %}
          {{ 'Vandaag' if (gftInDays == 0) else ('Morgen' if (gftInDays == 1) else 'Overmorgen') }}
        {% else %}
          {% set dayNumber = ((todayDateTime + (gftInDays * 86400))|timestamp_custom('%w'))|int %}
          {% if (gftInDays <= 7) %}
            {{ daysDutch[dayNumber] }}
          {% elif (gftInDays > 14) %}
            {{ daysDutch[dayNumber] }} over twee week
          {% else %}
              Volgende week {{ daysDutch[dayNumber]|lower }}
          {% endif %}
        {% endif %}
      {% endif %}

      {% if sortedNextInDays[ selected_item ] == papierInDays %}
        {% if (papierInDays <= 2) %}
          {{ 'Vandaag' if (papierInDays == 0) else ('Morgen' if (papierInDays == 1) else 'Overmorgen') }}
        {% else %}
          {% set dayNumber = ((todayDateTime + (papierInDays * 86400))|timestamp_custom('%w'))|int %}
          {% if (papierInDays <= 7) %}
            {{ daysDutch[dayNumber] }}
          {% elif (papierInDays > 14) %}
            {{ daysDutch[dayNumber] }} over twee week
          {% else %}
              Volgende week {{ daysDutch[dayNumber]|lower }}
          {% endif %}
        {% endif %}
      {% endif %}

      {% if sortedNextInDays[ selected_item ] == pmdInDays %}
        {% if (pmdInDays <= 2) %}
          {{ 'Vandaag' if (pmdInDays == 0) else ('Morgen' if (pmdInDays == 1) else 'Overmorgen') }}
        {% else %}
          {% set dayNumber = ((todayDateTime + (pmdInDays * 86400))|timestamp_custom('%w'))|int %}
          {% if (pmdInDays <= 7) %}
            {{ daysDutch[dayNumber] }}
          {% elif (pmdInDays > 14) %}
            {{ daysDutch[dayNumber] }} over twee week
          {% else %}
              Volgende week {{ daysDutch[dayNumber]|lower }}
          {% endif %}
        {% endif %}
      {% endif %}

      {% if sortedNextInDays[ selected_item ] == restAfvalInDays %}
        {% if (restAfvalInDays <= 2) %}
          {{ 'Vandaag' if (restAfvalInDays == 0) else ('Morgen' if (restAfvalInDays == 1) else 'Overmorgen') }}
        {% else %}
          {% set dayNumber = ((todayDateTime + (restAfvalInDays * 86400))|timestamp_custom('%w'))|int %}
          {% if (restAfvalInDays <= 7) %}
            {{ daysDutch[dayNumber] }}
          {% elif (restAfvalInDays > 14) %}
            {{ daysDutch[dayNumber] }} over twee week
          {% else %}
              Volgende week {{ daysDutch[dayNumber]|lower }}
          {% endif %}
        {% endif %}
      {% endif %}
first_trash_name:
  value_template: >-

      {% set selected_item = 0 %}
      {% set trash_gft_date_ts = as_timestamp(strptime(states.sensor.afvalwijzer_gft.state, "%d-%m-%Y"))|int %}
      {% set trash_papier_date_ts = as_timestamp(strptime(states.sensor.afvalwijzer_papier.state, "%d-%m-%Y"))|int %}
      {% set trash_pmd_date_ts = as_timestamp(strptime(states.sensor.afvalwijzer_pmd.state, "%d-%m-%Y"))|int %}
      {% set trash_restafval_date_ts = as_timestamp(strptime(states.sensor.afvalwijzer_restafval.state, "%d-%m-%Y"))|int %}
      {% set trash_ts_array = [trash_gft_date_ts, trash_pmd_date_ts, trash_papier_date_ts, trash_restafval_date_ts] %}
      {% set sorted_trash_ts_array = trash_ts_array|sort %}
      {% set selected_timestamp = sorted_trash_ts_array[ selected_item ] %}

      {% if (selected_timestamp == trash_papier_date_ts) %}
          papier
      {% elif (selected_timestamp == trash_pmd_date_ts) %}
          pmd
      {% elif (selected_timestamp == trash_gft_date_ts) %}
          gft
      {% elif (selected_timestamp == trash_restafval_date_ts) %}
          restafval
      {% else %}
          geen
      {% endif %}
second_trash_date:
  value_template: >-

      {% set selected_item = 1 %}
      {% set gftInDays = state_attr('sensor.afvalwijzer_gft', 'days_until_collection_date') | default(99) %}
      {% set papierInDays = state_attr('sensor.afvalwijzer_papier', 'days_until_collection_date') | default(99) %}
      {% set pmdInDays = state_attr('sensor.afvalwijzer_pmd', 'days_until_collection_date') | default(99) %}
      {% set restAfvalInDays = state_attr('sensor.afvalwijzer_restafval', 'days_until_collection_date') | default(99) %}

      {% set daysDutch = ['Zondag', 'Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag', 'Zaterdag'] %}
      {% set todayDateTime = as_timestamp(now())|int %}
      {% set nextInDays = [gftInDays, papierInDays, pmdInDays, restAfvalInDays] %}
      {% set sortedNextInDays = nextInDays|sort %}

      {% if sortedNextInDays[ selected_item ] == gftInDays %}
        {% if (gftInDays <= 2) %}
          {{ 'Vandaag' if (gftInDays == 0) else ('Morgen' if (gftInDays == 1) else 'Overmorgen') }}
        {% else %}
          {% set dayNumber = ((todayDateTime + (gftInDays * 86400))|timestamp_custom('%w'))|int %}
          {% if (gftInDays <= 7) %}
            {{ daysDutch[dayNumber] }}
          {% elif (gftInDays > 14) %}
            {{ daysDutch[dayNumber] }} over twee week
          {% else %}
              Volgende week {{ daysDutch[dayNumber]|lower }}
          {% endif %}
        {% endif %}
      {% endif %}

      {% if sortedNextInDays[ selected_item ] == papierInDays %}
        {% if (papierInDays <= 2) %}
          {{ 'Vandaag' if (papierInDays == 0) else ('Morgen' if (papierInDays == 1) else 'Overmorgen') }}
        {% else %}
          {% set dayNumber = ((todayDateTime + (papierInDays * 86400))|timestamp_custom('%w'))|int %}
          {% if (papierInDays <= 7) %}
            {{ daysDutch[dayNumber] }}
          {% elif (papierInDays > 14) %}
            {{ daysDutch[dayNumber] }} over twee week
          {% else %}
              Volgende week {{ daysDutch[dayNumber]|lower }}
          {% endif %}
        {% endif %}
      {% endif %}

      {% if sortedNextInDays[ selected_item ] == pmdInDays %}
        {% if (pmdInDays <= 2) %}
          {{ 'Vandaag' if (pmdInDays == 0) else ('Morgen' if (pmdInDays == 1) else 'Overmorgen') }}
        {% else %}
          {% set dayNumber = ((todayDateTime + (pmdInDays * 86400))|timestamp_custom('%w'))|int %}
          {% if (pmdInDays <= 7) %}
            {{ daysDutch[dayNumber] }}
          {% elif (pmdInDays > 14) %}
            {{ daysDutch[dayNumber] }} over twee week
          {% else %}
              Volgende week {{ daysDutch[dayNumber]|lower }}
          {% endif %}
        {% endif %}
      {% endif %}

      {% if sortedNextInDays[ selected_item ] == restAfvalInDays %}
        {% if (restAfvalInDays <= 2) %}
          {{ 'Vandaag' if (restAfvalInDays == 0) else ('Morgen' if (restAfvalInDays == 1) else 'Overmorgen') }}
        {% else %}
          {% set dayNumber = ((todayDateTime + (restAfvalInDays * 86400))|timestamp_custom('%w'))|int %}
          {% if (restAfvalInDays <= 7) %}
            {{ daysDutch[dayNumber] }}
          {% elif (restAfvalInDays > 14) %}
            {{ daysDutch[dayNumber] }} over twee week
          {% else %}
              Volgende week {{ daysDutch[dayNumber]|lower }}
          {% endif %}
        {% endif %}
      {% endif %}
second_trash_name:
  value_template: >-

      {% set selected_item = 1 %}
      {% set trash_gft_date_ts = as_timestamp(strptime(states.sensor.afvalwijzer_gft.state, "%d-%m-%Y"))|int %}
      {% set trash_papier_date_ts = as_timestamp(strptime(states.sensor.afvalwijzer_papier.state, "%d-%m-%Y"))|int %}
      {% set trash_pmd_date_ts = as_timestamp(strptime(states.sensor.afvalwijzer_pmd.state, "%d-%m-%Y"))|int %}
      {% set trash_restafval_date_ts = as_timestamp(strptime(states.sensor.afvalwijzer_restafval.state, "%d-%m-%Y"))|int %}
      {% set trash_ts_array = [trash_gft_date_ts, trash_pmd_date_ts, trash_papier_date_ts, trash_restafval_date_ts] %}
      {% set sorted_trash_ts_array = trash_ts_array|sort %}
      {% set selected_timestamp = sorted_trash_ts_array[ selected_item ] %}

      {% if (selected_timestamp == trash_papier_date_ts) %}
          papier
      {% elif (selected_timestamp == trash_pmd_date_ts) %}
          pmd
      {% elif (selected_timestamp == trash_gft_date_ts) %}
          gft
      {% elif (selected_timestamp == trash_restafval_date_ts) %}
          restafval
      {% else %}
          geen
      {% endif %}
third_trash_date:
  value_template: >-

      {% set selected_item = 2 %}
      {% set gftInDays = state_attr('sensor.afvalwijzer_gft', 'days_until_collection_date') | default(99) %}
      {% set papierInDays = state_attr('sensor.afvalwijzer_papier', 'days_until_collection_date') | default(99) %}
      {% set pmdInDays = state_attr('sensor.afvalwijzer_pmd', 'days_until_collection_date') | default(99) %}
      {% set restAfvalInDays = state_attr('sensor.afvalwijzer_restafval', 'days_until_collection_date') | default(99) %}

      {% set daysDutch = ['Zondag', 'Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag', 'Zaterdag'] %}
      {% set todayDateTime = as_timestamp(now())|int %}
      {% set nextInDays = [gftInDays, papierInDays, pmdInDays, restAfvalInDays] %}
      {% set sortedNextInDays = nextInDays|sort %}

      {% if sortedNextInDays[ selected_item ] == gftInDays %}
        {% if (gftInDays <= 2) %}
          {{ 'Vandaag' if (gftInDays == 0) else ('Morgen' if (gftInDays == 1) else 'Overmorgen') }}
        {% else %}
          {% set dayNumber = ((todayDateTime + (gftInDays * 86400))|timestamp_custom('%w'))|int %}
          {% if (gftInDays <= 7) %}
            {{ daysDutch[dayNumber] }}
          {% elif (gftInDays > 14) %}
            {{ daysDutch[dayNumber] }} over twee week
          {% else %}
              Volgende week {{ daysDutch[dayNumber]|lower }}
          {% endif %}
        {% endif %}
      {% endif %}

      {% if sortedNextInDays[ selected_item ] == papierInDays %}
        {% if (papierInDays <= 2) %}
          {{ 'Vandaag' if (papierInDays == 0) else ('Morgen' if (papierInDays == 1) else 'Overmorgen') }}
        {% else %}
          {% set dayNumber = ((todayDateTime + (papierInDays * 86400))|timestamp_custom('%w'))|int %}
          {% if (papierInDays <= 7) %}
            {{ daysDutch[dayNumber] }}
          {% elif (papierInDays > 14) %}
            {{ daysDutch[dayNumber] }} over twee week
          {% else %}
              Volgende week {{ daysDutch[dayNumber]|lower }}
          {% endif %}
        {% endif %}
      {% endif %}

      {% if sortedNextInDays[ selected_item ] == pmdInDays %}
        {% if (pmdInDays <= 2) %}
          {{ 'Vandaag' if (pmdInDays == 0) else ('Morgen' if (pmdInDays == 1) else 'Overmorgen') }}
        {% else %}
          {% set dayNumber = ((todayDateTime + (pmdInDays * 86400))|timestamp_custom('%w'))|int %}
          {% if (pmdInDays <= 7) %}
            {{ daysDutch[dayNumber] }}
          {% elif (pmdInDays > 14) %}
            {{ daysDutch[dayNumber] }} over twee week
          {% else %}
              Volgende week {{ daysDutch[dayNumber]|lower }}
          {% endif %}
        {% endif %}
      {% endif %}

      {% if sortedNextInDays[ selected_item ] == restAfvalInDays %}
        {% if (restAfvalInDays <= 2) %}
          {{ 'Vandaag' if (restAfvalInDays == 0) else ('Morgen' if (restAfvalInDays == 1) else 'Overmorgen') }}
        {% else %}
          {% set dayNumber = ((todayDateTime + (restAfvalInDays * 86400))|timestamp_custom('%w'))|int %}
          {% if (restAfvalInDays <= 7) %}
            {{ daysDutch[dayNumber] }}
          {% elif (restAfvalInDays > 14) %}
            {{ daysDutch[dayNumber] }} over twee week
          {% else %}
              Volgende week {{ daysDutch[dayNumber]|lower }}
          {% endif %}
        {% endif %}
      {% endif %}
third_trash_name:
  value_template: >-

      {% set selected_item = 2 %}
      {% set trash_gft_date_ts = as_timestamp(strptime(states.sensor.afvalwijzer_gft.state, "%d-%m-%Y"))|int %}
      {% set trash_papier_date_ts = as_timestamp(strptime(states.sensor.afvalwijzer_papier.state, "%d-%m-%Y"))|int %}
      {% set trash_pmd_date_ts = as_timestamp(strptime(states.sensor.afvalwijzer_pmd.state, "%d-%m-%Y"))|int %}
      {% set trash_restafval_date_ts = as_timestamp(strptime(states.sensor.afvalwijzer_restafval.state, "%d-%m-%Y"))|int %}
      {% set trash_ts_array = [trash_gft_date_ts, trash_pmd_date_ts, trash_papier_date_ts, trash_restafval_date_ts] %}
      {% set sorted_trash_ts_array = trash_ts_array|sort %}
      {% set selected_timestamp = sorted_trash_ts_array[ selected_item ] %}

      {% if (selected_timestamp == trash_papier_date_ts) %}
          papier
      {% elif (selected_timestamp == trash_pmd_date_ts) %}
          pmd
      {% elif (selected_timestamp == trash_gft_date_ts) %}
          gft
      {% elif (selected_timestamp == trash_restafval_date_ts) %}
          restafval
      {% else %}
          geen
      {% endif %}`

-- lovelace yaml --

`- cards:

If you need more info, or have any additions, please let me know.

xirixiz commented 4 years ago

Good work @neforce! I think it's usefull you shared this! However it's a lot of code within Home Assistant. Maybe this is some nice feature to implement in this component itself as sensors with attributes like "volgende week woensdag". In this case you don't have to add complex code to Home Assistant.

Anyway nice job! You could als share it in the community: https://community.home-assistant.io/t/garbage-pickup-date-mijnafvalwijzer-nl-custom-component/34631

xirixiz commented 4 years ago

@Mariusthvdb probably also likes this 😃

Mariusthvdb commented 4 years ago

HI, must be missing the obvious here, but what would be the difference with:

Schermafbeelding 2020-10-15 om 13 58 53

other than the layout which is a simple frontend thing?

neforce commented 4 years ago

HI, must be missing the obvious here, but what would be the difference with:

Schermafbeelding 2020-10-15 om 13 58 53

other than the layout which is a simple frontend thing?

I think day names (donderdag over een week, woensdag, morgen, vandaag) are nicer and easier to read/calculate than; 'Over 12 dagen'.

The code can be probably much simplified, but hey, it works :)

Mariusthvdb commented 4 years ago

ok, if you want a day name, use the attribute timestamp as on these template sensors I made:

      afvalwijzer_restafval_next:
        friendly_name: Restafval
        entity_picture_template: /local/afvalwijzer/restafval_saver.png
        value_template: >
          {{state_attr('sensor.afvalwijzer_restafval','days_until_collection_date')}}
        attribute_templates:
          date: >
            {{as_timestamp(strptime(states('sensor.afvalwijzer_restafval'),'%d-%m-%Y'))
              |timestamp_custom('%a %d %b')}}
          timestamp: >
            {{as_timestamp(strptime(states('sensor.afvalwijzer_restafval'),'%d-%m-%Y'))}}

and recalculate the day from that, or, even easier use:

            {{as_timestamp(strptime(states('sensor.afvalwijzer_restafval'),'%d-%m-%Y'))
              |timestamp_custom('%a')}}

and a translate template if you want it in Dutch ;-) you are way overcomplicating things...

btw, here's one to play with and adapt if you'd like.

      volgende_afval_ophaal_dag:
        friendly_name: Volgende afval-ophaal dag
        entity_id: sensor.afvalwijzer_first_next_date
        value_template: >
          {%- set tracker_timestamp = as_timestamp(strptime(states('sensor.afvalwijzer_first_next_date'),'%d-%m-%Y')) %}
          {%  set months = ['Januari','Februari','Maart','April','Mei','Juni','Juli','Augustus','September','Oktober','November','December'] %}
          {%- set wdays = ['Zondag','Maandag','Dinsdag','Woensdag','Donderdag','Vrijdag','Zaterdag'] %}
          {%- set wday = tracker_timestamp|timestamp_custom('%w')|int %}
          {%- set month = tracker_timestamp |timestamp_custom('%m')|int %}
          {{tracker_timestamp|timestamp_custom(wdays[wday]  + ' ' '%d' ' ' + months[month-1])}}

add a 'phrase' for the 'over 1/2/3 we(e)ken' (don't like over 2 week but that's probably a typo, sorry...) and you're probably set?

Mariusthvdb commented 4 years ago

@Mariusthvdb probably also likes this 😃

yeah, but I would prefer the code in the integration ;-) this is way too complex in jinja for the extra added value of the day phrase (because that's the only thing we don't have yet, in Dutch) imho.

neforce commented 4 years ago

@Mariusthvdb if i quickly scan your code, it's moreorless the same i think? What my code does:

and so on.

This was all build for version 4.X, maybe some things can be simplified now for version 5.X.

the hard part was;

'Let's say it's monday 12 october, and PMD will be emptied on friday 23', we'll need to check if friday 23 is in the current week, if so, is it on the first available friday, or is a week later? And more of these 'questions'.

It was quite hard and digged to deep into it i think that i lost sometimes overview.

But again, it does what i want :)

Mariusthvdb commented 4 years ago

if you only need the representation in the frontend, have a look at this:

  - type: custom:auto-entities
    card:
      type: entities
      title: Volgende ophaaldata
    filter:
      include:
        - entity_id: sensor.afvalwijzer_*_next
          options:
            type: custom:template-entity-row
            state: >
              {{state_attr(config.entity,'date')}}
            secondary: >
              {% set count = states(config.entity)|int %}
              {% set unit = 'Dag' if count == 1 else 'Dagen' %}
              {% if count == 0 %} Vandaag
              {% else %} Over {{count}} {{unit}}
              {% endif %}

cause that's all I use in the card. You could adjust the jinja here somewhat, to change the lines to what you like, without having to code that huge package of sensors, and triple that for all sensors.

this ones easier for the day translation:

          {% set dagen =
            { 'Mon': 'Maandag',
              'Tue': 'Dinsdag',
              'Wed': 'Woensdag',
              'Thu': 'Donderdag',
              'Fri': 'Vrijdag',
              'Sat': 'Zaterdag',
              'Sun': 'Zondag'} %}
          {% set state = state_attr('sensor.afvalwijzer_papier_next','date').split(' ')[0] %}
          {% set dag = dagen[state] if state in dagen else state %}
          {{dag}}
Mariusthvdb commented 4 years ago

nog een beetje aangepast (some further refinements):

  - type: custom:auto-entities
    card:
      type: entities
      title: Volgende ophaaldata
    filter:
      include:
        - entity_id: sensor.afvalwijzer_*_next
          options:
            type: custom:template-entity-row
            state: >
              {{state_attr(config.entity,'date')}}
            secondary: >
              {% set count = states(config.entity)|int %}
              {% set day = state_attr(config.entity,'day') %}
              {% set unit = 'Dag' if count == 1 else 'Dagen' %}

              {% if count >= 14 %} {% set phrase = day + ' over 2 weken' %}
              {% elif count >= 7 %} {% set phrase = 'Volgende week ' + day %}
              {% elif count >= 3 %} {% set phrase = 'komende ' + day %}
              {% elif count == 2 %} {% set phrase = day + ', overmorgen' %}
              {% elif count == 1 %} {% set phrase = 'morgen, ' + day %}
              {% else %} {% set phrase = 'Vandaag, ' + day %}
              {% endif %}
              {{phrase}} {% if count != 0 %} ({{count}} {{unit}}) {% endif %}
    sort:
      method: state
      numeric: true

en het wordt al meer wat je zoekt denk ik (getting close to what you are looking for):

Schermafbeelding 2020-10-16 om 00 34 31

Of course, play around with this template.

I did add the attribute 'day' to the sensors, to make it easy in the Lovelace card

      afvalwijzer_restafval_next:
        friendly_name: Restafval
        entity_picture_template: /local/afvalwijzer/restafval_saver.png
        value_template: >
          {{state_attr('sensor.afvalwijzer_restafval','days_until_collection_date')}}
        attribute_templates:
          day: >
            {{as_timestamp(strptime(states('sensor.afvalwijzer_restafval'),'%d-%m-%Y'))
              |timestamp_custom('%A')}}
          date: >
            {{as_timestamp(strptime(states('sensor.afvalwijzer_restafval'),'%d-%m-%Y'))
              |timestamp_custom('%a %d %b')}}
          timestamp: >
            {{as_timestamp(strptime(states('sensor.afvalwijzer_restafval'),'%d-%m-%Y'))}}
xirixiz commented 4 years ago

Bedankt voor de aanvulling @Mariusthvdb! Ik denk dat het mooi is dat mensen dit kunnen vinden! Thanks voor jullie bijdrage!

Mariusthvdb commented 4 years ago

here's the translated version:

  - type: custom:auto-entities
    card:
      type: entities
      title: Volgende ophaaldata
    filter:
      include:
        - entity_id: sensor.afvalwijzer_*_next
          options:
            type: custom:template-entity-row
            state: >
              {{state_attr(config.entity,'date')}}
            secondary: >
              {% set count = states(config.entity)|int %}
              {% set day = state_attr(config.entity,'day') %}
              {% set dagen =
                {'Monday': 'Maandag',
                'Tuesday': 'Dinsdag',
                'Wednesday': 'Woensdag',
                'Thursday': 'Donderdag',
                'Friday': 'Vrijdag',
                'Saterday': 'Zaterdag',
                'Sunday': 'Zondag'} %}
              {% set dag = dagen[day] if day in dagen else day %}
              {% set unit = 'Dag' if count == 1 else 'Dagen' %}

              {% if count >= 14 %} {% set phrase = dag + ' over 2 weken' %}
              {% elif count >= 7 %} {% set phrase = 'Volgende week ' + dag %}
              {% elif count >= 3 %} {% set phrase = 'komende ' + dag %}
              {% elif count == 2 %} {% set phrase = dag + ', overmorgen' %}
              {% elif count == 1 %} {% set phrase = 'morgen, ' + dag %}
              {% else %} {% set phrase = 'Vandaag, ' + dag %}
              {% endif %}
              {{phrase}} {% if count != 0%} ({{count}} {{unit}}) {% endif %}
    sort:
      method: state
      numeric: true

of course, this is for the secondary line, if you want the state to be translated, you'd have to use another template for that. Or wait for the date to be localized some day in core HA.

Or have the CC have a localize option, which would be cool btw ;-)

neforce commented 4 years ago

here's the translated version:

  - type: custom:auto-entities
more code

of course, this is for the secondary line, if you want the state to be translated, you'd have to use another template for that. Or wait for the date to be localized some day in core HA.

Or have the CC have a localize option, which would be cool btw ;-)

Yeah, that's almost what i have now:

type: 'custom:auto-entities'
card:
  type: entities
  title: Container bij de weg
filter:
  include:
    - entity_id: sensor.afvalwijzer_*_next
      options:
        type: 'custom:template-entity-row'
        state: >
          {% set dagen =
            { 'Monday': 'Maandag',
              'Tuesday': 'Dinsdag',
              'Wednesday': 'Woensdag',
              'Thursday': 'Donderdag',
              'Friday': 'Vrijdag',
              'Saturday': 'Zaterdag',
              'Sunday': 'Zondag'} %}
          {% set count = states(config.entity)|int %} {% set day =
          state_attr(config.entity,'day') %} {% set day = dagen[day] %}

          {% set unit = 'Dag' if count == 1 else 'dagen' %}

          {% if count > 14 %} 
            {% set phrase = day + ' over twee weken' %} 
          {% elif count >= 7 and count <= 14 %}
            {% set phrase = 'Volgende week ' + day|lower %}
          {% elif count >= 3 and count <= 7 %}
            {% set phrase = day %}
          {% elif count == 2 %}
            {% set phrase = 'Overmorgen' %}
          {% elif count == 1 %}
            {% set phrase = 'Morgen' %}
          {% else %}
            {% set phrase = 'Vandaag' %}
          {% endif %}

          {{phrase}}
sort:
  method: state
  numeric: true
  count: 3

Great! Next step is to transform this into a different view (like the image in the OP) :)

Mariusthvdb commented 4 years ago

Next step is to transform this into a different view (like the image in the OP)

this is a config for the secondary-row in a template entity row created by auto-entities.... cant really transform that into a set of picture-elements.

Unless I am misunderstanding what you're saying here. For me the combination is optimal having it all in 1 dashboard (and I really like to see the 3 days, always):

Schermafbeelding 2020-10-22 om 12 25 12
rodehoed commented 3 months ago

here's the translated version:

  - type: custom:auto-entities
    card:
      type: entities
      title: Volgende ophaaldata
    filter:
      include:
        - entity_id: sensor.afvalwijzer_*_next
          options:
            type: custom:template-entity-row
            state: >
              {{state_attr(config.entity,'date')}}
            secondary: >
              {% set count = states(config.entity)|int %}
              {% set day = state_attr(config.entity,'day') %}
              {% set dagen =
                {'Monday': 'Maandag',
                'Tuesday': 'Dinsdag',
                'Wednesday': 'Woensdag',
                'Thursday': 'Donderdag',
                'Friday': 'Vrijdag',
                'Saterday': 'Zaterdag',
                'Sunday': 'Zondag'} %}
              {% set dag = dagen[day] if day in dagen else day %}
              {% set unit = 'Dag' if count == 1 else 'Dagen' %}

              {% if count >= 14 %} {% set phrase = dag + ' over 2 weken' %}
              {% elif count >= 7 %} {% set phrase = 'Volgende week ' + dag %}
              {% elif count >= 3 %} {% set phrase = 'komende ' + dag %}
              {% elif count == 2 %} {% set phrase = dag + ', overmorgen' %}
              {% elif count == 1 %} {% set phrase = 'morgen, ' + dag %}
              {% else %} {% set phrase = 'Vandaag, ' + dag %}
              {% endif %}
              {{phrase}} {% if count != 0%} ({{count}} {{unit}}) {% endif %}
    sort:
      method: state
      numeric: true

of course, this is for the secondary line, if you want the state to be translated, you'd have to use another template for that. Or wait for the date to be localized some day in core HA.

Or have the CC have a localize option, which would be cool btw ;-)

I was using the specific example from here. But it stopped working with the new version, probably because of the new timestamp class. Could anyone point me to the right direction what the breaking code is?

Mariusthvdb commented 3 months ago

I believe it was already posted in the community, where these type of support questions belong, but here you go:

    - type: custom:auto-entities
      card:
        type: entities
      filter:
        exclude:
          - entity_id: sensor.afvalwijzer*next*
          - entity_id: sensor.afvalwijzer_*orgen
          - entity_id: sensor.afvalwijzer_v*
        include:
          - entity_id: sensor.afvalwijzer_*
            options:
              type: custom:template-entity-row
# set in customize
#               image: >
#                 {% set type = config.entity.split('afvalwijzer_')[1] %}
#                 /local/afvalwijzer/{{type}}.png
              state: >
                {% set trash = config.entity %}
                {% set today = state_attr(trash,'is_collection_date_today') %}
                {% set future = state_attr(trash,'days_until_collection_date') %}
                {% if future is none or
                   (is_state(trash,'geen') and future == 0) %} Tba
                {% else %}
                {% from 'easy_time.jinja' import month %}
                {{as_datetime(states(trash)).day}}
                  {{month(as_datetime(states(trash)).month)}}
                {% endif %}
              secondary: >
                {% set trash = config.entity %}
                {% set today = state_attr(trash,'is_collection_date_today') %}
                {% set future = state_attr(trash,'days_until_collection_date') %}
                {% if future is none or
                   (is_state(trash,'geen') and future == 0) %} Nog niet gepubliceerd
                {% else %}
                  {% set count =
                    state_attr(trash,'days_until_collection_date')|int(0) %}
                  {% set dagnummer = as_datetime(states(trash)).strftime('%w')%}
                  {% from 'easy_time.jinja' import weekday %}
                  {% set dag = weekday(dagnummer|int) %}
                  {% set unit = 'dag' if count == 1 else 'dagen' %}

                  {% if count >= 28 %} {% set phrase = dag + ' over 4 weken' %}
                  {% elif count >= 21 %} {% set phrase = dag + ' over 3 weken' %}
                  {% elif count >= 14 %} {% set phrase = dag + ' over 2 weken' %}
                  {% elif count >= 7 %} {% set phrase = 'Volgende week ' + dag %}
                  {% elif count >= 3 %} {% set phrase = 'Komende ' + dag %}
                  {% elif count == 2 %} {% set phrase = dag + ', overmorgen' %}
                  {% elif count == 1 %} {% set phrase = 'Morgen, ' + dag %}
                  {% else %} {% set phrase = 'Vandaag, ' + dag %}
                  {% endif %}
                  {{phrase}} {% if count != 0 %} ({{count}} {{unit}}) {% endif %}
                {% endif %}
      sort:
        method: attribute
        attribute: days_until_collection_date
        numeric: true

note I am using the custom templates easytime which you can download via HACS