I’m so lost in where all the configurations should go…
I got a similar situation: I got a night-hook on the front-door → when it’s unhooked the read-contact is closed (off) but Lovelace treat this by default as closed and when you tell lovelace it’s either a door or a lock becomes locked/closed when the contact is closed (off)…
I did a lot of reading and searching the internet, but I don’t understand how this all is fitted into lovelace… I mean if I put the example from @matthijsberg into the developer → template window (for my own sensor) it does seems to work on the template-output, but I am absolutely clueless how to add it to the dashboard -
Handling this (on is off, and off is on) in node-red to activate a light with a certain color is not a problem. Just rewiring the logic.
So where and how do I make this work so lovelace sees a closed binary_sensor as “on” (with appropriate color - become yellow by default when open but should be red - in my case - and pref be green when closed)
Lot of questions - srry that I hijacked/piggy-bagged the thread but I did not really find an answer.
So yeah - where in hassio should this be placed to use on the lovelace dashboard?
- platform: template
sensors:
frontdoor_hook: # <-- does this become a new "virtual" sensor ??
# binary_sensor.openclose_64 is the actual name of the sensor
friendly_name: "front-door hook"
value_template: >-
{% if is_state("binary_sensor.openclose_64", "on") %}
hooked
{% else %}
unhooked
{% endif %}
icon_template: >-
{% if is_state("binary_sensor.openclose_64", "on") %}
mdi:door-closed
{% else %}
mdi:door
{% endif %}
It doesn’t tie into lovelace. You’re making a new sensor (not a binary_sensor). binary_sensors can only be on or off, becuase you want to use hooked/unhooked… you’ll need to create a sensor which accepts any state. Then you’ll use that new sensor in the interface instead of the previous binary_sensor.
Changing colors in dashboards requires custom solutions. It’s not something that native home assistant can do.
I had to install the file editor plugin for that… While searching I found that I could add an include file like so which makes a lot more sense now:
binary_sensor: !include binary_sensor.yaml
Then I added this yaml into it - some trial and error got involved since I’m (for now) not entirely sure what I’m doing but I got it working. My /config/binary_sensor.yaml has this content now and does what I want (except for the color but that’s for an other day):
- platform: template
sensors:
frontdoor_hook:
device_class: opening
friendly_name: "front-door hook"
value_template: >-
{% if is_state("binary_sensor.openclose_64", "on") %}
off
{% else %}
on
{% endif %}
icon_template: >-
{% if is_state("binary_sensor.openclose_64", "on") %}
mdi:lock-outline
{% else %}
mdi:lock-open-variant-outline
{% endif %}
And this is the card I added to lovelace where openclose_64 is the original sensor and frontdoor_hook the custom sensor:
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.