I’ve created several calendars to track the Garbage Collection schedules. As I want to display in a dashboard an image and a text with the next garbage to be collected.
The input is the next event for all the mentioned calendars which is generated by the macro how_long_till_next_event
, as follows:
[
{
"calendar": "waste",
"image": "black-container.jpg",
"days": 5,
"days_as_str": "in 5 days"
},
{
"calendar": "plastic",
"image": "yellow-container.jpg",
"days": 19,
"days_as_str": "in 19 days"
},
{
"calendar": "paper",
"image": "blue-container.jpg",
"days": 26,
"days_as_str": "in 26 days"
}
]
The waste_management
macro outputs a dictionary with the next garbage type to be collected (waste, plastic, paper)
{
"calendar": "waste",
"image": "black-container.jpg",
"days": 5,
"days_as_str": "in 5 days"
}
in the ideal world I would do:
{%set var = waste_management() %}
{{var.image}}
But when doing so I’m getting: str object' has no attribute 'image
and don’t understand why; I’ve tried AI, converting from and to JSON and several other things and such a triviality is messing with my brain. Any help and simplification would be highly appreciated!
I’m leaving here the whole snippet for reference:
{%- macro format_time(in_how_many_days) -%}
{%- if in_how_many_days == "0" -%}
{% set time = 'Today!!' %}
{%- elif in_how_many_days == "1" -%}
{% set time = 'Tomorrow!' %}
{%- else -%}
{% set time = 'in ' ~ in_how_many_days ~ ' days' %}
{%- endif -%}
{{ time }}
{%- endmacro -%}
{%- macro how_long_till_next_event(calendar) -%}
{%- set days = ((state_attr(calendar, 'start_time') | as_timestamp - today_at('00:00') | as_timestamp) / 86400 ) | int -%}
{%- set calendar_name = calendar.replace('calendar.','') -%}
{%- set image = calendar_name.replace('waste','black-container.jpg').replace('plastic','yellow-container.jpg').replace('paper','blue-container.jpg') -%}
{
"calendar": {{ calendar_name | tojson }},
"image": {{ image | tojson }},
"days": {{ days | tojson }},
"days_as_str": {{ format_time(days) | tojson }}
}
{%- endmacro -%}
{%- macro get_garbage_calendars() -%}
[
{{ how_long_till_next_event('calendar.waste') }},
{{ how_long_till_next_event('calendar.plastic') }},
{{ how_long_till_next_event('calendar.paper') }}
]
{%- endmacro -%}
{% macro min_days_object(data) %}
{%- set min_days_item = data | min(attribute='days') %}
{{ min_days_item }}
{%- endmacro %}
{% macro waste_management() %}
{%- set sensor_array = get_garbage_calendars() -%}
{# {{sensor_array}} #}
{{ min_days_object(sensor_array | from_json) }}
{%- endmacro %}
{%set var = waste_management() %}
{# WORKS #}
{{var}}
{# DOESN'T WORK #}
{{var.image}}