royto / logbook-card

Logbook card for Home Assistant UI Lovelace
GNU General Public License v3.0
192 stars 9 forks source link

Custom icon based on attibute value #102

Closed PavelD closed 6 months ago

PavelD commented 7 months ago

Hello,

I have sensor updated with new video / movie title played on my projector. The attribute I used for the application (YouTube, YouTube Kids, Kodi, ...). The card is great but I'm not able to change icon based on the attribute value.

It will be great to be able to set icon (+ icon color) based on the attribute value.

kosmaluk commented 7 months ago

I also use this card and I am delighted with it. I can finally view the attribute history of my selected entities. What I'm missing is mapping the icon and icon color to the attribute state, so I'm also asking for such a function.

royto commented 7 months ago

Hello,

Home assistant saves history on state change but not on attributes changes.

So if only the attribute change, there is no history on the changes recorded. So, to me, the value of the attribute could be wrong ...

What is your use case ?

kosmaluk commented 7 months ago

Hi. HA records how an entity's state and attributes change with it. I see these attributes in history. Your tab allows you to change the icon and icon color by changing the state of the entity, and I have multiple entities whose state is heterogeneous and whose attribute is constant, and I would like to change the icon based on the value of that attribute. E.g: entity status: Love in Paradise (song title) attribute1 - Title: my songs attribute2 - Artist: Happiness attribute3 - Apps: spotify.music

I want to set a different icon for all items with an attribute value of Apps=spotify.music

You already have something similar and it works with the option: hidden_state: attribute: name: Applications value: spotify.music I'm trying to achieve this via card-mod but I can't figure out how to do it. the color of all entities changes See forum post: https://community.home-assistant.io/t/card-mod-add-css-styles-to-any-lovelace-card/120744/5993

śr., 7 lut 2024, 20:51 użytkownik Julien Roy @.***> napisał:

Hello,

Home assistant saves history on state change but not on attributes changes https://community.home-assistant.io/t/how-to-access-the-attribute-of-an-entity/205222/8 .

So if only the attribute change, there is no history on the changes recorded. So, to me, the value of the attribute could be wrong ...

What is your use case ?

— Reply to this email directly, view it on GitHub https://github.com/royto/logbook-card/issues/102#issuecomment-1932759254, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6D7UIWPNAR23Y6UAVGRCCTYSPLK7AVCNFSM6AAAAABC34RUE6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMZSG42TSMRVGQ . You are receiving this because you commented.Message ID: @.***>

royto commented 7 months ago

ok. i see. what is the integration you are using ?

kosmaluk commented 7 months ago

I have HAOS, Core 2024.1.5, OS 11.4, Supervisor 2024.01.1 card-mod 3.4 everything latest Your custom:logbook-card latest 2.3.0

śr., 7 lut 2024, 21:37 użytkownik Julien Roy @.***> napisał:

ok. i see. what is the integration you are using ?

— Reply to this email directly, view it on GitHub https://github.com/royto/logbook-card/issues/102#issuecomment-1932827790, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6D7UIUMCMIS6S4MMXQ6HBDYSPQYJAVCNFSM6AAAAABC34RUE6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMZSHAZDONZZGA . You are receiving this because you commented.Message ID: @.***>

royto commented 7 months ago

I was talking about integration

kosmaluk commented 7 months ago

Hi. I don't understand what integration you mean? I have a lot of integration.

In your cards I have entities from various integrations, mainly mobile.app (sensors), and template_sensor if that's what you mean, if not please explain.

czw., 8 lut 2024, 09:13 użytkownik Julien Roy @.***> napisał:

I was talking about integration https://www.home-assistant.io/integrations/

— Reply to this email directly, view it on GitHub https://github.com/royto/logbook-card/issues/102#issuecomment-1933560528, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6D7UIW5KUY3PHTUJ7ICVR3YSSCLJAVCNFSM6AAAAABC34RUE6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMZTGU3DANJSHA . You are receiving this because you commented.Message ID: @.***>

royto commented 7 months ago

my point is that if the sensor is a template sensor, you can change the icon directly in the template depending on the attribute value

kosmaluk commented 7 months ago

yes i know. I have itbut the color of the icon can not be set in this template sensor. That's why I want to do it in the card.You do this via state_map, but you map the icon and icon color to the entity state, not the attribute.Maybe you can write what elements and how you change them, maybe it will help me use it in card-mod

kosmaluk commented 6 months ago

ok, thanks for the functions, it works great, but I have a problem with specifying a wildcard for a multi-level state. Maybe you know how to do it?

my cod:

type: custom:logbook-card
show:
  end_date: false
  start_date: true
  separator: true
  duration: true
separator_style:
  color: '#3D3D3B'
entity:
  - sensor.notify_last_redmi_all_attr
title: ''
state_map:
  - value: '*'
    icon_color: '#1E90FF'
    attributes:
      - name: Apps
        value: com.miui.misound
  - value: '?'
    icon_color: '#094689'
    attributes:
      - name: Apps
        value: pl.pkobp.iko
  - value: '*'
    icon_color: green
    attributes:
      - name: Apps
        value: com.spotify.music

cards pl.pkobp.iko - silver bank is not working: IMG_20240318_161115

royto commented 6 months ago

is it because your are using - value: '?' ? Can you try with - value: '*'

kosmaluk commented 6 months ago

I tried '*' and it doesn't work. these are multi-line messages from electronic banking. The items directly pl.pkobp.iko and android.messenging do not work IMG_20240318_174837

royto commented 6 months ago

can you try with

  - icon_color: '#094689'
    attributes:
      - name: Apps
        value: pl.pkobp.iko
kosmaluk commented 6 months ago

this doesn't work either

royto commented 6 months ago

Can you give me the response of the call of the history for your entity. Go to the developer console (F12), in the network tab, the response of the call to the history API. The URL should looks something like this https://YOUR_HA_URL/api/history/period/2023-10-25T20:29:25.120Z?filter_entity_id=sensor.notify_last_redmi_all_attr

Only one response is needed

kosmaluk commented 6 months ago

sorry but I don't know how to response it (APi history). I go to the console and the network tab and I don't know what to do

royto commented 6 months ago

Open the dev tools (F12)

  1. Go to network
  2. find the request
  3. Go to Response tab
  4. Copy the response and paste it here

image

kosmaluk commented 6 months ago

{ "entity_id": "sensor.notify_last_redmi_all_attr", "state": "Autoryzacja transakcji kartowej\nKonto 50..0022\nKarta 3585\nKwota 70.40 PLN\nDINO NAMYSLOW 2 K.1\nNAMYSLOW\nDostepne 1682.59 PLN\n2024-03-18 13:24", "attributes": { "Tytul": "Inteligo", "Tresc": "Autoryzacja transakcji kartowej\nKonto 50..0022\nKarta 3585\nKwota 70.40 PLN\nDINO NAMYSLOW 2 K.1\nNAMYSLOW\nDostepne 1682.59 PLN\n2024-03-18 13:24", "Tresc2": "null", "Apps": "com.google.android.apps.messaging", "TextLines": null, "Messages": "Inteligo, Autoryzacja transakcji kartowej\\nKonto 50..0022\\nKarta 3585\\nKwota 70.40 PLN\\nDINO NAMYSLOW 2 K.1\\nNAMYSLOW\\nDostepne 1682.59 PLN\\n2024-03-18 13:24\\n", "icon": "mdi:message", "friendly_name": "Notify last redmi all attr" },

kosmaluk commented 6 months ago

IMG20240318212918

kosmaluk commented 6 months ago

IMG20240318213212

royto commented 6 months ago

it is linked with the use of \n in the state causing the matching to fail. Is it something that you can change ?

kosmaluk commented 6 months ago

I have two sensors, one original is "last notification" from mobile_app integration (Android os) and the other is a template sensor whose state is the state of the first one. The system formats this sensor state itself, although I could try to convert the template sensor state into one line (without \n). With this knowledge, I can also try to search for wilcard for the expression with \n from Gpt chat. Alternatively, consider whether you can make this function so that the entity state is not required (only the attribute state is required).

kosmaluk commented 5 months ago

Ok, I've finished the tests and here it is:

  1. There is no wildcard for multi-level entity states. Other expressions and filters for this state also do not work.
  2. I converted the original sensor state to sensor template remove \n and your function worked.

Since I mainly use logbook with this template sensor, this solution finally suits me.

Thanks for the help.

I also remind myself in my other new topic regarding the new version 2.5.1 where my cards are empty :)

this is card after removed \n

IMG_20240320_132449

PavelD commented 5 months ago

Thanks a lot! It works as expected!

ildar170975 commented 1 month ago

So if only the attribute change, there is no history on the changes recorded

This is not correct. On every attribute change a new row is added into states table with an updated last_updated value. The states table may contain rows with SAME state & different data in attributes_id column; the attributes_id points to a record in states_attributes table.