Customize Sensor

I’m a little, well, lost here.

I’m trying to do two things. 1) rename the display of the binary sensor for my roomba from “on” and “off” to “Full” and “Empty”, and 2) customize the icon based on that state.

From the documentation, it seems like this could work, but there aren’t any specific examples that call it out:

template:
  sensor:
    - name: "Roomba"
      state: >
        {% if is_state('binary_sensor.roomba_bin_full', 'on') %}
          Full
        {% else %}
          Empty
        {% endif %}
      icon: >
        {% if is_state('binary_sensor.roomba_bin_full', 'on') %}
          mdi:delete-variant
        {% else %}
          mdi:delete_empty
        {% endif %}

Trying that, the display in lovelace seems to not understand:
image

A lot of posts talk about doing something like

- platform: template
  sensors:
    garage_door:
      etc.

Using value_template and icon_template… But, that is a deprecated way of doing things, correct?

Officially it’s a “legacy” method. Still works, but presumably it will be depreciated at some point. Possibly not, though, because lots of people still use it.

1 Like

Try this version (note the hyphenation of sensor; the shortened template is immaterial).

template:
  - sensor:
      - name: "Roomba"
        state: "{{ 'Full' if is_state('binary_sensor.roomba_bin_full', 'on') else 'Empty' }}"
        icon: "{{ 'mdi:delete-variant' if is_state('binary_sensor.roomba_bin_full', 'on') else 'mdi:delete_empty' }}"
1 Like

Almost there! The only thing now is I’d like to have a friendly that won’t overlap with future Roomba’s, and within lovelace get it to say something like “Bin Full?”

I’ve tried a few different things with the unique_id field, but to no avail. Here’s where i’m stuck:

template:
  - sensor:
      - unique_id: "living_room_roomba_bin"
        name: "Bin Full?"
        state: "{{ 'Full' if is_state('binary_sensor.roomba_bin_full', 'on') else 'Empty' }}"
        icon: "{{ 'mdi:delete-variant' if is_state('binary_sensor.roomba_bin_full', 'on') else 'mdi:delete_empty' }}"

Also, perhaps it’s a separate thread, but there is no way to do this as a “binary_sensor” so that I can have lovelace add color when it’s full, is there? From what I read, I’d want to approach it with a device class but unfortunately there’s no way to create a custom device class for a trash bin, correct?

When defining a Template Sensor, its name is its “friendly_name” and it will also be used to create its object_id (that’s the right half of its entity_id).

If you redefine it as a binary_sensor its state values become on/off and cannot be custom like Full/Empty. To make a binary_sensor display different state values in the UI, you would select an appropriate device_class but there’s none for Full/Empty.

That’s what I thought. Thanks!

In the future, it would be great to be able to define some custom device_classes :slight_smile: !

1 Like

If you’re interested, there are several Feature Requests for new device classes including one asking for exactly what you suggested, the ability to define custom device classes.

2 Likes