It depends a bit on your use case. If you want to give a sensor 3 states it might need to be a different sensor type (typically those sensors are binary sensors that can only have state on/off). So if you want to have > 2 states you need a different sensor type thus a new one.
I know we could go down a rabbit hole with this one. But the state the device reports may only be 2 fundamentally. But the system interface will then have a number of others including unavailable which needs to be taken care of in automations / blueprints. Additionally there is also the ability to build using other HA sensors such as Template, Bayesian, Threshold, Trend add with the ability to add command line and formulas to the math. My point is that even though the devices may only report the basic 2 Binary states there is the ability with Zigbee and Zwave to drill down to the actual device interfaces and obtain other data, as well as apply other math to the change states.
I am glad you got the custom value and icon template working for binary sensors. From what I understand one creates a new/dummy sensor that âinheritsâ certain values from your main sensor and those values such as color, icon, state value output can be amended through the template.
What I struggle with is the actual hierarchy/structure of how to put. this into the customisation.yaml file.
Here is my top line file including the part with your code.
Thanks for your help, much appreciated. I must say I struggle a bit (new top HA) when combining information from the main documentation then mixed with community suggestions that (here and there) date back a few years. Obviously schemas and concepts have changed/improved over the years and it was good to now understand how templates in relationship to sensors work.
Here is the code that now works - creating two sensors (not to be mixed up with binary_sensors that are the triggers) that show correct state value and icon. The code is in configuration.yaml and not nested within the homeassistant: object.
template:
- sensor:
#Main Garage Door
- name: "Status Main Garage Door"
state: >
{% if is_state("binary_sensor.status_garage_roll_gate", "on") %}
closed
{% else %}
open
{% endif %}
icon: >
{% if is_state("binary_sensor.status_garage_roll_gate", "on") %}
mdi:garage-variant-lock
{% else %}
mdi:garage-open-variant
{% endif %}
#Main Entrance Gate
- name: "Status Main Entrance Gate"
state: >
{% if is_state("binary_sensor. status_main_entrance_gate", "on") %}
closed
{% else %}
open
{% endif %}
icon: >
{% if is_state("binary_sensor. status_main_entrance_gate", "on") %}
mdi:gate
{% else %}
mdi:gate-arrow-right
{% endif %}
homeassistant:
customize:
binary_sensor.status_ufh_circulation_pump:
...
...
...
...
pack_security:
knx:
binary_sensor:
- name: "Status Main Entrance Gate"
# Closed = 1, Open = 0
state_address: "9/7/3"
- name: "Status Garage Roll Gate"
# Closed = 1, Open = 0
state_address: "9/7/6"
button:
- name: "Button Main Gate"
address: "9/2/0"
- name: "Button Main Garage"
address: "9/2/1"
- name: "Button Guest House Gate"
address: "9/2/2"
This is very similar to my code for my garage doors, but I also changed the colors. Iâm using custom-ui in HACS and added this to my customize.yaml file
cover.2_car_garage_door_opener:
friendly_name: '2-Car Garage Door Opener'
templates:
icon: >
if (state == 'open') return 'mdi:garage-open-variant';
if (state == 'closed') return 'mdi:garage-variant';
return 'mdi:garage-alert-variant';
icon_color: >
if (state == 'open') return 'red';
if (state == 'closed') return 'green';
return 'yellow';
cover.1_car_garage_door_opener:
friendly_name: '1-Car Garage Door Opener'
templates:
icon: >
if (state == 'open') return 'mdi:garage-open';
if (state == 'closed') return 'mdi:garage';
return 'mdi:garage-alert';
icon_color: >
if (state == 'open') return 'red';
if (state == 'closed') return 'green';
return 'yellow';
Hi,
I am trying similar thing but I donât have lovelace installed.
I am trying to change state of my wifi icon , based on the signal strength of the wifi_signal.
Hi, addind to the above, seeing a post i have modifief the yaml file of my esp32 to following, but the icon is not changing dynamically, it changes only once.
Can any body guide , what is the problem in my yaml code.