#Custom button card styling (border with SVG)
1 messages ยท Page 1 of 1 (latest)
lets use this thread ๐
so. why not use two path? and create 1 SVG for the background and 1 SVG for the colored border
so basically add a #background-path {
stroke: grey;
stroke-width: ${bar_thickness};
}
and use the "remaining" path of the earler created one?
<svg id="progress-bar" fill="none" stroke-linecap="round" viewBox="0 0 100 100">
<path id="background-path" d="M50,0.2
h0 a50,50 0 0 1 50,50 v0 a50,50 0 0 1 -50,50
h0 a50,50 0 0 1 -50,-50 v0 a50,50 0 0 1 50,-50" />
<path id="progress-path" d="M50,0.2
h0 a50,50 0 0 1 50,50 v0 a50,50 0 0 1 -50,50
h0 a50,50 0 0 1 -50,-50 v0 a50,50 0 0 1 50,-50" />
</svg>
<style>
#background-path {
stroke: grey;
stroke-width: ${bar_thickness};
}
#progress-path {
stroke: ${color};
stroke-width: ${bar_thickness};
stroke-dasharray: ${totalLength};
stroke-dashoffset: ${progress};
transition: stroke-dashoffset 1s linear;
}
</style>
I was initialy thinking about that but the box-shadow seemed like a good fit. Might run into an issue with the z-index depending on how they're both drawing. (I assume if gray is drawn first then it will be on the bottom.)
Seems like that was the way to go.
it worked, just copy pasted the SVG (the same) And didnt give it a path.. just let it act as a background
the problem now is this:
its not round anymore hehe
I think it is because totalLength has to change because now there is an additional path that it has to draw.
I do get it to 100% nicely, but the border is not have rounded edges,, its to "thick" for its own good it seems like
I think my card styling is the issue..
it was the stroke-linecap tricking my brain
I literally just figured that out...
<svg id="progress-bar" fill="none" viewBox="0 0 100 100">
<path id="background-path" d="M50,0.2
h0 a50,50 0 0 1 50,50 v0 a50,50 0 0 1 -50,50
h0 a50,50 0 0 1 -50,-50 v0 a50,50 0 0 1 50,-50" />
<path id="progress-path" d="M50,0.2
h0 a50,50 0 0 1 50,50 v0 a50,50 0 0 1 -50,50
h0 a50,50 0 0 1 -50,-50 v0 a50,50 0 0 1 50,-50" />
</svg>
<style>
#background-path {
stroke: grey;
stroke-width: ${bar_thickness};
}
#progress-path {
stroke: ${color};
stroke-width: ${bar_thickness};
stroke-dasharray: ${totalLength};
stroke-dashoffset: ${progress};
transition: stroke-dashoffset 1s linear;
}
</style>
hehe
i just removed the whole strole-linecap and worked ๐
but none might be more safe to use
your way*
and i have to fix my const to var i see now hehe
var is the safest approach IMO. const is best if you're not going to change the value later.
I also ended up making a second custom_field and setting the z-index of each. It works, but if it works with a single custom_field, that's probably the better way to go.
so you z-indexed the SVG background in a custom field? that might work for many other things.. like adding shadows and stuff ๐
i will need to test that ๐
Yeah. It allowed me to put the background under the progress.
But, interestingly, the background stroke-width wouldn't change and I couldn't figure out why. ...I still had the box-shadow in place... ๐คฆโโ๏ธ
Putting the background in the code before the progress would have probably worked too but I wanted the background at the very end (so it would be easy to delete if it didn't work.)
Had to change totalLength from 311.1 to 314 now that the linecap is gone. (There was a gap at 100%.)
i think i will try your way with the background when im back home, i se it made some complication having it in the same custom field
I just put the two paths back together. Seems to work for me.
You guys are awesome, I drop and image and with in a couple hours you 2 smashed it out. I really appreciate the support guys. ๐.
I am just on my road to learning all this, you are were I want to be. Kudos.
If you would have posted your question a week or two ago, I doubt there would have been a response. It just so happened that BerrisNO was working on something similar and the concept was able to be adapted.
I just added some variables that can be easily defined and color options for the bar. The bar can now be a defined color, simple 5-color scale, or dynamic red-to-green.```yaml
variables:
demo_percentage: 65
bar_thickness: 13
bar_background: gray
bar_color: dynamic
I incorporated the dynamic colors from one of the examples on my GH.
So, I just learned something new based on something that I've been trying to incorporate more: conditional (ternary) operators. But, I used it to set a variable. I went from javascript if (typeof variables.demo_percentage !== 'undefined') { var percentage = variables.demo_percentage } else { var percentage = entity.state } to```javascript
var percentage = (typeof variables.demo_percentage !== 'undefined') ? variables.demo_percentage : entity.state
Oh.
Just had a genius idea: merging the square and circle configurations into one super card. ๐ฆธ
you and me think alike bro!
I was thinking of a if variable.type = circle ( sett different values)
We do like super card hehe
using your code might help with that...
var type = (typeof variables.type !== 'circle') ? variables.type : square
how the frick did you add the colors to pasting code in discord? ๐
I was thinking along those lines, however, I have an interesting spin on it. Two custom fields: progress-square and progress-circle. In the styles: for each custom_field, display: none could be set to hide the one not in use.
For the rest of the card, there should be a relatively easy way to handle the layout without having to use IF statements on each line. Perhaps mediaquery or something.
After the three backticks, add the language: yaml, javascript, python, jinja, etc.
I've already incorporated more variables. It will soon be ready to turn into a template and theat's when it will become really useful. ```yaml
variables:
demo_percentage: 34
card_height_width: 120
card_background: black
icon_state_color: white
bar_thickness: 13
bar_background: gray
bar_color: blue
Same as with github basically
exactly.
why use height and width?
Just because apect_ratio never really works for me. I tend to set specific sizes for the majority of my cards (provided they're not sitting in a grid card or something like that.)
ah noted
That's what works for me but I'm sure there's a way to incorporate both.
i add everything inside grids automatically hehe
woops i did something with the text hehehe
I noticed earlier if I set the demo_percentage to 0, the state disappeared.
Correction: it actually returns the entity.state now that I have it set to use either. Kind of a weird thing. Not really a bug but I guess it has something to do with 0 kind of being undefined.
@karmic raptor Come join in the fun here. I'm curious how you got that to work.
I'm on my way home. I can discuss 2 hours later
@open ridge what styles have you applied to the card?
my name and state is to fare bewlow hehe.
probably because i dont use height and width
I started working with the layout a little bit. I posted the whole code here: ~~https://pastebin.com/sSq4bm0c~~ https://pastebin.com/raw/sSq4bm0c (TTL: 1 week)
This has both square and circle buttons.
what happens if you set the border radius to 25% instead?
It makes a rounded square. That's what I used in the first example around Line 11.
This is using the background thing that we worked on earlier?
thats the code withouth the SVG thing that we are working on
I belive my theme is screwing with me hehe. i cant get the text to be placed correctly hehee
never mind.. spell check
I use it and have edited it
it looks good ๐
If you want to follow the shape, you need to add code for it
I can try 2 hours later
no no, we are trying a different way, without using the background thing ๐
@open ridge i got it working now:
Ah. That was the conic-gradient background method.
yes sir
Im working on all the variables now, prepairing it for the config change ๐ but i need your help on making this the super duper card! ๐
One thing that I've considered about my version is that it is really only useful for 0-100% values. Although, I was considering how to make it a bit more flexible to handling other values.
you can add the threshold thing
i can share mine in 2 sec
might take a bit more then 2 sec.. i screwed up my threshold hehehe
I forgot how to add a sensor in variables lol hehe
https://dpaste.org/2DGJN this is how fare i got ๐ just need to figure out how to set a sensor into the variables, and return it in the code. : )
Do you mean like this: yaml variables: sensor_entity: sensor.ams_7494_pthen at Line 61javascript var percentage = (state / states[variables.sensor_entity].state) * 100
oh my god.. was it my ' ' that made it go in error mode?
Or you can just pull the value into the variable:yaml variables: sensor_entity_state: "[[[ return states['sensor.ams_7494_p'].state ]]]"then at Line 61javascript var percentage = (state / variables.sensor_entity_state) * 100
i always do this.. add ' ' where is not supposed to be, and then not when i need them hehehe
Yeah. They can make a big difference sometimes. ๐
dude... 20 minuits of problem shooting.....
var threshold = states[variables.sensor_threshold].state
var percentage = (state / states[variables.sensor_threshold].state) * 100
nice it works!
i have so many things i should do... but this thing is fun ๐
I should be asleep right now... 5.5 hours should be good enough though.
Oh. I noticed what you did with the background opacity. I didn't even consider going that route, but it makes sense that it would work.yaml opacity: ${bar_bg_opacity};
yes, inspired by you tricking me into using variables ๐
You don't have to set the variables in your code block to use variables set at the card level. Just call the variables.whatever or entity.state directly since they're already defined. javascript var bar_thickness = variables.bar_thickness; var state = entity.state; var bar_bg_opacity = variables.bar_bg_opacity; var bar_bg_color = variables.bar_bg_color;
ah ok
The only problem with setting the bar_bg_opacity, is if it is undefined, it will break the card. When/if you turn it into a template, you must set the variable within the template (like a default setting). Any variables defined at the card level will override the template. Really, all variables need to be defined at the template level to ensure default values are present. (Except for demo_percentage that I used which is special.)
i usually always set tie variables in the template
var state = entity.state;
var threshold = states[variables.sensor_threshold].state
var percentage = (state / states[variables.sensor_threshold].state) * 100
if (percentage >= 90) var color = 'red';
else if (percentage >= 70) var color = 'orange';
else if (percentage >= 50) var color = 'yellow';
else if (percentage >= 30) var color = 'lightgreen';
else var color = 'lightgreen';
var totalLength = 315;
var progress = (1 - state / threshold) * totalLength;
var svg = `
<svg id="progress-bar" fill="none" viewBox="0 0 100 100" width: 100%; height: 100%;>
<path id="background-path" d="M50,0.2
h0 a50,50 0 0 1 50,50 v0 a50,50 0 0 1 -50,50
h0 a50,50 0 0 1 -50,-50 v0 a50,50 0 0 1 50,-50" />
<path id="progress-path" d="M50,0.2
h0 a50,50 0 0 1 50,50 v0 a50,50 0 0 1 -50,50
h0 a50,50 0 0 1 -50,-50 v0 a50,50 0 0 1 50,-50" />
</svg>
<style>
#background-path {
opacity: ${variables.bar_bg_opacity};
stroke: ${variables.bar_bg_color};
stroke-width: ${variables.bar_thickness};
}
#progress-path {
stroke: ${color};
stroke-width: ${variables.bar_thickness};
stroke-dasharray: ${totalLength};
stroke-dashoffset: ${progress};
transition: stroke-dashoffset 1s linear;
}
</style>
`;
return svg;
thanks, that made it allot cleaner ๐
Just found something that could be useful.
The nullish coalescing (??) operator is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing
Well, that solves that problem: opacity: ${variables.bar_background_opacity ?? 0.2};
I was actually looking for something else dealing with null but this turned out to be better.
Correct. If the variable is not defined at all or is simply null, it returns 0.2. If the variable has a value, it uses that value.
that is sawesome
so we can set all defaults, to help the user get a "standard" then they can edit variables if needed
Yup. Without having to explicitly define the variables in the template ahead of time.
nice find dude! that is perfect
so i just added it and it works real nice: bar_thickness: #set a value between 1-100, default set to 5
real handy!
#background-path {
opacity: ${variables.bar_background_opacity ?? 0.2};
stroke: ${variables.bar_background ?? "gray"};
stroke-width: ${variables.bar_thickness ?? 13};
}```
same with what i did hehe perfectto
Is it allowed to work on this in the hospital, if wife desides to poop out child nr2 while we are working on this? hehhe
I'm sure she won't mind. (Disclaimer: Don't listen to me as I could be wrong.)
styles:
card:
- height: '[[[ return `${variables.card_height_width ?? 120}px` ]]]'
- width: '[[[ return `${variables.card_height_width ?? 120}px` ]]]'
- background: '[[[ return variables.card_background ?? "black" ]]]'
your faster then me! dang it ๐
but i dont get the "stroke: gray" thing... it gives me an error
stroke: ${variables.bar_bg_color ?? gray};
This gives no error if you have something in the variables, but when you remove it, i get a syntax
Looks like gray has to be in quotes. stroke: ${variables.bar_background_color ?? "gray"}; which makes sense because otherwise it could be a a variable.
true
styles:
img_cell:
- height: '[[[ return `${variables.icon_height ?? 120}` ]]]'
- height: '[[[ return `${variables.icon_width ?? 120}` ]]]'
so that actually made things allot easier for "beginners"
might be a stupid question.. but when i use 100 instead of 100px, what does it actually use?
px might be a default. I didn't consider it would have a default so I just added the px to keep it from breaking.
EDIT: I just tried to remove px from the card's height and width and it broke the card.
If the demo_icon is defined, it will use that. Otherwise, it defaults to the entity's icon. icon: "[[[ return `${variables.demo_icon ?? ''}` ]]]"
might be safe to use px then.
i can se that without it braks at my side as well hehe
was kind of cool with a bigger backgrund svg hehe
there is many customazation things we can do with this card.. by just changing the width and stuf.. love it!
might consider using % instead of px.
yeah i usually do
Can someone send the whole card code?
I tried to piece together stuff and failed
I will update my GitHub when I'm back home tomorrow. Currently a bit busy ๐
I know where to come when my get my next idea haha. I spent all weekend searching for something like this, and a few evenings Trying to force ApexCharts to do what I want. Would be really cool to test it out when you guys are ready... I am really good at breaking things ๐คฃ
background_in_card:
- width: |-
[[[ if ( variables.main_card_height < variables.main_card_width )
return Math.floor(variables.main_card_width-(variables.main_card_height-(variables.main_card_height/1.1)))+'px';
return Math.floor(variables.main_card_width/1.1)+'px';
]]]```
for this
https://github.com/BerrisNO/HA-Custom-Cards ๐ its just in testing, but try it out ๐
https://github.com/BerrisNO/images/blob/main/circle.PNG
I think the images repo you use is private
I've seen images "not get linked properly" when pasting directly into the editor. That's probably all that is. Just means the image needs to be re-added.
Nevermind. I just looked at the link. He probably wrote it in a private repository and then copied the markdown into the public.
I will try to fix it tonight
Just want to say thanks to everyone who helped and a big thanks to @woeful dome for the coaching. I have got the key parts of what I ws hoping to achieve, just need a little tweaking of shadows etc.
I take no credit for this. Here is the Code I ended up with for each "chip card"
as I make more tweaks I will share them here if anyone is interested
Beautiful ๐ quick note. When pasting code in here you might want to use dpaste.org or something ๐๐
Could you check now? I think I solved it hehe
I visited earlier and noticed the image was visible.
Thanks buddy!
I tried the site last time you said that and it didn't load, just tried again and its working now. so here is my latest.
Now I just want to find other things with Batteries so I can make more hahah
Do the iPad & Polestar images not show the rest of the bar properly (from 48-100% and 91-100%, respectively)?
Or do you just have it set to none which I just figured out works, too.
I'm dumb. Just noticed you posted code link. I see it now. You have the bar thickness at 8 and the bar background thickness at 2. That makes sense now.
In my version, I just used one variable that applied to both paths.
I'm using this yaml
Any way to fix the overlap in horizontal stacks ?
You want them to display on top of each other?
next to each other
like this
Here I used a grid card but you can apply the same concept to the horizontal-stack.```yaml
type: grid
cards:
- type: custom:button-card
entity: sensor.s22_ultra_battery_level
[...all the yaml here...] - type: custom:button-card
entity: sensor.s22_ultra_battery_level
[...all the yaml here...]```
grid seems wo work well, but it adds a lot of extra blank space
You can "control" the spacing by setting the number of columns (accounting for future cards.)
columns: 8```
Perfect!
I can have a row on my dashboard dedicated to batteries
If you want to get really fancy, you can incorporate an auto-entities card and have it "generate" the bar-cards. Here, BerrisNO has it set to find power usage and sort it by consumption. (It can "find" the entities to display or you could define a list. Minimal repetitive code.)
The auto entity is using labels to find what I want to show. So I add labels to everything I want shown there, and it filters it automatically ๐
i don't have a lot of stuff to display like that
only phone and watch currently
possibly a galaxy ring in the future :)
It can also accept wildcards. So, something like sensor.*_battery_level could be used.
Holy crap! 750-day streak on DuoLingo. That's crazy!
if i don't practice in a day all the screens in my house go crazy
How so?
๐
that works well with the dark theme, on a light theme, the Green disappears a little in to the grey colour I chose.
I used the new view type that is experimental, called "sections"
then I can drag and re-arrange the cards easily
I don't find it customizable enough to make my dashboards in
I prefer the grid view with CSS layouts
I usually do to, just trying out the new feature.
Strange one. Does any one know why the coloured progress bar isn't showing up on my iPad dashboard? everything else is fine, just the progress bar is missing....
Yeah its kind of a IOS thing..IOS does not like SVG files, trying to find a way around it
can you install other borwsers in ipad? like firefox or something?
I am using the HA app on the iPad in focused mode, I am guessing switching to Chrome wouldn't be a big issue.
Well not sure if it will help... But it's worth a try
No luck, same issue with chrome on the iPad.
Hmm well shit
playing with other uses of this card of yours, and trying to figure out how to change the Shadow colour based on the entity state. Why? - I have sensors on doors and windows, when they are all closed I just want a plain grey card, but if any are open I want it to be red. the idea is that I can quickly glance at my dashboard and know if everything is closed before leaving or going to bed.
I have tried adding if/else statements in the box-shadow area, but it doesn't seem to like that, tried using another method I found here: https://community.home-assistant.io/t/custom-button-card/386807 but again I think its the box-shadow doesn't like it.
I even tried setting up two box_shadows as variables but again styles doesn't seem to like if/else statements ( or at least how I have written them). If I google it, I never seem to get the same answer in 2 places.
I have already started on that with shadows, it will come out on next update I think
But. I would probably use another method.. and that's conditions. So it only shows if anything is open, and hides closed stuff
It will be on a separate card though
oh cool. Well just if it helps at all this is some of the ones I am playing with:
My next step was going to make the Electric car box-shadows glow blue, when they are charging. so I will take a look at conditions.
or maybe pulsating, but let me start with getting the right colour first hahaha
just to help me narrow down my searching and learning, this is considered CSS right? or is it something else?
yes similar to that, but for the shadow only. need to learn how to use conditions in styles first
I tried to follow what you have done on your Vehicle card, and tried it 20 different ways, but I am getting some odd behaviour. Sometimes it changes colour, but then it won't change back. I thought it might have something to do with the state value being text rather than an integer, but I tried both and still nothing. No rush, you got a lot more important things going on, just when you get a few mins spare, can you tell me where I am going wrong? cheers
thought it might have something to do with the state value being text rather than an integer
@olive oracle All states are strings. But, when you do comparisons of a string that is a number, it has to be coveted to an integer. With Javascript, useparseInt(value)orparseFloat(value); with Jinja, pipe to int/floatvalue | intorvalue | float.
turns out it was throwing up the strnage bevahiour as it was unhappy that I had semi-colons at the end of the box shadow inputs. wish the inbuilt de-bugging was a little smarter to counter act my lack of it haha
I also needed to create another template that gave me an 'on' / 'off' value based on the number of doors open.
everything closed
a door open:
and if you tap the card a pop-up tells me what door is open:
What does this pop up look like when they're are multiple doors open?
You can use Bubble Card or Browser_mod
back on this now, been a busy week at work and been on holiday. Even though my template says to " | list | join('/n')" for some reason it doesn't put them on a new line, just adds them to the back of each other. so will play a bit more to get that to work better. If I can't figure that out I will use the pop_up card mod
unfortunately, because Apple have to be different I was unable to get these to work on my iPad dashboard. so in the meantime I have changed tack to using the inset shadow to give me a red/amber/green visual state. I do not like it as much as the one you guys developed ( progress bar around the outside), but not much I can do about Apple and SVGs
might add another colour state ( pulsating blue) based on a different entity ( charging state), next.
animation is fun ๐
@woeful dome I have the SVG working on the Ipad now too, if you are interested
how?
@woeful dome
Here's my full code. Still working in the car one that gives an additional state of plugged in and not charging. Will share that when it's done. But for now here is my one for a device state of charge with a pulsing shadow if charging.
https://dpaste.org/jaW7D
And the svg works on iPad? What part of the code made the svg on iPad work?
Thanks. I haven't had much time after our newborn son came to life, need quite bussy hehe
In all honesty, it was the chatgpt part lol. I popped your svg code in and asked it to adjust it to work with apple, after a few extra adjustments it got it working. You could stick both codes into chatgpt and ask it why one does and the other doesn't work.
I have been there myself mate, I get it.