Need help with mapper

HI,

usually when creating a value_template with many option for the value, I create a mapper and set some variables, to prevent having to write many lines over and over, As I want to do with this:

afval_overmorgen_2:
  friendly_name: Afval overmorgen
  value_template: >
    {% set papier = (as_timestamp(strptime(states('sensor.trash_papier'), "%d-%m-%Y")) - 
          (2 * 86400 )) | timestamp_custom("%d-%m-%Y") %}
    {% set gft = (as_timestamp(strptime(states('sensor.trash_gft'), "%d-%m-%Y")) - 
          (2 * 86400 )) | timestamp_custom("%d-%m-%Y") %}
    {% set plastic = (as_timestamp(strptime(states('sensor.trash_plastic'), "%d-%m-%Y")) - 
          (2 * 86400 )) | timestamp_custom("%d-%m-%Y") %}
    {% set rest = (as_timestamp(strptime(states('sensor.trash_restafval'), "%d-%m-%Y")) - 
          (2 * 86400 )) | timestamp_custom("%d-%m-%Y") %}
    {% set date = now().strftime("%d-%m-%Y") %}
    {% if date == papier %} Papier
    {% elif date == gft %} Gft
    {% elif date == plastic %} Plastic
    {% elif date == restafval %} Restafval
    {% else %} Geen
    {% endif %}

which works fine as it is. I want the final part to have the Mapper form , but can’t get it to work like this:

      {% set mapper = 
        { 'papier': 'Papier',
          'gft': 'Gft',
          'plastic': 'Plastic',
          'rest': 'Restafval'} %}
      {% set date = now().strftime("%d-%m-%Y") %}
      {% set Afval = mapper[date] if date in mapper else 'Geen' %}
        {{Afval}}

I fear the conversion to the names isn’t correct, but fail to see how it must be done at the moment… @petro, what am I missing? Of course the date isn’t in the mapper, it is in the variables that aren’t yet in the Mapper, or? I am confused…
please have a look?

I wouldn’t touch it. Maybe put it in a for loop. Mapper could cause problems if 2 fall on the same date. A dictionary (mapper) would output the last date found. A for loop would output the first date found. Either way, mapper wouldn’t actually save you any code length. A for loop would cut the code down. Also, if you actually want to account for 2 falling on the same date… Anyways, I’d create a macro.

{%- macro getTrashDays(mapper) %}
{%- for key, value in mapper.items() %}
  {%- set trashTS = as_timestamp(strptime(states('sensor.trash_'+key), '%d-%m-%Y')) %}
  {%- set trashDate = (trashTS - (2 * 86400)) | timestamp_custom("%d-%m-%Y") %}
  {%- if now().strftime("%d-%m-%Y") == trashDate %}
    {{ value+' ' }}
  {%- endif %}
{%- endfor %}
{%- endmacro %}
{% set mapper = 
    { 'papier': 'Papier',
      'gft': 'Gft',
      'plastic': 'Plastic',
      'rest': 'Restafval'} %}
{% set trashDays = getTrashDays(mapper).strip().split(' ') %}
{{ ', '.join(trashDays) if trashDays else 'Geen' }}

wow, that’s another story all together…
doesn’t work yet (no value is displayed at all) and for the sake of correctness I can’t stand that :wink:

need some investigation here…

this works:

 {% set mapper = 
    { (as_timestamp(strptime(states('sensor.trash_papier'), "%d-%m-%Y")) - 
      (1 * 86400 )) | timestamp_custom("%d-%m-%Y"): 'Papier',
      (as_timestamp(strptime(states('sensor.trash_gft'), "%d-%m-%Y")) - 
      (1 * 86400 )) | timestamp_custom("%d-%m-%Y"): 'Gft',
      (as_timestamp(strptime(states('sensor.trash_plastic'), "%d-%m-%Y")) - 
      (1 * 86400 )) | timestamp_custom("%d-%m-%Y"): 'Plastic',
      (as_timestamp(strptime(states('sensor.trash_restafval'), "%d-%m-%Y")) - 
      (1 * 86400 )) | timestamp_custom("%d-%m-%Y"): 'Restafval'} %}
  {% set date = now().strftime("%d-%m-%Y") %}
  {% set Trash = mapper[date] if date in mapper else 'Geen' %}
    {{Trash}}


apparently the intermediate variable confuses things… at least in my head :wink:

You were close. I think this might do it (although, like @petro, I’m not sure this helps much):

      {% set mapper = 
        { papier: 'Papier',
          gft: 'Gft',
          plastic: 'Plastic',
          rest: 'Restafval'} %}
      {% set date = now().strftime("%d-%m-%Y") %}
      {{ mapper[date] if date in mapper else 'Geen' }}

Also, this will only work if papier, gft, plastic & rest are always unique values, otherwise, as already stated, you’ll only have one entry in mapper per unique date.

they are unique in my municipality so that’s not the issue, and I think Ive tried unquoting the variables. (–update-- yes I have, and it doesn’t work)

It’s the intermediary set variable that frustrates this mapper. Not sure why though, because evaluating date == papier works fine.

==Update2==!

it does work, its after midnight and the days count changed…

this is correct for now:

   {% set papier = (as_timestamp(strptime(states('sensor.trash_papier'), "%d-%m-%Y")) - 
          (1 * 86400 )) | timestamp_custom("%d-%m-%Y") %}
    {% set gft = (as_timestamp(strptime(states('sensor.trash_gft'), "%d-%m-%Y")) - 
          (1 * 86400 )) | timestamp_custom("%d-%m-%Y") %}
    {% set plastic = (as_timestamp(strptime(states('sensor.trash_plastic'), "%d-%m-%Y")) - 
          (1 * 86400 )) | timestamp_custom("%d-%m-%Y") %}
    {% set rest = (as_timestamp(strptime(states('sensor.trash_restafval'), "%d-%m-%Y")) - 
          (1 * 86400 )) | timestamp_custom("%d-%m-%Y") %}   
   {% set mapper = 
        { papier: 'Papier',
          gft: 'Gft',
          plastic: 'Plastic',
          rest: 'Restafval'} %}
      {% set date = now().strftime("%d-%m-%Y") %}
      {{ mapper[date] if date in mapper else 'Geen' }}

{{date}} {{papier}}

or the final part:

      {% set Trash =  mapper[date] if date in mapper else 'Geen' %}
        {{Trash}}

cool! thanks @petro and @pnbruckner ! a lot.

Not sure I follow. Here is a test (in Template Editor):

{% set papier = '01-02-2019' %}
{% set gft = '02-02-2019' %}
{% set mapper = {papier: 'Papier', gft: 'Gft'} %}
{% set date = '01-02-2019' %}
{{ mapper[date] if date in mapper else 'Geen' }}

If date is set to the same string as papier, I get Papier. If set to gft, I get Gft. If anything else, I get Geen.