Is there a way to refer to an entity other than by name?

Hi, i’m trying to make my code as simple as posible and want to create a template for a custom button card… Is there a way to refer to an entity other than by name? is the entity name available to use as a variable or something generic like “entity_name” or something like that.

i have this code

    - opacity: |
       [[[  return states['light.tira_de_luz'].attributes.brightness / 255 ]]]

and looking for something like

   - opacity: |
      [[[  return states['entity_name'].attributes.brightness / 255 ]]]

thanks in advance

entity_id is unique within the system → therefore it is good to identify entities

entity name is not unique → you can have multiple entities with the same name → therefore I don’t think it would be a good way to identify an entity.

I don’t know if it is possible to extract entity id(s) from entity name.

2 Likes

Entity ID’s are in /config/.storage/core.config_entities

1 Like

Yes, there’s an entity object you can use, e.g. entity.entity_id or entity.state.

  • entity: The current entity object, if the entity is defined in the card

See the templating section.

1 Like

Try this , and read the Github-Documentation , it’s very helpful, beside the Search-box in the forum :wink:

I suppose you are being ironic and you notice that the message you repost is mine…that code do not work in my new dashboard that’s the reason I’m asking. I already search before asking. Sorry for asking and bother you….

If this code don’t work it might be because you have specified your(which) “entity” in your card, as Pieter showed/told you

You have to show your full code, if people should have a fair chance to understand what you are doing

Above code ( Yours i Edited ) , works as that is how it could be … i have tons of buttons cards with exact template

type: custom:button-card
entity: YOUR.entity_so the rest of your card knows which entity your referring to !

        [[[
          return `<span>${entity.attributes.brightness / 255}</span>`
        ]]]

No im not being ironic, i sincerely think people should use the search function, After they read the documentation, so the Forum dont get drowned for every little thoughts people have on their mind, and find it “easier” to just open a topic

And I Assume you are aware that you are dividing the brightness level output-number with 255 !

Yes, I’m aware thank you. As you have a lot of card with this same code you should know why. If not use the search button or read the documentation…

Not with your specific code or your entities obviously , But as Pieter clearly showed , i gave you a working example, However i removed the / 255 ( Shouldn’t make a difference) but i found the “result” awful meaningless

Please mark Pieter’s answer as solution, as you(and others) there will find another example of showing brightness , and its a good start to get familiar with the documentation, as it can help to understand the examples your can find afterwords in this forum.
There is a huge Topic(several) specific for this Card, where you can find lots of tips N solutions

1 Like

Do not work with the opacity style, at least in my code. Your exemple on the label works but not in the opacity style, my full working code is the following. if i replace the opacity part with:

  • opacity: |
    [[[ return ${entity.attributes.brightness / 255} ]]]

dont work

          - type: custom:button-card
            action: none
            entity: light.tira_de_luz
            show_icon: false
            show_name: false
            hold_action:
              action: none
            entity_picture: /local/CasaHA/transparente.png
            state:
              - value: 'on'
                entity_picture: /local/CasaHA/casadormitorioon.png
              - value: 'off'
                entity_picture: /local/CasaHA/transparente.png
            show_entity_picture: true
            tap_action:
              action: none
            style:
              width: 100%
              height: 100%
              left: 50%
              top: 50%
              mix-blend-mode: lighten
            styles:
              entity_picture:
                - width: 100%
                - height: 100%
                - object-fit: contain
                - opacity: >
                    [[[  return states['light.tira_de_luz'].attributes.brightness /
                    255 ]]]
              card:
                - padding: 0
                - background: none

right , i would have been surprised if it did

try with card_mod

or use - filter

or apply it to the img_cell:

Your question is basically answered, and “as simple as possible” is how your working solution already is.
There are examples in the code, where a reference to “main entity” don’t work, i think it’s mentioned somewhere, in either the Docs, or in 1 of the major topics for Custom:Button-card

Apparently we’re back to square 1

type: custom:button-card
entity: light.driver_hallway
show_icon: false
show_entity_picture: true
entity_picture: /local/weather-icons/shadows/rainy.svg
styles:
  entity_picture:
    - opacity: |
        [[[
          return `${entity.attributes.brightness / 255}`
        ]]]

The right brace is misplaced and the reference must be in backticks.

[[[ return `${entity.attributes.brightness} / 255` ]]]
1 Like