How can I use this icon attribute and place it anywhere on my button? A return statement for the icon itself does not seem to be supported, and if I use the following code then it just displays the name on my button → “icon: mdi:battery-80”
So far I have just seen solutions where this was manually build, with ten different if statements to pick the right battery icon. That is also how the guy from the example picture above did it. But if that logic is already build into home assistant, it would be nicer to just use that.
[[[
var i = states['sensor.fliphone_battery_level'].attributes.icon;
var b = states['sensor.fliphone_battery_level'].state;
return `<ha-icon icon='${i}' style='width: 18px; vertical-align: 3px'></ha-icon>${b}%`
]]]
Awesome, that works! Thanks for this, I was not smart enough for that (and still do not understand that fully, but the result is fine :-))
@devilclarke: Right, this guy had 10 if statements and selected a different battery icon for each. The above code prevents that and just uses the standard functionality.
The apple watch battery information from icloud3 does unfortunately so far not have a dynamic battery icon, so still need to improve that, but overall I am already quite happy with that.
I see that my HTML/CSS/Javascript knowledge is too low, I stopped actively using that when you were still building weg pages with HTML table structures
For sure, I could also only compile that with other different examples and the help here.
I am still working on further improvements, so this is not the end, but this achieves what you see in the last screenshot (yesterday I have added the distance and time to home to be only shown if above 0, those sensors require icloud3 to be available).
Thanks alot. I got most of it working. I’m also using the memoji avatar created with an iPhone but I saved it as a moving .gif I have different .gifs for different states/locations.
Sounds cool. Simply putting a gif instead of my png would also show that animated?
How did you create this gif and what is there animated? Recording the screen while using the face capture of the iOS emoji?
Yes, just a .gif instead of .png.
You can create the Memoji and hit record in the Messages app on iPhone X or later while doing facial expressions. I recorded a standard Memoji and one with working clothes on. Also two for my girlfriend. So whenever we’re at work it shows a different Memoji.
I recorded a video and used the Shortcuts app to convert this to a gif. When using this in Home Assistant, it indeed shows as animated gif, but with a black rectangle, no transparency around the head. Is that just not visible for you as you are using a dark theme, or is there a smart way to have a animated gif with transparent background? The initial video before conversion seems to be transparent.
Independently of animation I also like your idea to use different images dependent on the location.
Ok thanks, than it was not just me being to dumb.
The tool you mentioned did not work, as it also made the black in the eye transparent, but this one here successfully just removed the black around the head, looks cool!
I just wanted to thank you for posting your code. I had some custom:button-cards that show battery levels of a couple of my devices. I wanted to add the battery percentage shown in the corner of the card. I was a bit overwhelmed with trying to understand how to make it happen. I took your code, ripped out what I needed, and adapted it for my use. I even managed to incorporate a few things that you might find useful.
In the raw configuration editor, I have two templates (a general template that gets used on all my custom-buttons and another that is specific for the battery cards):
Using this method, I can easily replicate the card for use with different devices all while cutting down on the number of lines needed to make them work. The code also uses the entity defined within the card rather than hard-coding each one. Color of the icon and the battery percentage is handled with a variable that cuts down on duplicate lines of code. Also, if the battery percentage is 10% or lower, the icon will flash.
Again, that’s for sharing. This gives me a better understanding of what I can accomplish.