#Define script variables with iterative code

1 messages · Page 1 of 1 (latest)

exotic edge
#

I currently have a script that builds a complex voice announcement in the message field of the announce action.
For better debugging (mostly) I want to define parts of it as variables.

But the way the message is built doesn't work too well with functional style templating and uses iterative loops.
I don't see a straightforward way to get this template split and hooked into the "Define Variable" action.
I don't want to build the entire text, but the lists currently accumulated in the acc namespace.

{% set today = today_at() %}
{% set tomorrow = today_at("23:59") %}
{% set acc = namespace(due= [], overdue= [], other= action_response['todo.display']['items'] | selectattr('due', 'undefined')) %}

{% for item in action_response['todo.display']['items'] | selectattr('due', 'defined') %}
  {% set itemtime = strptime(item.due, "%Y-%m-%d") | as_local %}
  {% if itemtime < tomorrow %}
    {% if itemtime > today %}
      {% set acc.due = acc.due + [item] %}
    {% else %}
      {% set acc.overdue = acc.overdue + [item] %}
    {% endif %}
  {% else %}
    {% set acc.other = acc.other + [item] %}
  {% endif %}
{% endfor %}

{% if acc.due %}
Tasks Today: 
{% for item in acc.due -%}
  {{ item.summary }};
{% endfor -%}
{% endif -%}

{% if acc.overdue %}
Tasks overdue:
{% for item in acc.overdue -%}
  {{ item.summary }};
{% endfor -%}
{% endif -%}

{% if acc.other %}
Unscheduled Tasks:
{% for item in acc.other -%}
  {{ item.summary }};
{% endfor -%}
{% endif %}
exotic edge
#

I did some more searching and figured out how to do the iteration in the variable definition

#
  - variables:
      todos: "{{ action_response['todo.display']['items'] }}"
      unscheduled: "{{ todos | selectattr('due', 'undefined') | list }}"
      scheduled: "{{ todos | selectattr('due', 'defined') | list }}"
      ready: |
        {% set acc = namespace(due= [], overdue= []) %}
        {% set today = today_at() %}
        {% set tomorrow = today_at("23:59") %}

        {% for item in scheduled %}
          {% set itemtime = strptime(item.due, "%Y-%m-%d") | as_local %}
          {% if itemtime < tomorrow %}
            {% if itemtime >= today %}
              {% set acc.due = acc.due + [item] %}
            {% else %}
              {% set acc.overdue = acc.overdue + [item] %}
            {% endif %}
          {% endif %}
        {% endfor %}
        {
          'due': {{acc.due}},
          'overdue': {{ acc.overdue }}
        }
      any_task: "{{ (unscheduled + ready.due + ready.overdue) | length > 0}}"
  - action: assist_satellite.announce
    metadata: {}
    data:
      message: |
        {% if any_task %}
          {% if ready.due %}
            Tasks Today: 
            {% for item in ready.due -%}
              {{ item.summary }};
            {% endfor -%}
          {% endif -%}

          {% if ready.overdue %}
            Tasks overdue:
            {% for item in ready.overdue -%}
              {{ item.summary }};
            {% endfor -%}
          {% endif -%}

          {% if unscheduled %}
            Unscheduled Tasks:
            {% for item in unscheduled -%}
              {{ item.summary }};
            {% endfor -%}
          {% endif %}
        {% else %}
        No tasks today
        {% endif %}

It's not the prettiest ever, but it works