Cześć
Podzielę się z Wami dopracowaną, minimalistyczną i niezwykle funkcjonalną kartą do zarządzania oświetleniem (np. w salonie),
opartą na niezastąpionym Piotras Smart Button (custom:piotras-smart-button).
Karta została zaprojektowana tak, aby w widoku głównym pokazywać zbiorczy status i grupowe włączenie/wyłączenie światła, a po kliknięciu w dany punkt świetlny rozwijać nowoczesny widok szczegółowy z wyborem jasności, barwy i szybkim powrotem.
Najważniejsze funkcje:
- Dynamiczny widok główny i szczegółowy: Przełączanie między widokiem zbiorczym a sterowaniem konkretną lampą jednym dotknięciem.
- Automatyczny powrót (Auto-back): Karta po określonym czasie bezczynności (np. 10 sekund) sama wraca do widoku głównego.
- Inteligentne wykrywanie możliwości: Skrypt sam sprawdza, czy dana encja obsługuje ściemnianie (dimmer), czy ma pełną paletę RGB lub temperaturę barwową (Kelwiny) i dynamicznie dopasowuje przyciski!
- W pełni konfigurowalna tablica: Wszystkie nazwy, ikony i encje zdefiniowane są w przejrzystej sekcji na samej górze – łatwo przetłumaczysz ją na dowolny język lub dostosujesz pod swój dom.
- Kolory żarówek na żywo: Ikony dynamicznie zmieniają barwę w zależności od stanu i koloru światła.
Kod karty (YAML)
type: custom:piotras-smart-button
custom_data:
skrypt: |-
p{{{
// ===== SEKCJA KONFIGURACYJNA DLA UŻYTKOWNIKA =====
const TITLE = 'Salon Oświetlenie'; // Tytuł karty (możesz zmienić na swój język)
const TEXT_CANCEL = 'Wstecz'; // Tekst przycisku powrotu (np. Cancel, Wstecz, Zamknij)
const AUTO_BACK_SEC = 10; // Po ilu sekundach brak aktywności wraca do widoku głównego
const LAMPS = [
{ id: 'light.salon_glowne', name: 'Główne', icon: 'mdi:lightbulb-group' },
{ id: 'light.salon_kinkiet', name: 'Kinkiet', icon: 'mdi:wall-sconce-flat' },
{ id: 'light.salon_led', name: 'Taśma LED', icon: 'mdi:led-strip' }
];
// ==========================================
const IDS = [...new Set(LAMPS.map(l => l.id))]; // Każda encja raz (dla przycisku grupy)
const LIMIT = AUTO_BACK_SEC * 1000;
const now = () => Date.now();
if (card._subView === undefined) card._subView = 'main'; // 'main' albo numer lampy
if (card._timer === undefined) card._timer = null;
const disarm = () => { if (card._timer) { clearTimeout(card._timer); card._timer = null; } };
const arm = () => {
disarm();
const left = Math.max(0, LIMIT - (now() - (card._lastAct || 0)));
card._timer = setTimeout(() => {
card._timer = null;
if (!card.isConnected || card._subView === 'main') return;
if (now() - (card._lastAct || 0) >= LIMIT) { card._subView = 'main'; card._updateState(); }
else arm(); // w międzyczasie było dotknięcie - czekamy dalej
}, left + 20);
};
const touch = () => { card._lastAct = now(); arm(); };
const goMain = () => { disarm(); card._subView = 'main'; card._updateState(); };
const open = i => () => { card._subView = i; touch(); card._updateState(); };
if (card._subView !== 'main') {
if (!card._lastAct) card._lastAct = now();
if (now() - card._lastAct >= LIMIT) { disarm(); card._subView = 'main'; }
else if (!card._timer) arm(); // timer zgubiony - uzbrój bez resetu odliczania
}
const pobierzKolor = (entityId) => {
const s = states[entityId];
if (s?.state !== 'on') return '#757575'; // wyłączona -> szary
const rgb = s?.attributes?.rgb_color;
if (rgb && rgb.length === 3) return `rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]})`;
return '#ffca28'; // włączona bez RGB -> żółty
};
if (card._subView !== 'main') {
const lamp = LAMPS[card._subView] || LAMPS[0];
const id = lamp.id;
const s = states[id];
const stan = s?.state;
const attrs = s?.attributes || {};
const modes = attrs.supported_color_modes || [];
const canDim = modes.length ? !modes.every(m => m === 'onoff') : true;
const hasRgb = modes.length ? modes.some(m => ['rgb', 'rgbw', 'rgbww', 'hs', 'xy'].includes(m)) : true;
const hasCt = modes.includes('color_temp');
const pct = typeof attrs.brightness === 'number' ? Math.round(attrs.brightness / 2.55) : null;
const etykieta = stan === 'on' ? ((canDim && pct !== null) ? pct + '%' : 'ON') : stan === 'off' ? 'OFF' : '—';
const kolorIkony = pobierzKolor(id);
const call = (service, data) => () => { touch(); card._hass.callService('light', service, { entity_id: id, ...(data || {}) }); };
const actToggle = customTap.id_js({ action: call('toggle') });
const dim = p => customTap.id_js({ action: call('turn_on', { brightness_pct: p }) });
const warm = customTap.id_js({ action: call('turn_on', hasRgb ? { rgb_color: [255, 188, 117] } : { color_temp_kelvin: 2700 }) });
const cold = customTap.id_js({ action: call('turn_on', hasRgb ? { rgb_color: [204, 224, 255] } : { color_temp_kelvin: 6500 }) });
const actCancel = customTap.id_js({ action: goMain });
const rowDim = canDim ? `
<div style="display: flex; gap: 6px; justify-content: center; margin-bottom: 6px;">
<span ${dim(30)} style="cursor: pointer; padding: 2px 6px; font-size: 11px; background: rgba(255,255,255,0.1); border-radius: 3px;">30%</span>
<span ${dim(70)} style="cursor: pointer; padding: 2px 6px; font-size: 11px; background: rgba(255,255,255,0.1); border-radius: 3px;">70%</span>
<span ${dim(100)} style="cursor: pointer; padding: 2px 6px; font-size: 11px; background: rgba(255,255,255,0.1); border-radius: 3px;">100%</span>
</div>` : '';
const warmBtn = (hasRgb || hasCt) ? `<span ${warm} style="cursor: pointer; width: 14px; height: 14px; background: #ffbc75; border-radius: 50%; border: 1px solid #fff;"></span>` : '';
const coldBtn = (hasRgb || hasCt) ? `<span ${cold} style="cursor: pointer; width: 14px; height: 14px; background: #cce0ff; border-radius: 50%; border: 1px solid #fff;"></span>` : '';
return {
name: `
<div style="display: flex; justify-content: center; margin-bottom: 5px;">
<ha-icon icon="mdi:lightbulb" style="color: ${kolorIkony}; --mdc-icon-size: 28px; pointer-events: none;"></ha-icon>
</div>
<div style="font-weight: bold; font-size: 14px; text-align: center; margin-bottom: 10px;">${lamp.name}: ${etykieta}</div>
${rowDim}
<div style="display: flex; gap: 12px; justify-content: center; align-items: center; margin-bottom: 12px;">
${warmBtn}
<span ${actToggle} style="cursor: pointer; font-size: 12px; font-weight: bold; background: rgba(255,255,255,0.15); padding: 1px 6px; border-radius: 3px;">ON/OFF</span>
${coldBtn}
</div>
<div style="text-align: center;">
<span ${actCancel} style="cursor: pointer; color: #aaa; font-size: 11px; font-weight: bold; border: 1px solid #aaa; padding: 2px 8px; border-radius: 4px;">${TEXT_CANCEL}</span>
</div>
`
};
}
const anyOn = IDS.some(i => states[i]?.state === 'on');
const glownaIkonaKolor = anyOn ? '#ffca28' : '#757575';
const glownaIkonaTyp = anyOn ? 'mdi:lightbulb-group' : 'mdi:lightbulb-group-off';
const actToggleAll = customTap.id_js({
action: () => { card._hass.callService('light', anyOn ? 'turn_off' : 'turn_on', { entity_id: IDS }); }
});
const tiles = LAMPS.map((l, i) => `
<span ${customTap.id_js({ action: open(i) })} style="cursor: pointer; padding: 4px; border-radius: 4px; background: rgba(255,255,255,0.05);">
<ha-icon icon="${l.icon}" style="color: ${pobierzKolor(l.id)}; pointer-events: none;"></ha-icon>
</span>`).join('');
return {
name: `
<div style="text-align: center; margin-bottom: 3px;">
<span ${actToggleAll} style="cursor: pointer; padding: 6px; display: inline-block;">
<ha-icon icon="${glownaIkonaTyp}" style="color: ${glownaIkonaKolor}; --mdc-icon-size: 50px; pointer-events: none;"></ha-icon>
</span>
</div>
<div style="font-weight: bold; font-size: 15px; text-align: center; margin-bottom: 15px;">${TITLE}</div>
<div style="display: flex; gap: 14px; justify-content: center; margin-top: 6px;">${tiles}
</div>
`
};
}}}q
name: customData.skrypt.name
show_state: false
show_icon: false
tap_action:
action: none
card_width: 200
card_height: 160
name_mode: 2
background_color1: auto
show_press_effect: false
border_width: 1
show_shadow: false
text_shadow: false
text_color: auto
Wymagania:
- Zainstalowana karta Piotras Smart Button (
custom:piotras-smart-button) przez HACS. - 🔘 Piotras Smart Button — karta do sterowania prawie wszystkim - #15 przez Piotras
- Własne encje oświetlenia podmienione w tablicy
LAMPSna początku kodu.
Dajcie znać, jak Wam się podoba i czy chcielibyście zobaczyć wersję z płynnym suwakiem jasności zamiast przycisków procentowych! ![]()



