Ability to create a template sensor and define both state and attributes together?

Hi team,

This is one of those questions that as soon as I ask it I’m sure the obvious answer will present itself to me and I’ll feel embarrassed to have asked it in the first place.

But I’m curious how others would solve this, so I’m going to ask it anyways, since I really enjoy learning from others.

To keep it brief, I’ll try to give a TL;DR:

  • I use Mushroom cards and have created a fairly full-fledged mobile dashboard using them
  • Each standard mushroom card contains support for: Title, Secondary Text, Icon, Badge Icon, and Badge Icon Color.

Here’s a screenshot of my main dashboard:

I’ve developed a pattern that I use, for example the “zzz” at the main status bar indicates our toddler is napping, and the “secondary text” of his room (lower right) contains how long he has been napping for.

Similarly, the secondary text on the Kitchen indicates when the dishwasher will finish, the backyard shows us how much the solar panels are producing, etc etc.

Clicking on any room takes you to that room’s details:

Now, when something is awry, I might have a badge icon and associated color (red for example) on that room.

Here is an example I created, where the cat litter box (located in Serena’s office) is rotating, Ethan’s space heater is heating, and one of the garage dehumidifiers is full and must be emptied:

So, all of this is controlled via template sensors, which are loaded up on the dashboard. This helps separate the presentation logic from the actual logic, so that for example the room’s current icon can be used elsewhere (not hard coded on the dashboard itself).

Here is an example, for the backyard:

Now, the problem is, you can already see the “if” condition for the power being produced < or > 0 is duplicated - one for each template sensor - since it might affect both the icon and the secondary text (which is true in this example).

Ideally, I can have a single template sensor for “backyard”, and have attributes for the badge icon, color, etc.

But, the way a template sensor is defined, I have to have an “if” statement for each attribute and the state itself - so, still have the duplication.

The only thing I can think of is to emit JSON from a sensor, and have JSON keys for badgeIcon, badgeColor, secondaryText, etc etc. That way I can not duplicate the “if” statements.

I hope this is making some/any sense.

Again, I’m sure there’s a much better way to think about this that I am missing!

Cheers
Matt

You can do this using attribute_templates. See discussion here: https://community.home-assistant.io/t/how-to-make-a-platform-template-entity-with-attributes/323890
But I’m afraid this will not reduce duplicates a lot since you still need them to define the state of the attributes.
If the template you would like to re-use is basically a copy then can use yaml anchors to reduce the need for duplicates. Search the internet for “yaml anchors” and you will find more details on how to use those.

1 Like

You can make “UI info” entities that are sensors.

e.g.

- name: Backyard Dashboard Info
  state: >
    {{ something that matters to the overall dashboard }}
  attributes:
    info: >
      {% if states('....') %}
        {{ dict(
          primary=...,
          secondary=...,
        ) }}
      {% else %}
        {{ dict(
          primary=...,
          secondary=...,
        ) }}
      {% endif %}

Then in your template cards…

primary: "{{ state_attr('sensor.backyard_dashboard_info','info').primary }}"

Next time, if you want people to use real examples with your code, post the code. Not a picture of the code.

1 Like

You can return dictionaries from any single attribute, you cannot template the whole attributes field. So this can be done if you return a dictionary from a single attribute like shown in my previous post.

1 Like

@starob thank you kindly for your reply. I appreciate it.

@petro thank you sir as well for your reply. I really appreciate it as well. I am so sorry, that was so stupid of me to put the picture. Next time I’ll copy the code and put it there so you can copy/modify/paste it. Thank you very much for your time.

I will try out the dictionary approach to see how I like it.

Again, thank you!

Matt

Worked well. Thank you kindly @petro:

    - name: dashboard_matts_office_badge
      state: 0
      attributes:
        info: >
          {% set printer_status_color = states('sensor.printer_status_color') %}

          {% if printer_status_color != "green" %}
            {{ dict(
              badgeIcon = 'mdi:printer-alert',
              badgeColor = printer_status_color,
            ) }}
          {% elif is_state('binary_sensor.matts_office_motion_occupancy', 'on') %}
            {{ dict(
              badgeIcon = 'mdi:motion-sensor',
              badgeColor = 'blue',
            ) }}
          {% endif %}
1 Like