Help needed setting up 2 templates

I would like to use the template feature to change the way the below entity values are displayed my cards.


Example 1:

If I display the value for entity: sensor.sm_a505u_battery_level in a card, it will display as:

SM-N960U Geocoded Location       1234 Huntington Way, Bumington, WA 12345, USA

I would like to change it to say:

Joseph       1234 Huntington Way, Bumington, WA 12345, USA

…also, how do I override the respective icon for this entity? I tried specifying “- icon: mdi:xxx” in the card code, however that obviously wasn’t right.


Example 2:

If I display the value for entity: binary_sensor.ecolink_door_window_sensor_sensor in a card, it will display as:

On

I would like to change it to say:

Open

…and, if it displays, Off to change it to display Closed, respectively.

I was able to make a template for this, however, it’s still not right. I don’t get any errors, however, the value for sliding_glass_door always shows as off in lower case letters.

binary_sensor:
  - platform: template
    sensors:
      sliding_glass_door:
        value_template: "{%if states.binary_sensor.ecolink_door_window_sensor_sensor.state == 'On' %}Open{%elif states.binary_sensor.ecolink_door_window_sensor_sensor.state == 'Off' %}Closed{% endif %}"
        friendly_name: 'Sliding Glass Door'

I tried using the template below; however, that resulted in my whole Home Assistant going into Safe Mode complaining about “mapping values are not allowed here”

  - platform: template
      sensors:
      sliding_glass_door:
          value_template: '{% if states.binary_sensor.ecolink_door_window_sensor_sensor %}
          {% if states.binary_sensor.ecolink_door_window_sensor_sensor.state == "on" %}
              Open
          {% else %}
              Closed
          {% endif %}
          {% else %}
          n/a
          {% endif %}'
          friendly_name: 'Sliding Glass Door'

I really hope this isn’t too much to ask for someone to help out. But, I REALLY tried before posting. I would really appreciate the help to get two working templates.

Note you can only do this in the sensor definition, not the card, unless you have the custom card template installed:

"{{ states('sensor.sm_a505u_battery_level')|replace('SM-N960U Geocoded Location', 'Joseph') }}"

The device class ‘door’ does what you want and comes with fancy changing icons;

binary_sensor:
  - platform: template
    sensors:
      sliding_glass_door:
        value_template: "{{ is_state('binary_sensor.ecolink_door_window_sensor_sensor', 'on') }}"
        friendly_name: 'Sliding Glass Door'
        device_class: 'door'

The other device classes are listed in the binary sensor documents.

1 Like

Thank you so much Tom. device class door really made it easy for me. This example will let me explore other device classes too.

I also was able to use the other example you gave me for replacing the name. I will explore using lovelace-card-templater once I get the hang of using more templates.

BTW: I just want to say, its people like you that help novices like me to become future experts in Home Assistant. I’m hoping one day I’ll be able to contribute back to the community by offering to help others.

What errors? Be specific.

Always test templates in the developer tools template editor first.

And always perform a configuration check before restarting. If it does not pass fix the problem and test again. Don’t restart until the configuraion check passes. That way you wont get ‘safe mode’.

In the developer tools template editor what do these return:

 {{ states('sensor.sm_a505u_geocoded_location') }}
 {{ states('sensor.sm_a505u_geocoded_location')|replace('SM-A505U Geocoded Location', 'Joseph') }}

Sorry Tom, I edited my post too late (right before your last comment). I figured it out. I was using a quote in the friendly name. I had to escape the single quote:

friendly_name: 'Joseph''s Location'