Hi,
I’m using the template integration to create a state template finding some values. Only one should set in state and others at attributes. I think it isn’t fine to running the same template in every attribute define again. So how can I set the different attributes.
sensor:
- unique_id: tmp_urlaubskalender
name: tmp_urlaubskalender
icon: mdi:calendar-search
state: >-
{% set nextoffset = namespace(found = false, offset = 999, date = '') %}
{% for scheduler in integration_entities('scheduler') %}
{% if 'Urlaub' in state_attr(scheduler,'tags') %}
{% if not (state_attr(scheduler,'next_trigger') in ['unavailable', 'unknown', 'none']) %}
{% set today = as_timestamp(now()) %}
{% set next = as_timestamp(state_attr(scheduler,'next_trigger')) %}
{% set offset = ((next-today)/86400) | round(0,'ceil') %}
{% if offset < nextoffset.offset %}
{% set nextoffset.found = true %}
{% set nextoffset.offset = offset %}
{% set nextoffset.date = next | timestamp_custom('%d.%m.') %}
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
{{- nextoffset.found -}}
So here you can see setting the state to nextoffset.found and I want to set at attribute call offset to nextoffset.offset …
But without using always this code to get the different values?
I hope it’s clear ![:wink: :wink:](https://community.home-assistant.io/images/emoji/twitter/wink.png?v=10)
Thank you, Steffen
Maybe it’s a little bit clearer if I post the full code. Here You can see that I run nearly the same code three times (yes a little bit of optimization is needed), an using ressources to fill the state and different attributes. The question is can I fill attributes with only on running template (reference the variables of state in attribute section or create attributes from state section …)?
- trigger:
- platform: homeassistant
event: start
- platform: time
at:
- '00:00:10'
- '12:00:10'
- platform: state
entity_id:
- input_boolean.urlaubsmodus
- platform: state
entity_id:
- input_boolean.templatetrigger
to: 'on'
sensor:
- unique_id: tmp_nextdate_urlaubskalender
name: tmp_nextdate_urlaubskalender
device_class: date
icon: mdi:calendar-search
state: >-
{% set nextoffset = namespace(found = false, offset = 365, date = '') %}
{% for scheduler in integration_entities('scheduler') %}
{% if 'Urlaub' in state_attr(scheduler,'tags') %}
{% if not (state_attr(scheduler,'next_trigger') in ['unavailable', 'unknown', 'none']) %}
{% set today = as_timestamp(now().date()) %}
{% set next = as_timestamp((state_attr(scheduler,'next_trigger') | as_datetime | as_local).date()) %}
{% set offset = ((next-today)/86400) | round(0,'ceil') %}
{% if offset < nextoffset.offset %}
{% set nextoffset.found = true %}
{% set nextoffset.offset = offset %}
{% set nextoffset.date = next %}
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
{% if nextoffset.found == true %}
{{- nextoffset.date | timestamp_custom('%Y-%m-%d') -}}
{% else %}
{{- none -}}
{% endif %}
attributes:
days_left: >-
{% set nextoffset = namespace(found = false, offset = 365, date = '') %}
{% for scheduler in integration_entities('scheduler') %}
{% if 'Urlaub' in state_attr(scheduler,'tags') %}
{% if not (state_attr(scheduler,'next_trigger') in ['unavailable', 'unknown', 'none']) %}
{% set today = as_timestamp(now().date()) %}
{% set next = as_timestamp((state_attr(scheduler,'next_trigger') | as_datetime | as_local).date()) %}
{% set offset = ((next-today)/86400) | round(0,'ceil') %}
{% if offset < nextoffset.offset %}
{% set nextoffset.found = true %}
{% set nextoffset.offset = offset %}
{% set nextoffset.date = next %}
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
{% if nextoffset.found == true %}
{{- nextoffset.offset -}}
{% else %}
{{- none -}}
{% endif %}
next_date_text: >-
{% set nextoffset = namespace(found = false, offset = 365, date = '') %}
{% for scheduler in integration_entities('scheduler') %}
{% if 'Urlaub' in state_attr(scheduler,'tags') %}
{% if not (state_attr(scheduler,'next_trigger') in ['unavailable', 'unknown', 'none']) %}
{% set today = as_timestamp(now().date()) %}
{% set next = as_timestamp((state_attr(scheduler,'next_trigger') | as_datetime | as_local).date()) %}
{% set offset = ((next-today)/86400) | round(0,'ceil') %}
{% if offset < nextoffset.offset %}
{% set nextoffset.found = true %}
{% set nextoffset.offset = offset %}
{% set nextoffset.date = next %}
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
{% if nextoffset.found == true %}
{% if (nextoffset.offset) <= 0 %}
{{- "Heute startet der Urlaub!" -}}
{% elif (nextoffset.offset) < 1 %}
{{- "Nächster Urlaub startet Morgen (" + (nextoffset.date | timestamp_custom('%d.%m.')) + ")." -}}
{% elif (nextoffset.offset) < 2 %}
{{- "Nächster Urlaub startet Übermorgen (" + (nextoffset.date | timestamp_custom('%d.%m.')) + ")." -}}
{% else %}
{{- "Nächster Urlaub in " -}}
{{- nextoffset.offset | round(0,'ceil') -}}
{{- " Tagen (" + (nextoffset.date | timestamp_custom('%d.%m.')) + ")." -}}
{% endif %}
{% else %}
{{- none -}}
{% endif %}