For example, here’s a Template Sensor I use to report the quantity of open doors. It also has an attribute reporting the names of all open doors. The two templates it uses are nearly identical.
Currently I need to use the content of the state in availability again plus adding a check. Depending on the state code complexity/amount, this would reduce the code by almost 50 %.
this is exactly what I was looking for, I am hoping that will be added…
because if I want to use the same code for the state (value) and for the icon (choose) the only option - I think - I have is to copy the it and paste it 2 times…and it’s a waste of resources.
In the nearly 3 years since this thread started there have been numerous features added to template sensors. You can use the self-referencing variable “this” to cover your icon issue. Additionally, trigger-based template sensors allow the use of both the trigger variable and trigger-attached variables.
Not sure if this would fulfill the Feature Request but a trigger variable might. The this variable is complicated by where it’s employed and how.
Here’s my understanding of how it works:
The state option’s template is evaluated before any attribute’s template. So if you use this.state in the state option, its value is the Template Sensor’s existing value (i.e. the value before the state option’s template is evaluated). However if you use this.state in an attribute, it’s the value after the state option’s template is evaluated.
This can make it tricky to get the desired pre or post evaluation value of this.state.
The template of a proper variable (as requested in this FR) would be evaluated before either state or any attributes (like the trigger variable for a Trigger-based Template Sensor).
Fwiw, the recent addition of custom_templates has been very useful in this regard.
It’s not the exact same thing, and requires some care in setting up, and some repetitive imports…
but at least we can import the same value in any of the states/attributes now and prevent re-evaluation in all of those
Form complex macros to simpler strings, and it is not frustrated by the intricacies of the this variable which is especially useless difficult to use in trigger based templates …
can even nest those custom_templates, see the state: or attribute laatste of the below sensor.
template:
- trigger:
# - platform: time_pattern
# hours: 0
# minutes: 0
- platform: state
entity_id: sensor.date
- platform: homeassistant
event: start
- platform: event
event_type: event_template_reloaded
sensor:
- unique_id: marijn_verjaardag
state: >
{% from 'feestdata.jinja' import vj_marijn %}
{% set event = vj_marijn %}
{% set year = 1 if event.month < now().month or
(event.month == now().month and event.day < now().day)
else 0 %}
{% from 'easy_time.jinja' import count_the_days %}
{{count_the_days(event.replace(year=now().year + year))}}
picture: /local/family/marijn.jpg
unit_of_measurement: dagen
attributes:
id: Marijn
datum: !secret vj_marijn
type: Verjaardag
leeftijd: >
{% from 'feestdata.jinja' import vj_marijn %}
{% set event = vj_marijn %}
{% set year = 0 if event.month < now().month or
(event.month == now().month and event.day <= now().day)
else 1 %}
{{now().year - event.year - year}}
# {{(now() - event).days // 365.24}}
feest: >
{% from 'feestdata.jinja' import vj_marijn %}
{% set event = vj_marijn %}
{{now().strftime('%m-%d') == event.strftime('%m-%d')}}
laatste: >
{% from 'feestdata.jinja' import vj_marijn %}
{% set event = vj_marijn %}
{% set year = 0 if event.month < now().month or
(event.month == now().month and event.day < now().day)
else -1 %}
{% from 'easy_time.jinja' import count_the_days %}
{{count_the_days(event.replace(year=now().year + year))|int|abs}}
dagen: >
{% from 'feestdata.jinja' import vj_marijn %}
{% set event = vj_marijn %}
{{(now() - event).days}}