Need help with a template.
Here is an auto-entities
list of "device_tracker"
entities with "card_mod"
:
- type: custom:auto-entities
card:
type: entities
title: fixed color
filter:
template: |
{% for DEVICE in states.device_tracker -%}
{%- if is_state_attr(DEVICE.entity_id,"source_type","router") -%}
{{
{
'entity': DEVICE.entity_id,
'card_mod':
{
'style':
':host {
--paper-item-icon-color: red;
}'
}
}
}},
{%- endif -%}
{%- endfor %}
sort:
count: 7
Now letās try to create a conditional color, something like this:
:host {
--paper-item-icon-color:
{% if ... %}
brown;
{% elif ... %}
red;
{% else %}
green;
{% endif %}
}
This "if ... else ..."
code cannot be used inside a "{{ ... }}"
template.
So letās define the color outside the "{{ ... }}"
template.
First, letās use some fixed color defined by using "set"
.
- type: custom:auto-entities
card:
type: entities
title: conditional color
filter:
template: |
{% for DEVICE in states.device_tracker -%}
{%- set COLOR = 'red' -%}
{%- if is_state_attr(DEVICE.entity_id,"source_type","router") -%}
{{
{
'entity': DEVICE.entity_id,
'card_mod':
{
'style':
':host {
--paper-item-icon-color: COLOR;
}'
}
}
}},
{%- endif -%}
{%- endfor %}
sort:
count: 7
Does not work. Letās check in in the āTemplatesā window:
{'entity': 'device_tracker.00_17_5a_23_94_49', 'card_mod': {'style': ':host {\n --paper-item-icon-color: COLOR;\n }'}},
......
Seems that the "COLOR"
variable is not recognized as a variable - it is recognized as a string āCOLORā - probably because of using '
.
The question - how to pass this "COLOR"
value inside the template as a variable?
This code works - for the "name"
property (here I do not need to use the '
):
{% for DEVICE in states.device_tracker -%}
{%- set COLOR = 'red' -%}
{%- if is_state_attr(DEVICE.entity_id,"source_type","router") -%}
{{
{
'entity': DEVICE.entity_id,
'name': COLOR
}
}},
{%- endif -%}
{%- endfor %}
Also tried this, gives an error:
{% for DEVICE in states.device_tracker -%}
{%- set COLOR = 'red' -%}
{%- if is_state_attr(DEVICE.entity_id,"source_type","router") -%}
{{
{
'entity': DEVICE.entity_id,
'card_mod':
{
'style':
':host {
--paper-item-icon-color: '}} {{COLOR}} {{';
}'
}
}
}},
{%- endif -%}
{%- endfor %}
Updated: SOLVED!!!
'card_mod':
{
'style':
':host {
--paper-item-icon-color:' + COLOR + ';
}'
}