Entities, templates, JINJA, Inconsistencies

First off, my apologies for the upcoming written diarrhea that I am about to spew on this post! It’s difficult to gather my thoughts into a cohesive post at the moment as my brain is fried!

I’m already bald, but this is still making me want to pull my hair out!

I’ve watched a ton of YT vids on creating templates. But I find that every single one of them only focuses on the developer tools/template page. None of them actually go to a dashboard and demonstrate clearly how that new ‘whatever’, is used on a card.

My current headache is this

I have door contact sensors. The default presentation of that entity is a blue icon if it’s closed and a greyed-out icon if it’s open. I want to show the icon in ‘green’ if the door is closed and ‘red’ if the door is open. But you can’t put that on the entity of a card. You have to create a special-purpose sensor to show these specific state values.

If you look at the log of the contact sensor, it shows ‘open’ or ‘closed.’ And if you look at the sensor state, it’s either ‘ON’ or ‘OFF.’ So which is it? Are the two values ON and OFF or OPEN and CLOSED?!?!?! There’s consistency for ya!!

Why are there no simple settings in the card builder UI that let you define the icon color based on an entity value? Why are we forced to always go to code to set these things?!?!?!

And then I’ll have to create one for each and every door that I want to display this way. That’s creating a special entity to reference an already existing entity just to customize what is displayed. That is stupid and goes against all advice for code reuse and maintainability.

You can’t put this code on a card. It will error out.

type: custom:mushroom-entity-card
name: Back Door
layout: vertical
fill_container: true
tap_action:
  action: none
hold_action:
  action: none
double_tap_action:
  action: none
entity: binary_sensor.back_door_contact_contact
secondary_info: state 
icon_color: >-
  {% if is_state("binary_sensor.back_door_contact_contact", "on") %}
    Green
  {% else %}
    Red
  {% endif %}

I fully believe that the adoption of HA would be ten times what it is already if the UI was just built in a more accessible way for those that are not full-time developers.

If anyone can recommend any course, video blog whatever that teaches this stuff effectively and clearly. Please send it on over!

The actual available states of any binary_sensor are ‘on’ or ‘off’. Binary sensors with a defined device_class also have “prettier” front-end representations like “Open” and “Closed”.

You can’t put any random configuration on just any card… you’ve chosen the wrong card type if you want to make custom changes using templates. You need to use the Mushroom Template Card. If you read Mushroom’s docs you will find that it has built-in variables like entity that make it very easy to duplicate a card configuration and only need to change a couple fields like the primary information/name and the entity.

type: custom:mushroom-template-card
layout: vertical
fill_container: true
tap_action:
  action: none
hold_action:
  action: none
double_tap_action:
  action: none
entity: binary_sensor.sonoff_ds01_02_b_door
icon: mdi:door
icon_color: |-
  {% if is_state(entity, "on") %}
    green
  {% else %}
    red
  {% endif %}
primary: Back Door
secondary: '{{states(entity)}}'

Tile and Entity cards allow minor customization of icon color using only the UI. For binary sensors the icon will be the theme default when the sensor is off and the chosen color when it is on. Entities cards have a similar option to have colored icons, but it follows your theme’s state color configuration, so you could change it if you wanted to, though not solely in the UI.

You have chosen a custom card and you have chosen to do further customization beyond the basic options of that…

None of the core cards except Markdown even allow templating, so you aren’t being forced to code anything. Until relatively recently, templates weren’t really useful for cards. This is likely part of the reason why there are fewer video tutorials on the subject.

1 Like