As the title says. I have an input select with 3 options and I would like the input_select’s icon to change depending on the option chosen. This doesn’t appear possible as I can’t define a template for the icon even if i edit the input_select in yaml directly.
Is there any way around this?
Within customize.yaml you can do the following:
input_select.test_input_select:
templates:
icon: >
if (state == 'OptionA') return 'mdi:ab-testing';
if (state == 'OptionB') return 'mdi:account-cowboy-hat';
return 'mdi:alert-circle-outline';
That only works if you use CustomUI and does not work without CustomUI.
You can create a template select entity instead of an input select entity. That supports an icon template.
Awesome thank you, that was exactly what I needed. I must have read the templating page in the documentation 100 times this year but somehow missed the template select every time.
TBH, I think template selects aren’t documented in a great place in relation to other template entities (everything else has it’s own separate page). So, I’m not surprised you missed it.
Yeah you’re right. I was thinking even just putting that list of template entities that share the same page (Sensors, binary (on/off) sensors, numbers and selects) in a bullet list like the list below it that leads to the other entities’ separate pages would make it a lot easier to see. I’ll go ahead and suggest that edit on the documentation page.
What is a ‘template select entity’, and where and how to I define one, and is there documentation for it?
Simply put, a template select is an input select (drop down) that can use templates for its state, action to take when you change the dropdown value, and other fields.
You define it in your configuration.yaml. Basic documentation can be found here. Note that you should read the stuff higher up on the page too as it explains how it all works and where exactly you define it.
I find template sensors are hugely powerful and I can often replace multiple automations with a single template sensor. It can be a bit complicated to wrap your head around though especially since the documentation can be a bit sparse. So here’s how I implemented my drop down with a changing template depending on state (why I started this thread).
This dropdown lets me change my mobile phone do not disturb mode using using notification commands on the mobile companion app (I believe this only works with Android.)
It works in both ways - if i cange dnd mode on my phone, the drop down updates itself (the power of templates!). And because this is now a sensor with a state that I can change (as opposed to the read only sensor the mobile app exposes), I can add this to my scenes, and so I can automatically put my phone on do not disturb when I activate a specific scene (I use it for meditating).
This code goes inside your configuration.yaml. Note that you can only have one main template:
entry, but as many sensors under that as you want
template:
- select:
- name: "Mobile Phone Do Not Disturb Mode"
unique_id: mobilephonedndmode
state: "{{ states('sensor.mobile_phone_do_not_disturb_sensor').replace('_',' ').title() }}"
options: "{{ ['Off','Priority Only','Alarms Only','Total Silence'] }}"
icon: >-
{% if is_state('select.mobile_phone_do_not_disturb_mode', 'Off') %}
mdi:bell-ring
{% elif is_state('select.mobile_phone_do_not_disturb_mode', 'Priority Only') %}
mdi:bell-alert
{% elif is_state('select.mobile_phone_do_not_disturb_mode', 'Alarms Only') %}
mdi:bell-sleep
{% elif is_state('select.mobile_phone_do_not_disturb_mode', 'Total Silence') %}
mdi:bell-off
{% endif %}
select_option:
service: notify.mobile_app_mobile_phone
data:
message: command_dnd
title: '{{option.replace(" ", "_").lower()}}'
Is this still the best way to do this 2.5years later? I know there has been a bunch of work on the UI in this time!
Looking to do the same thing and would like to have a different icon (and ideally an image) for different options in the dropdown?