Hi all,
Disclaimer: I am VERY new to coding so please bear with me. I learned what a string was about two months ago.
What I want is a list of chores on my dashboard, sorted automatically based on how overdue they are. I’ve managed to accomplish this using the custom template card from Mushroom on HACS, but the way I thought of is horribly inefficient and bloated. I’m noticing slowdowns because of my enormous 6000 line templates.yaml file. I’m hoping someone here might have a MUCH cleaner way of accomplishing the same thing.
Here’s how I’ve done it:
First, I made an input_datetime helper and template sensor for each chore (totaling over 30 across multiple separate categories). The “type” and “period” attributes exist because there are multiple categories of chores that I want sorted but only within those categories.
- sensor:
- name: mopping_due
state: >
{% if (((as_timestamp(now()) - state_attr('input_datetime.mopping','timestamp')) | int / 86400) | round(0) -1) >= 7 %}
due
{% elif (((as_timestamp(now()) - state_attr('input_datetime.mopping','timestamp')) | int / 86400) | round(0) -1) >= 5 %}
almost
{% else %}
notdue
{% endif %}
attributes:
type: 'weekly'
overdue: >
{{((as_timestamp(now()) - state_attr('input_datetime.mopping','timestamp')) | int / 86400) | round(0) -8}}
mdi: "mdi:broom"
period: 'seven'
name: 'Mop'
inputdatetime: 'input_datetime.mopping'
Then, I created an additional template sensor for each chore, this time sorting them.
- sensor:
- name: 0th_weekly_cleaning
state: >
{% set x = (states.sensor
| selectattr('state', 'in', ['notdue','almost', 'due'])
| selectattr("attributes.type", 'eq', 'weekly')
| sort(attribute="attributes.overdue", reverse=true)) %}
{% set output = namespace(task=[]) %}
{% for state in x %}
{% set s = state_attr(state.entity_id, 'overdue') %}
{% set output.task = output.task + [s] %}
{% endfor %}
{{ output.task[0] }}
attributes:
name: >
{% set x = (states.sensor
| selectattr('state', 'in', ['notdue','almost', 'due'])
| selectattr("attributes.type", 'eq', 'weekly')
| sort(attribute="attributes.overdue", reverse=true)) %}
{% set output = namespace(task=[]) %}
{% for state in x %}
{% set s = state_attr(state.entity_id, 'name') %}
{% set output.task = output.task + [s] %}
{% endfor %}
{{ output.task[0] }}
mdi: >
{% set x = (states.sensor
| selectattr('state', 'in', ['notdue','almost', 'due'])
| selectattr("attributes.type", 'eq', 'weekly')
| sort(attribute="attributes.overdue", reverse=true)) %}
{% set output = namespace(task=[]) %}
{% for state in x %}
{% set s = state_attr(state.entity_id, 'mdi') %}
{% set output.task = output.task + [s] %}
{% endfor %}
{{ output.task[0] }}
inputdatetime: >
{% set x = (states.sensor
| selectattr('state', 'in', ['notdue','almost', 'due'])
| selectattr("attributes.type", 'eq', 'weekly')
| sort(attribute="attributes.overdue", reverse=true)) %}
{% set output = namespace(task=[]) %}
{% for state in x %}
{% set s = state_attr(state.entity_id, 'inputdatetime') %}
{% set output.task = output.task + [s] %}
{% endfor %}
{{ output.task[0] }}
due: >
{% set x = (states.sensor
| selectattr('state', 'in', ['notdue','almost', 'due'])
| selectattr("attributes.type", 'eq', 'weekly')
| sort(attribute="attributes.overdue", reverse=true)) %}
{% set output = namespace(task=[]) %}
{% for state in x %}
{% set s = states(state.entity_id) %}
{% set output.task = output.task + [s] %}
{% endfor %}
{{ output.task[0] }}
secondary: >
{% set x = (states.sensor
| selectattr('state', 'in', ['notdue','almost', 'due'])
| selectattr("attributes.type", 'eq', 'weekly')
| sort(attribute="attributes.overdue", reverse=true)) %}
{% set output = namespace(task=[]) %}
{% set output2 = namespace(task=[]) %}
{% for state in x %}
{% set n = state_attr(state.entity_id, 'overdue') %}
{% set output2.task = output2.task + [n] %}
{% set s = state_attr(state.entity_id, 'period') %}
{% set output.task = output.task + [s] %}
{% endfor %}
{% if output2.task[0] == -1 %}
Every 7 days - Due tomorrow
{% elif output2.task[0] ==0 %}
Every 7 days - Due today
{% elif output2.task[0] ==1 %}
Every 7 days - 1 day overdue
{% elif output2.task[0] >1 %}
Every 7 days - {{output2.task[0]}} days overdue
{% elif output2.task[0] <-1 %}
Every 7 days - {{ output2.task[0] * -1}} days left
{% endif %}
Finally, I made cards for the sorted template sensors.
- type: custom:mushroom-template-card
primary: '{{ state_attr(''sensor.0th_weekly_cleaning'',''name'')}}'
secondary: '{{ state_attr(''sensor.0th_weekly_cleaning'',''secondary'')}}'
icon: '{{ state_attr(''sensor.0th_weekly_cleaning'',''mdi'')}}'
icon_color: |-
{% if is_state_attr('sensor.0th_weekly_cleaning','due', 'due') %}
red
{% elif is_state_attr('sensor.0th_weekly_cleaning','due', 'almost') %}
#DF7853
{% else %}
green
{% endif %}
badge_icon: |-
{% if is_state_attr('sensor.0th_weekly_cleaning','due', 'due') %}
mdi:alert-circle
{% else %}
{% endif %}
badge_color: |-
{% if is_state_attr('sensor.0th_weekly_cleaning','due', 'due') %}
red
{% elif is_state_attr('sensor.0th_weekly_cleaning','due', 'almost') %}
#DF7853
{% else %}
green
{% endif %}
tap_action:
action: none
hold_action:
action: none
double_tap_action:
action: call-service
service: script.resetweekly0
This is the script for the above card
resetweeklychore0:
alias: Resetweeklychore0
sequence:
- service: input_datetime.set_datetime
data_template:
date: '{{ now().strftime(''%Y-%m-%d'') }}'
target:
entity_id: '{{ state_attr(''sensor.0th_weekly_chore'', ''inputdatetime'') }}'
mode: single
One way I thought might help is if I could somehow store the value of the following block and then reference that in my templates instead of repeating it a million times, but it seems that the state of a sensor can’t be above 255 characters. I have Node-RED maybe I could do something there?
{% set x = (states.sensor
| selectattr('state', 'in', ['notdue','almost', 'due'])
| selectattr("attributes.type", 'eq', 'weekly')
| sort(attribute="attributes.overdue", reverse=true)) %}
I’m aware of the Lovelace Auto Entities card , but I want the customizability of the template card, including the ability to assign separate tap actions to each chore.
If there’s no better way, do you think a faster CPU might fix things? I’m running HAOS on a vm on my Synology NAS (CPU is a Celeron J4125). Maybe if I upgraded to something like an i5-12400 and ran HAOS on bare metal it might help.