fablav

Multiple light entity

Feb 5th, 2025
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 7.17 KB | Software | 0 0
  1. type: custom:button-card
  2. entity: light.master_bedroom
  3. tap_action:
  4.   action: more-info
  5. show_icon: false
  6. name: Master Bedroom Lights
  7. custom_fields:
  8.   slider:
  9.     card:
  10.       type: custom:my-slider-v2
  11.       entity: light.master_bedroom
  12.       colorMode: brightness
  13.       styles:
  14.         container:
  15.           - border-radius: 100px
  16.           - overflow: visible
  17.           - background: none
  18.         card:
  19.           - height: 40px
  20.           - padding: 0 20px
  21.           - background: |
  22.              [[[
  23.                 // Get all valid lights (on, with rgb_color)
  24.                 function getValidLights() {
  25.                   let valid = [];
  26.                   if (entity.attributes.entity_id && Array.isArray(entity.attributes.entity_id)) {
  27.                     entity.attributes.entity_id.forEach(lightId => {
  28.                       let light = states[lightId];
  29.                       if (
  30.                         light &&
  31.                         light.state === 'on' &&
  32.                         light.attributes.rgb_color &&
  33.                         light.attributes.brightness > 0
  34.                       ) {
  35.                         valid.push(light.attributes.rgb_color);
  36.                       }
  37.                     });
  38.                   }
  39.                   return valid;
  40.                 }
  41.                 // Check if all lights have the same colour
  42.                 function isUniform(lights) {
  43.                   if (lights.length === 0) return false;
  44.                   return lights.every(c => c[0] === lights[0][0] && c[1] === lights[0][1] && c[2] === lights[0][2]);
  45.                 }
  46.                
  47.                 let validLights = getValidLights();
  48.                
  49.                 if (validLights.length === 1 || (validLights.length > 1 && isUniform(validLights))) {
  50.                   // Single (or uniform) light: use original gradient (darker to lighter)
  51.                   let [r, g, b] = validLights[0];
  52.                   let darkR = Math.min(255, Math.floor(r * 0.6));
  53.                   let darkG = Math.min(255, Math.floor(g * 0.6));
  54.                   let darkB = Math.min(255, Math.floor(b * 0.6));
  55.                   let lightR = Math.min(255, Math.floor(r * 1.3));
  56.                   let lightG = Math.min(255, Math.floor(g * 1.3));
  57.                   let lightB = Math.min(255, Math.floor(b * 1.3));
  58.                   return `linear-gradient(to right, rgb(${darkR}, ${darkG}, ${darkB}), rgb(${lightR}, ${lightG}, ${lightB}))`;
  59.                 } else if (validLights.length > 1) {
  60.                   // Multiple different colours: blend using each light's actual colour
  61.                   let stops = validLights.map(c => `rgb(${c[0]}, ${c[1]}, ${c[2]})`);
  62.                   return `linear-gradient(to right, ${stops.join(', ')})`;
  63.                 }
  64.                 // Fallback colour when no lights are on
  65.                 return "#4e4457";
  66.               ]]]
  67.           - transition: background 0.5s ease-in-out
  68.         track:
  69.           - overflow: visible
  70.           - background: none
  71.         progress:
  72.           - background: none
  73.         thumb:
  74.           - background: |
  75.              [[[
  76.                 // Use the first available bulb's colour for the thumb (darkened)
  77.                 function getValidLights() {
  78.                   let valid = [];
  79.                   if (entity.attributes.entity_id && Array.isArray(entity.attributes.entity_id)) {
  80.                     entity.attributes.entity_id.forEach(lightId => {
  81.                       let light = states[lightId];
  82.                       if (
  83.                         light &&
  84.                         light.state === 'on' &&
  85.                         light.attributes.rgb_color &&
  86.                         light.attributes.brightness > 0
  87.                       ) {
  88.                         valid.push(light.attributes.rgb_color);
  89.                       }
  90.                     });
  91.                   }
  92.                   return valid;
  93.                 }
  94.                 let validLights = getValidLights();
  95.                 if (validLights.length > 0) {
  96.                   let [r, g, b] = validLights[0];
  97.                   let dr = Math.floor(r * 0.3);
  98.                   let dg = Math.floor(g * 0.3);
  99.                   let db = Math.floor(b * 0.3);
  100.                   return `rgb(${dr}, ${dg}, ${db})`;
  101.                 }
  102.                 return "#393646";
  103.               ]]]
  104.           - top: 2px
  105.           - right: "-18px"
  106.           - height: 36px
  107.           - width: 36px
  108.           - border-radius: 100px
  109. styles:
  110.   grid:
  111.     - grid-template-areas: "\"n\" \"slider\""
  112.     - grid-template-columns: 1fr
  113.     - grid-template-rows: 1fr min-content min-content
  114.   card:
  115.     - background: |
  116.        [[[
  117.           // Get all valid lights (on, with rgb_color)
  118.           function getValidLights() {
  119.             let valid = [];
  120.             if (entity.attributes.entity_id && Array.isArray(entity.attributes.entity_id)) {
  121.               entity.attributes.entity_id.forEach(lightId => {
  122.                 let light = states[lightId];
  123.                 if (
  124.                   light &&
  125.                   light.state === 'on' &&
  126.                   light.attributes.rgb_color &&
  127.                   light.attributes.brightness > 0
  128.                 ) {
  129.                   valid.push(light.attributes.rgb_color);
  130.                 }
  131.               });
  132.             }
  133.             return valid;
  134.           }
  135.           // Check if all lights have the same colour
  136.           function isUniform(lights) {
  137.             if (lights.length === 0) return false;
  138.             return lights.every(c => c[0] === lights[0][0] && c[1] === lights[0][1] && c[2] === lights[0][2]);
  139.           }
  140.          
  141.           let validLights = getValidLights();
  142.          
  143.           if (validLights.length === 1 || (validLights.length > 1 && isUniform(validLights))) {
  144.             // Single (or uniform) light: use original darker gradient logic
  145.             let [r, g, b] = validLights[0];
  146.             let darkR = Math.min(255, Math.floor(r * 0.4));
  147.             let darkG = Math.min(255, Math.floor(g * 0.4));
  148.             let darkB = Math.min(255, Math.floor(b * 0.4));
  149.             let darkerR = Math.min(255, Math.floor(r * 0.5));
  150.             let darkerG = Math.min(255, Math.floor(g * 0.5));
  151.             let darkerB = Math.min(255, Math.floor(b * 0.5));
  152.             return `linear-gradient(to right, rgb(${darkR}, ${darkG}, ${darkB}), rgb(${darkerR}, ${darkerG}, ${darkerB}))`;
  153.           } else if (validLights.length > 1) {
  154.             // Multiple different colours: create darker tinted gradient from each light's colour
  155.             let stops = validLights.map(c => {
  156.               let r = Math.min(255, Math.floor(c[0] * 0.4));
  157.               let g = Math.min(255, Math.floor(c[1] * 0.4));
  158.               let b = Math.min(255, Math.floor(c[2] * 0.4));
  159.               return `rgb(${r}, ${g}, ${b})`;
  160.             });
  161.             return `linear-gradient(to right, ${stops.join(', ')})`;
  162.           }
  163.           // Fallback colour when off
  164.           return "#393646";
  165.         ]]]
  166.     - padding: 16px
  167.     - "--mdc-ripple-press-opacity": 0
  168.     - transition: background 0.5s ease-in-out
  169.   name:
  170.     - justify-self: start
  171.     - font-size: 14px
  172.     - margin: 4px 0 12px 0
  173.     - color: var(--contrast20)
  174.  
Tags: ha
Add Comment
Please, Sign In to add comment