『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 1177|回复: 20

[UI界面] button card使用分享

[复制链接]

5

主题

75

帖子

788

积分

高级会员

Rank: 4

积分
788
金钱
713
HASS币
0
发表于 2024-7-27 12:10:22 | 显示全部楼层 |阅读模式
本帖最后由 hungheo 于 2024-8-19 22:06 编辑

之前的手机客户端UI主要用card-modDecluttering Card来写的
虽然两个插件都很强大,但是用card-mod来left、right调位置确实调的辛苦

看了这个UI大神的分享:https://bbs.hassbian.com/thread-25894-1-1.html
自己用button card改了之前的UI,感觉舒服很多,尤其是2个不同分辨率的手机同时用一套UI的时候

个人觉得,button card适用于对前端不熟悉的,对JavaScript熟悉的
本人不是程序员,纯属个人爱好,之前用nodered学了一点JavaScript,感觉用着还是比较顺畅
AI工具真的很强大,很好用,大部分代码或者问题都是问的AI,我用的是阿里的通义千问
参考链接:
本论坛的UI大神:https://bbs.hassbian.com/thread-25964-1-1.html
玩button card的国外大神:https://github.com/UI-Lovelace-Minimalist/UI


以下是分享的一些认识、技巧:
一楼:button card的基本操作,主要是布局部分
二楼:template模板介绍,简单使用
三楼:template使用中的几个小注意
四楼:一些觉得有意思的卡片模板分享

还在完善中,后面再分享一些自己的总结和基于模板卡片

如果有错误的地方,欢迎指正
有更好的idea,欢迎分享

完整代码在19楼


最后,分享下自己的界面:

手机UI
屏幕截图 2024-07-27 151911.png 屏幕截图 2024-07-27 151931.png 屏幕截图 2024-07-27 151948.png 屏幕截图 2024-07-27 152113.png 屏幕截图 2024-07-27 152135.png 屏幕截图 2024-07-27 152412.png

PAD UI

屏幕截图 2024-07-27 151802.png







评分

参与人数 4金钱 +48 收起 理由
DIYSmartHome + 8 论坛有你更精彩!
ekingfan + 8 高手,这是高手!
消逝 + 16 论坛有你更精彩!
hhh. + 16 高手,这是高手!

查看全部评分

回复

使用道具 举报

5

主题

75

帖子

788

积分

高级会员

Rank: 4

积分
788
金钱
713
HASS币
0
 楼主| 发表于 2024-7-27 12:36:11 | 显示全部楼层
本帖最后由 hungheo 于 2024-7-27 15:32 编辑

一、布局:

1、button card的默认布局,如下图:

屏幕截图 2024-07-27 110105.png

card:最外层卡片
container:grid布局
img_cell:图标或图片,里面还有icon标签
name:名称
state:状态
label:标签

设置显示与不显示,对应的true或者false就行
2、styles设置
2.1、grid设置
在grid里面,“i”对应img_cell,“n”对应name,“s”对应state,“l”对应label
调整grid-template-area的值就能对各个div进行调整,代码:
type: custom:button-card
entity: sensor.a4c138d5271a_temperature
show_name: true
show_icon: true
show_state: true
show_label: true
label: 这是lebel
styles:
  grid:
    - grid-template-areas: |
        "i n" 
        "i s" 
        "i l"
    - grid-template-rows: 1fr min-content min-content
    - grid-template-columns:  min-content 1fr;
  img_cell:
    - width: 40px
    - height: 40px

grid-template-rows:调整行距离
grid-template-columns:调整列距离
常用的是:min-content(最小的合适空间),1fr(1倍剩余空间,也可以是0.5fr),或者固定大小(50px),或者百分比(20%)grid里面还有其他的属性可以设置,比如居中、对齐、线间距等等,具体可以AI工具搜:CSS grid属性
个人习惯:宽度一般不用px设置,这样在不同的手机屏幕就可以自动适应宽度
调整后效果:
屏幕截图 2024-07-27 111820.png

2.2、其他标签设置
card、name、state、img_cell、icon、label、custom_fields 都可以设置对应的css样式
具体有哪些,可以浏览器F12,选中标签,然后计算样式里面都有
屏幕截图 2024-07-27 123141.png

修改下之前的代码:
type: custom:button-card
entity: sensor.a4c138d5271a_temperature
show_name: true
show_icon: true
show_state: true
show_label: true
label: ,这个温度太热啦
styles:
  grid:
    - grid-template-areas: |
        "i n n" 
        "i s l" 
    - grid-template-rows: 1fr min-content
    - grid-template-columns: min-content min-content 1fr;
  card:
    - padding: 8px
  img_cell:
    - width: 40px
    - height: 40px
    - background: rgba(139,195,74,0.2)
    - border-radius: 50%
    - margin-right: 8px
  icon:
    - width: 24px
    - height: 24px
    - color: var(--green-color)
  name:
    - justify-self: flex-start
    - font-size: 14px
    - font-weight: 600
  state:
    - justify-self: flex-start
    - font-size: 12px
  label:
    - justify-self: flex-start
    - font-size: 12px
类似mushroom card的template card的样式卡片效果就出来了
屏幕截图 2024-07-27 123538.png

2.3、自定义布局

主要参考这位UI大神的技巧分享:https://bbs.hassbian.com/thread-25964-1-1.html
设置布局时,grid-template-areas里面的相同区域必须是矩形,其他的根据个人需求
比如,模仿户型图:
type: custom:button-card
styles:
  grid:
    - grid-template-areas: |
        "a1 z1 z1 a2 a3 d1 d1 a4 a5 k k k"
        "z2 z2 z2 z2 a3 d d d a5 k k k"
        "a7 a7 a7 z3 a3 a6 a6 a6 a5 k k k"
        "p p a8 z3 a3 k1 k1 k1 k1 k k k"
        "p p a9 a9 a9 a9 a9 a9 a9 k k k"
        "b1 b1 b1 x x x b4 c b5 k k k"
        "b1 b1 b1 x x x b4 b6 b5 b7 b8 k2"
        "b1 b1 b1 c1 c1 c1 c1 f f f b8 k2"
        "b1 b1 b1 b2 f1 f1 f1 f f f b8 b9"
    - grid-template-columns: 5px 20px 5px 15px 5px 20px 5px 20px 5px 25px 5px 20px
    - grid-template-rows: 10px 55px 5px 15px 5px 35px 5px 5px 25px
  card:
    - width: 150px
    - height: 160px
    - background-color: rgba(0,0,0,0)
custom_fields:
  k: #区域代号
    card:
      # ......这个区域的卡片
效果:

屏幕截图 2024-07-27 130825.png


2.4、custom_fields自定义区域
在styles下的时候,可以自定义某个区域的css属性

和styles同级的时候,可以自定义某个区域的内容,可以是html标签、可以是card等等
代码:
type: custom:button-card
show_icon: false
show_state: false
show_name: false
styles:
  grid:
    - grid-template-areas: |
        "weather sun person1 person2"
        "info info info info"
        "card1 card1 card1 card1"
        "card2 card2 card2 card2"
        "card3 card3 card3 card3"
        "card4 card4 card4 card4"
    - grid-template-columns: min-content 1fr 38px 38px
    - grid-template-rows: 55px min-content 1fr
  card:
    - padding: 8px
  custom_fields: #自定义区域的属性
    sun: #sun区域的属性
      - justify-self: center #让sun这个区域的内容,水平居中
      - align-self: flex-start #让sun这个区域的内容,垂直方向,在最顶上
    card1: #card1区域属性
      - display: "[[[ return states['input_boolean.xinxi_xiangqing'].state == 'on' ? 'block' : 'none' ]]]" #如果是开则显示card1、是关不显示card1
custom_fields: #自定义区域的内容
  sun: #自定义区域名称
    card: #区域的类型
      type: custom:button-card
      entity: sun.sun
      show_icon: true
      show_name: false
      show_state: true
      # .....该卡片其他属性
  card1:
    card:
      type: custom:button-card
      entity: sensor.youjia
      template: entity_card
      icon: mdi:gas-station
      name: 92#油价
      label: |
        [[[ return `${states['sensor.youjia'].attributes[92]}元/L`]]]
      variables:
        icon_color: var(--green-color)
  # .......区域内容
PS:display这个属性很好玩,可以实现收起、展开效果
效果:

屏幕截图 2024-07-27 134955.png     屏幕截图 2024-07-27 134925.png





回复

使用道具 举报

5

主题

75

帖子

788

积分

高级会员

Rank: 4

积分
788
金钱
713
HASS币
0
 楼主| 发表于 2024-7-27 14:22:51 | 显示全部楼层
本帖最后由 hungheo 于 2024-7-27 15:00 编辑

2、template模板
这个功能很强大!!
可以追加、覆盖、套嵌等等
如果用到变量处理,不支持jinja2模板,只支持JavaScript!
2.1、 基本结构

#template结构
button_card_templates: #顶格
  some_template: #模板名称
    show_name: true  
    name: 这是模板里面的名称
    styles:
      icon:
        - color: '[[[return variables.icon_color]]]' #这里的variables.icon_color是模板内的变量,
    #......其他内容

#template使用
type: custom:button-card
entity: sensor.a4c13801e940_temperature
show_label: true
name: 这是card的名称  #相同的name,template里面的name的内容,在这里被替换掉
label: > #entity.state,因为之前定义了entity: sensor.a4c13801e940_temperature,所以这里等效于states.['sensor.a4c13801e940_temperature'].state
  ]]]
    if(entity.state > 30) return "好热啊"  
  [[[   
template: some_template #引用模板
variables: 
  icon_color: red #设定变量值
2.2、 template追加、覆盖、多级引用
可以创建多个template模板,然后一起引用,template的内容追加上去,重复定义的根据顺序也会覆盖掉
比如:
button_card_templates: 
  title: #标题
    show_icon: false
    show_name: true
    show_state: false
    show_label: true
    tap_action:
      action: "none"
    styles:
      grid:
        - grid-template-areas: '"n c d" "l c d"'
        - grid-template-columns: minmax(auto, 1fr) 60px 60px
        - grid-template-rows: 1fr
        - column-gap: 20px #分隔大小
      card:
        - height: auto
        - padding: 0px 0px 0px 10px
        - box-shadow: none
        - border-radius: 0px
        - background: none
        - overflow: visible
      name:
        - justify-self: start
        - font-weight: 500
        - font-size: 1.2rem
      label:
        - justify-self: start
        - font-size: 1rem
        - opacity: 0.5

  title_popup: #标题带阴影
    styles:
      name:
        - overflow: visible
        - color: white
        - text-shadow: black 0px 0px 20px; #颜色 横向偏移 纵向偏移 扩散
      label:
        - overflow: visible
        - color: white
        - text-shadow: black 0px 0px 20px; #颜色 横向偏移 纵向偏移 扩散
    template: title #引用title模板
  
  title_button: #标题全开全关按钮
    custom_fields:
      c:
        card:
          type: custom:button-card
          name: 全开
          show_icon: false
          show_name: true
          show_state: false
          styles:
            name:
              - font-size: 1rem
              - justify-self: center
              - font-weight: 500
            card:
              - margin-top: 10px
              - padding-top: 5px; 
              - padding-bottom: 5px;
              - overflow: visible
          tap_action:
            action: call-service
            service: '[[[ return variables.turn_on]]]'
      d:
        card:
          type: custom:button-card
          name: 全关
          show_icon: false
          show_name: true
          show_state: false
          styles:
            name:
              - font-size: 1rem
              - justify-self: center
              - font-weight: 500
            card:
              - margin-top: 10px
              - padding-top: 5px; 
              - padding-bottom: 5px;
              - overflow: visible
          tap_action:
            action: call-service
            service: '[[[ return variables.turn_off]]]' 
上面定义了3个template
title是最基础的
title_popup引用了title模板,并且追加了文字是带阴影
title_button是custom_fields的内容

应用起来就是:

1、引用最基础的title:
type: custom:button-card
name: 餐厅
label: 子擎存在传感器
template: title 
效果:

屏幕截图 2024-07-27 143746.png

2、追加一个title_button
template引用分先后的,这里要先引用title,再是title_button,因为title是card主体,title_button是追加部分
type: custom:button-card
name: 主卧
template: 
  - title #它先
  - title_button #这个是追加上去的
variables:
  turn_on: script.turn_on_auto_light_zhuwo
  turn_off: script.turn_off_auto_light_zhuwo
效果:

屏幕截图 2024-07-27 144258.png

3、多级引用
type: custom:button-card
template: 
  - title_popup #这个模板引用了title模板
  - title_button #这个是追加上去的
name: 客厅
label: >
  [[[return Number(states['sensor.ke_ting_kai_deng_shu_liang'].state) + "个灯开着";]]]
variables:
  turn_on: script.turn_on_light_keting
  turn_off: script.turn_off_light_keting
效果:

屏幕截图 2024-07-27 144821.png




回复

使用道具 举报

5

主题

75

帖子

788

积分

高级会员

Rank: 4

积分
788
金钱
713
HASS币
0
 楼主| 发表于 2024-7-27 15:35:08 | 显示全部楼层
本帖最后由 hungheo 于 2024-7-27 21:09 编辑

3、template使用中的几个小注意
3.1、状态的表达方式
return states['switch.myswitch'].state // 这样写能正确显示

// 下面这样写会报错
 const test = switch.myswitch
 return states[test].state 

3.2、variables:以字母顺序来确定是否可以引用
### 这个是能引用的
variables:
  index: 2
  value: '[[[ return variables.index + 2; ]]]'
name: '[[[ return variable.value; ]]]'  # 输出 4  因为i在v前,所以value能继承index的值

### 这个是不能引用的
variables:
  z_index: 2
  value: '[[[ return variables.z_index + 2; ]]]' # 错误  因为v在z前,所以value不能继承z-index的值
name: '[[[ return variable.value; ]]]' 


3.3、entity的使用位置

button card中如果定义了entity,在JavaScript模板中是可以直接用entity接属性直接用的,但是要注意一下

多级button card情况下
type: custom:button-card
entity: light.649e3186d9cd_light
show_state: true
show_label: true
label: '[[[ return entity.entity_id ]]]'
styles:
  grid:
    - grid-template-areas: |
        "i n a"
        "i s a"
        "i l a"
    - grid-template-columns: min-content 0.5fr 0.5fr 
    - grid-template-rows: 1fr
custom_fields:
  a:
    card:
      type: custom:button-card
      entity: sensor.a4c138d5271a_temperature
      show_state: true
      show_label: true
      label: '[[[ return entity.entity_id ]]]'
一级、二级的label都会显示一级的entity_id

如图:
屏幕截图 2024-07-27 205515.png

如果想要用本级card的entity_id。要么在card前面加上do_not_eval:true,要么用template模板

......
custom_fields:
  a:
    do_not_eval: true 
    card:
      type: custom:button-card
      entity: sensor.a4c138d5271a_temperature
.......
效果:
屏幕截图 2024-07-27 210026.png


回复

使用道具 举报

5

主题

75

帖子

788

积分

高级会员

Rank: 4

积分
788
金钱
713
HASS币
0
 楼主| 发表于 2024-7-27 15:36:23 | 显示全部楼层
本帖最后由 hungheo 于 2024-7-27 22:44 编辑

4、一些有意思的模板卡片
4.1、自动生成看板小图标
这个是照搬UI-Lovelace-Minimalist,精简了一些内容。
button_card_templates: 
  row_pill:
    show_icon: false
    show_name: true
    show_label: false
    variables: #想要更多这里再加entity_?
      entity_1:
        entity_id: ""
        name: ""
        icon: ""
        color: ""
      entity_2:
        entity_id: ""
        name: ""
        icon: ""
        color: ""
      entity_3:
        entity_id: ""
        name: ""
        icon: ""
        color: ""
      entity_4:
        entity_id: ""
        name: ""
        icon: ""
        color: ""
      entity_5:
        entity_id: ""
        name: ""
        icon: ""
        color: ""
    styles:
      grid:
        - grid-template-areas: >
            [[[
              var pills = []
              const entities = [variables.entity_1?.entity_id, variables.entity_2?.entity_id, variables.entity_3?.entity_id, variables.entity_4?.entity_id, variables.entity_5?.entity_id]  //想要更多这里再加variables
              function entity_check(item) {
                  if (item != "") {
                      pills.push("item" + (pills.length+1))
                  }
              }
              entities.forEach(entity_check)
              return pills.join(" ")
            ]]]
        - display: flex
        - grid-template-rows: min-content
        - justify-content: space-evenly
      card:
        - background: none
        - box-shadow: none
        - broder: 0px
        - border-radius: none
        - padding: 12px
        - overflow: visible
      custom_fields:
        item1:
          - display: "[[[ return (variables.entity_1.entity_id != '') ? 'block' : 'none' ]]]"
        item2:
          - display: "[[[ return (variables.entity_2.entity_id != '') ? 'block' : 'none' ]]]"
        item3:
          - display: "[[[ return (variables.entity_3.entity_id != '') ? 'block' : 'none' ]]]"
        item4:
          - display: "[[[ return (variables.entity_4.entity_id != '') ? 'block' : 'none' ]]]"
        item5:
          - display: "[[[ return (variables.entity_5.entity_id != '') ? 'block' : 'none' ]]]"
        item6:
          - display: "[[[ return (variables.entity_6.entity_id != '') ? 'block' : 'none' ]]]"
        item7:
          - display: "[[[ return (variables.entity_7.entity_id != '') ? 'block' : 'none' ]]]"
    custom_fields:
      item1:
        card:
          type: "custom:button-card"
          template: "pill"
          variables: "[[[ return variables.entity_1; ]]]"
          entity: "[[[ return variables.entity_1.entity_id ]]]"
          name: "[[[ return variables.entity_1.name ]]]"
          icon: "[[[ return variables.entity_1.icon ]]]"
          color: "[[[ return variables.entity_1.color ]]]"
      item2:
        card:
          type: "custom:button-card"
          template: "pill"
          variables: "[[[ return variables.entity_2; ]]]"
          entity: "[[[ return variables.entity_2.entity_id ]]]"
          name: "[[[ return variables.entity_2.name ]]]"
          icon: "[[[ return variables.entity_2.icon ]]]"
          color: "[[[ return variables.entity_2.color ]]]"
      item3:
        card:
          type: "custom:button-card"
          template: "pill"
          variables: "[[[ return variables.entity_3; ]]]"
          entity: "[[[ return variables.entity_3.entity_id ]]]"
          name: "[[[ return variables.entity_3.name ]]]"
          icon: "[[[ return variables.entity_3.icon ]]]"
          color: "[[[ return variables.entity_3.color ]]]"
      item4:
        card:
          type: "custom:button-card"
          template: "pill"
          variables: "[[[ return variables.entity_4; ]]]"
          entity: "[[[ return variables.entity_4.entity_id ]]]"
          name: "[[[ return variables.entity_4.name ]]]"
          icon: "[[[ return variables.entity_4.icon ]]]"
          color: "[[[ return variables.entity_4.color ]]]"
      item5:
        card:
          type: "custom:button-card"
          template: "pill"
          variables: "[[[ return variables.entity_5; ]]]"
          entity: "[[[ return variables.entity_5.entity_id ]]]"
          name: "[[[ return variables.entity_5.name ]]]"
          icon: "[[[ return variables.entity_5.icon ]]]"
          color: "[[[ return variables.entity_5.color ]]]"
  #椭圆形图标        
  pill:
    styles:
      grid:
        - grid-template-areas: '"i" "n"'
        - grid-template-columns: 54px
        - grid-template-rows: 54px min-content
      card:
        - border-radius: 50px
        - place-self: center
        - width: 54px
        - height: 84px
        - padding: 0px
      icon:
        - color: '[[[ return variables?.color ]]]'
        - width: 24px
      img_cell:
        - background-color: '[[[return "color-mix(in srgb, "+ variables?.color +" 20%, transparent)" ]]]'
        - border-radius: 50%
        - width: 42px
        - height: 42px
      name:
        - justify-self: center
        - font-weight: bold
        - font-size: 12px
    state:
      - value: 'off'
        styles:
          card:
            - height: 54px
          name:
            - display: none
    tap_action:
      action: more-info
模板只编辑5个小图标,想要多少照着格式改下就行。
引用代码:
type: custom:button-card
template: row_pill #template名称
variables:
  entity_1:
    entity_id: sensor.0x00158d000444b5af_humidity
    name: 湿度
    icon: mdi:water
    color: var(--light-green-color)  #这里涉及到计算透明背景,只能用var(--颜色名称-color),等效于light-green
  entity_2:
    entity_id: sensor.0x00158d000444b5af_humidity
    name: 湿度
    icon: mdi:bed
    color: var(--light-blue-color)
  entity_3:
    entity_id: sensor.0x00158d000444b5af_humidity
    name: 湿度
    icon: mdi:lightbulb
    color: var(--red-color)
  entity_4:
    entity_id: sensor.0x00158d000444b5af_humidity
    name: 湿度
    icon: ''
    color: var(--orange-color)
    #......,总共有5个,如果只想要4个,最后一个不定义就可以
效果:

屏幕截图 2024-07-27 221450.png


4.2、仿homekit灯卡片
跟button card作者的GitHub上的示例一样的,多了根据灯的样式,自动选择图标和显示不同类型的灯的详情

代码:
button_card_templates:
  light_card: #仿HomeKit卡模板
    show_state: false
    show_icon: true
    show_name: true
    icon: > #根据不同的名称,返回不同的图标
        [[[ var friendlyName = entity.attributes.friendly_name;
          if (friendlyName.includes('灯带'))  return 'mdi:led-strip-variant'; //检测包含文字
          else if (entity.entity_id.includes('auto_light')) return 'mdi:lightbulb-auto'; //检测包含文字
          else if (friendlyName.includes('吸顶灯')) return 'mdi:wall-sconce-flat'; //检测包含文字
          else if (friendlyName.includes('射灯')) return 'mdi:track-light'; //检测包含文字
          else if (friendlyName.includes('床头灯')) return 'mdi:lamp'; //检测包含文字
          else if (friendlyName.includes('浴霸')) return 'mdi:light-recessed'; //检测包含文字
          else if (friendlyName.includes('插座')) return 'mdi:power-socket-us'; //检测包含文字
          else return 'mdi:lightbulb';
        ]]] 
    label:  > #根据不同的类型,返回不同的详情状态
        [[[ 
            if (entity.entity_id.startsWith('light.')) {
                if (entity.state === "on") {
                    if (entity.attributes.color_temp_kelvin)  return `开 ${entity.attributes.color_temp_kelvin}k ${Math.floor(entity.attributes.brightness * 100 / 255)}%`;
                    else return '开启';
                }
                if (entity.state === "off")  return '关闭';
            } else if (entity.entity_id.startsWith('switch.')) {
                if (entity.state === "on") return '开启';
                if (entity.state === "off") return '关闭';
            } else if (entity.entity_id.includes('auto_light')) {
                if (entity.state === "on") return '开启';
                if (entity.state === "off") return '关闭';
            }
            return '错误';
        ]]]
    show_label: true
    grid:
      - grid-template-areas: '"i" "n" "s"'
      - grid-template-columns: 1fr
      - grid-template-rows: 1fr min-content min-content
    styles:
      label:
        - font-size: 12px
        - justify-self: start
        - padding-left: 10px
      card:
        - width: 90px
        - height: 90px
      img_cell:
        - justify-content: flex-start
      icon:
        - padding-left: 5px
      name:
        - justify-self: start
        - padding-left: 10px
        - font-weight: bold
        - font-size: 14px
    state:
      - value: 'off'
        styles:
          icon:
            - color: var(--light-grey-color)
          name:
            - color: var(--light-grey-color)
          label:
            - color: var(--light-grey-color)


#使用:
type: custom:button-card
entity: switch.6cd3eec81262_channel_1
name: 客厅射灯
template: light_card

效果:

屏幕截图 2024-07-27 222440.png

4.3、light卡片模板
我用的是乐式的驱动,比米家的多了一个灯光模式选择,自己加了自动调光的开关按钮
代码:
button_card_templates:
  light_ls_card: #乐式灯卡片模板
    show_icon: false
    show_name: false
    show_state: false
    styles:
      grid:
        - grid-template-areas: |
            "x a c z d y"
            "x w w w w y"
            "x b b b b y"
        - grid-template-columns: 4% 1fr 120px 15px 40px 4%
        - grid-template-rows: 40px 5px 50px
    custom_fields:
      a: #灯光图标、详情
        card:
          type: custom:button-card
          entity: '[[[ return  entity.entity_id ]]]'  #这里的entity是为了点击图标开关灯
          show_icon: true
          show_state: false
          show_name: true
          show_label: true
          icon: mdi:lightbulb
          name: '[[[ return variables.light_name ]]]' #这里的name因为这里是二级card
          label: >
            [[[ 
                if (entity.state == "on") 
                  return '开启 ' + entity.attributes.color_temp_kelvin + 'k ' + Math.floor(entity.attributes.brightness *100 / 255) + '%';  //计算取整
                else if (entity.state == "off") 
                  return '关闭';
                else return  '错误'
            ]]]
          styles:
            grid: 
              - grid-template-areas: |
                  "i a n"
                  "i a s"
                  "i a l"
              - grid-template-rows: 1fr min-content 1fr;
              - grid-template-columns: 40px 5px 1fr;  
            card:
              - height: 40px
              - padding: 0px
              - box-shadow: none
              - border-radius: 0px
              - border: 0px
              - background: none
            icon:
              - width: 24px
              - height: 24px
              - padding: 8px
              - border-radius: 50%
              - color: orange
              - background: rgb(255, 152, 0, 0.2)
            name:
              - font-size: 14px
              - justify-self: flex-start
            label:
              - font-size: 12px
              - justify-self: flex-start
          state:  
            - value: 'on'
              styles:
                icon:
                  - background: rgba(255, 152, 0, 0.2)
                  - color: orange
            - value: 'off'
              styles:
                icon:
                  - background: rgb(158, 158, 158,0.2)
                  - color: rgb(158, 158, 158)
      b: #亮度色温控制
        card:
          type: custom:mushroom-light-card
          primary_info: none
          secondary_info: none
          icon_type: none
          show_brightness_control: true
          show_color_temp_control: true
          entity: '[[[ return  entity.entity_id ]]]'
          card_mod:
            style: |
              ha-card { 
                padding: 0px !important;
                border-radius: 0px !important;
                border: 0px !important;
                margin: 0px !important;
                box-shadow: none !important;
              }
      c: #模式选择
        card:
          type: custom:mushroom-select-card
          entity: '[[[ return  variables.light_select ]]]'
          primary_info: none
          secondary_info: none
          icon_type: none
          card_mod:
            style: |
              ha-card {
                padding: 0px !important;
                border-radius: 0px !important;
                border: 0px !important;
                margin: 0px !important;
                --mdc-theme-surface: white !important;
                background: white !important;
                box-shadow: none !important;
                overflow: visible !important;
              }
      d: #自动调光开关
        card:
          type: custom:button-card
          entity: '[[[ return  variables.light_auto ]]]' 
          tap_action:
            action: toggle
          show_icon: true
          show_name: false
          show_state: false
          icon: mdi:lightbulb-auto
          styles:
            grid:
              - grid-template-areas: '"i"'
              - grid-template-columns: 40px
              - grid-template-rows: 40px
            card:
              - padding: 0px
              - background: none
              - border-radius: 12px 
              - box-shadow: none
              - background: none
            img_cell:
              - border-radius: 12px
            icon:
              - width: 24px
          state:
            - value: 'on'
              styles:
                img_cell:
                  - background: rgb(255, 152, 0, 0.2)
                icon:
                  - color: rgb(255, 152, 0)
            - value: 'off'
              styles:
                img_cell:
                  - background: rgb(158, 158, 158, 0.1)
                icon:
                  - color: rgb(158, 158, 158)



#使用
type: custom:button-card
entity: light.b850d8174501_light
template: light_ls_card
variables:
  light_name: 吸顶灯
  light_select: select.b850d8174501_mode
  light_auto: input_boolean.auto_light_zhuwo_xidingdeng


效果:

屏幕截图 2024-07-27 223148.png


4.3、两种样式卡片互相切换
这个用到之前提到的display属性,关灯的时候一种卡片样式,开灯的时候一种卡片样式

结合之前的仿HomeKit卡片和light卡片的模板
代码:
button_card_templates:
  light_card_exchange:
    show_icon: false
    show_state: false
    show_name: false
    styles:
      grid:
        - grid-template-areas: '"b" "d"'
        - grid-template-columns: 1fr
        - grid-template-rows: 1fr
      card:
        - padding: 0px
        - broder: 0px
        - margin: 0px
        - background: none
        - border-radius: 0px
        - box-shadow: none
        - overflow: visible
      custom_fields:
        b:
          - display: > #block显示,none不显示
              [[[ return entity.state == "on" ? "block" : "none" ]]]
        d:
          - padding-top: 10px
          - display: >
              [[[ return entity.state == "off" ? "block" : "none" ]]]
    custom_fields:
      b:
        card:
          type: custom:button-card
          entity: '[[[return entity.entity_id]]]'
          template: light_ls_card #引用模板
          variables:
            light_name: '[[[return variables.light_name]]]'
            light_select: '[[[return variables.light_select]]]'
            light_auto: '[[[return variables.light_auto]]]'
      d:
        card:
          type: custom:button-card
          name: '[[[return variables.light_name]]]'
          entity: '[[[return entity.entity_id]]]'
          template: light_card #引用模板



#使用
type: custom:button-card
entity: light.dced830254b8_light
template: light_card_exchange
variables:
  light_name: 吸顶灯
  light_select: select.dced830254b8_mode
  light_auto: input_boolean.auto_light_keting_xidingdeng
效果:

屏幕截图 2024-07-27 224332.png


未完待续。。。。
回复

使用道具 举报

7

主题

152

帖子

2680

积分

论坛技术达人

积分
2680
金钱
2518
HASS币
50
发表于 2024-7-27 17:24:21 | 显示全部楼层
大佬,pad ui教程能分享吗
回复

使用道具 举报

5

主题

75

帖子

788

积分

高级会员

Rank: 4

积分
788
金钱
713
HASS币
0
 楼主| 发表于 2024-7-27 19:41:10 | 显示全部楼层
ck3 发表于 2024-7-27 17:24
大佬,pad ui教程能分享吗

我也是参考别人的,原贴链接在这里:https://bbs.hassbian.com/thread-25894-1-1.html
难点不是代码,是彩平图,图标等等,CAD图纸是自己画的,彩平图是淘宝10块别人帮忙弄的,图标搞了好几天,找个图标制作网站,自己慢慢拼出来的
回复

使用道具 举报

9

主题

216

帖子

1811

积分

论坛UI达人

积分
1811
金钱
1595
HASS币
0
发表于 2024-7-27 20:15:50 | 显示全部楼层
大量使用button-card,配合mushroom-card,刷新界面,会出现不显示icon,楼主有遇到过吗?


回复

使用道具 举报

5

主题

75

帖子

788

积分

高级会员

Rank: 4

积分
788
金钱
713
HASS币
0
 楼主| 发表于 2024-7-27 21:18:53 | 显示全部楼层
hhh. 发表于 2024-7-27 20:15
大量使用button-card,配合mushroom-card,刷新界面,会出现不显示icon,楼主有遇到过吗?

我用了几种mushroom卡片:entity, climate,light,chips这些,也用card-mod。
其他的全是button card,最多套到了5层。
也遇到过加载不出来,要刷新很多次才显示,或者偶尔能加载出来。
好像是button card的问题,也有说用了card-mod或者browser-mod才出现。
F12看控制台,一般就是:
TypeError: Cannot read properties of undefined (reading 'style')
Uncaught (in promise) TypeError: e.setConfig is not a function
这个得慢慢找,清空缓存,或者隐身模式,找到了把他们放在template模板里面大多都能能解决
回复

使用道具 举报

9

主题

216

帖子

1811

积分

论坛UI达人

积分
1811
金钱
1595
HASS币
0
发表于 2024-7-27 21:29:31 | 显示全部楼层
hungheo 发表于 2024-7-27 21:18
我用了几种mushroom卡片:entity, climate,light,chips这些,也用card-mod。
其他的全是button card, ...

单独使用没有任何问题,只要在button-card中使用mushroom的卡片,就会出现这种问题,应该与其他卡片无关,就是按钮卡片和蘑菇卡片相互不兼容。

我现在想的就是尽量少用第三方卡片,多用原生卡片,按钮卡片,卡片模组辅助。

确实,这个问题的慢慢找,而且错误较多,得一一排查。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-9-8 09:13 , Processed in 0.070328 second(s), 37 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表