• Stars
    star
    386
  • Rank 110,786 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🔹Dynamically replace lovelace cards depending on occasion

state-switch

hacs_badge

Dynamically replace lovelace cards depending on occasion.

state-switch mov

Note in the animation above that the two browser windows have two different users logged in, which changes the rightmost card.

For installation instructions, see this guide.

Install state-switch.js as a module.

resources:
  - url: /local/state-switch.js
    type: module

Usage

type: custom:state-switch
entity: <entity>
default: <default>
transition: <transition>
transition_time: <transition_time>
states:
  <state 1>:
    <card 1>
  <state 2>:
    <card 2>
  ...

When the state of <entity> is <state 1>, <card 1> will be displayed, when it's <state 2>, <card 2> will be displayed.

If the state of <entity> doesn't match any <state>, the <card> for the <default> state will be displayed.

Options

  • <entity> Required An entity id or hash, user, group, deviceID, mediaquery
  • <default> State to use as default fallback
  • <state N> The state to match
  • <card N> Lovelace card configuration
  • <transition> Animated transition to use (slide-left, slide-right, swap-left, swap_right or flip). Default: none
  • <transition_time> The time for an animated transition in ms. Default: 500

State matching

entity_id

If the entity parameter is set to an entity id, which card is displayed will depend on the state of that entity.

cards:
  - type: entities
    entities:
      - input_select.home_mode
  - type: custom:state-switch
    entity: input_select.home_mode
    states:
      Home:
        type: vertical-stack
        cards:
          - type: entities
            title: Lights
            entities:
              - light.bed_light
              - light.ceiling_lights
              - light.kitchen_lights
          - type: picture-glance
            camera_image: camera.demo_camera
            entities: []
      Away:
        type: alarm-panel
        entity: alarm_control_panel.alarm
      Guests:
        type: glance
        title: Lights
        entities:
          - light.bed_light
          - light.ceiling_lights
          - light.kitchen_lights

Note that the words on and off are magic in yaml, so if the entity is e.g. a switch, you need to quote the keys in the states: mapping:

states:
  "on":
    type: markdown
    content:>
      Light is on
  "off":
    type: markdown
    content:>
      Light is off

hash

If the entity parameter is set to hash, which card is displayed will depend on the "hash" part of the current URL - i.e. whatever comes after an optional # symbol in the current page URL.

This allows for controlling the view on a browser-window to browser-window basis, and without needing a controlling entity.

- type: horizontal-stack
  cards:
    - type: entity-button
      entity: light.my_dummy
      tap_action:
        action: navigate
        navigation_path: "#p1"
    - type: entity-button
      entity: light.my_dummy
      tap_action:
        action: navigate
        navigation_path: "#p2"
    - type: entity-button
      entity: light.my_dummy
      tap_action:
        action: navigate
        navigation_path: "#p2"
- type: custom:state-switch
  entity: hash
  default: p1
  states:
    p1:
      type: markdown
      content: |
        # Page 1
    p2:
      type: markdown
      content: |
        # Page 2
    p3:
      type: markdown
      content: |
        # Page 3

user

If the entity parameter is set to user, which card is displayed will depend on the currently logged in users Display Name.

type: custom:state-switch
entity: user
default: default
states:
  A:
    type: entities
    title: User A stuff
    entities:
      - light.bed_light
      - light.ceiling_lights
      - light.kitchen_lights
  B:
    type: glance
    title: User B stuff
    entities:
      - light.bed_light
      - light.ceiling_lights
      - light.kitchen_lights
  default:
    type: markdown
    content: >
      ## Unknown user

group

group can take one of two values - admin or user depending on whether the currently logged in user is in the Administrators group or not.

deviceID

If the entity parameter is set to deviceID, which card is displayed will depend on the device-browser combination which is currently displaying the page.

See browser_mod for a description on how deviceIDs work.

type: custom:state-switch
entity: deviceID
states:
  "9c2aaf6f-ed26e3c1":
    type: markdown
    content: >
      Desktop
  "c8a4981c-d69c5e3c":
    type: markdown
    content: >
      Mobile

mediaquery

If the entity parameter is set to mediaquery, the card that is displayed will be the first one that matches a CSS @media rule.

Skärminspelning 2019-12-10 kl  21 18 12 mov

type: custom:state-switch
entity: mediaquery
states:
  "(min-width: 1000px)":
    type: markdown
    content: more than 1000 px
  "(min-width: 500px)":
    type: markdown
    content: 500 to 1000 px
  "all":
    type: markdown
    content: "Really small"

template

If the entity parameter is set to template, the card that is displayed will be the one that matches the jinja template's result.

type: custom:state-switch
entity: template
template: "{% if is_state('switch.night_mode', 'on') and now().weekday() < 5 %} day {% else %} night {% endif %}"
states:
  day:
    type: markdown
    content: Where do you want to go today?
  night:
    type: markdown
    content: Sleep tight!

You may also simply define the entity parameter as a jinja template string.

type: custom:state-switch
entity: "{% if is_state('switch.night_mode', 'on') and now().weekday() < 5 %} day {% else %} night {% endif %}"
states:
  day:
    type: markdown
    content: Where do you want to go today?
  night:
    type: markdown
    content: Sleep tight!

Note: Jinja2 templating is not cheap. Avoid it for simple things that can be solved with just an entity.

Transitions

The switch from one card to another can be animated by setting the <transition> option. The speed of the transition is set by <transition_time> (milliseconds). Note that some animations do two things, and thus take two times <transition_time> to complete.

Available transitions are:

  • slide-down
  • slide-up
  • slide-left
  • slide-right
  • swap-down
  • swap-up
  • swap-left
  • swap-right
  • flip-x
  • flip-y

jVbI15cMvT

A few tips


Buy Me A Coffee

More Repositories

1

hass-browser_mod

🔹 A Home Assistant integration to turn your browser into a controllable entity and media player
TypeScript
1,253
star
2

lovelace-auto-entities

🔹Automatically populate the entities-list of lovelace cards
TypeScript
1,190
star
3

lovelace-card-mod

🔹 Add CSS styles to (almost) any lovelace card
TypeScript
1,059
star
4

lovelace-layout-card

🔹 Get more control over the placement of lovelace cards.
TypeScript
1,015
star
5

lovelace-slider-entity-row

🔹 Add sliders to entity cards
TypeScript
801
star
6

lovelace-fold-entity-row

🔹 A foldable row for entities card, containing other rows
TypeScript
563
star
7

hass-fontawesome

🔹 Use icons from fontawesome in home-assistant
JavaScript
272
star
8

lovelace-card-tools

🔹A collection of tools for other lovelace plugins to use
JavaScript
244
star
9

lovelace-template-entity-row

🔹 Display whatever you want in an entities card row.
TypeScript
213
star
10

hass-config

My Home Assistant configuration
Python
210
star
11

hass-lovelace_gen

🔹 Improve the lovelace yaml parser for Home Assistant
Python
209
star
12

lovelace-more-info-card

🔹 Display the more-info dialog of any entity as a lovelace card
TypeScript
139
star
13

lovelace-hui-element

🔹 Use built-in elements in the wrong place
TypeScript
105
star
14

hass-favicon

🔹 Change the favicon of your Home Assistant instance
Python
101
star
15

lovelace-card-modder

JavaScript
101
star
16

lovelace-flower-card

JavaScript
99
star
17

lovelace-popup-card

JavaScript
94
star
18

hass-plejd

🔹 Plejd BLE integration for Home Assistant
Python
82
star
19

round-slider

JavaScript
81
star
20

lovelace-badge-card

🔹 Place badges anywhere in the lovelace layout
JavaScript
77
star
21

lovelace-toggle-lock-entity-row

JavaScript
62
star
22

lovelace-fullykiosk

Lovelace plugin for using Fully Kiosk Browser features in home-assistant
JavaScript
61
star
23

homeassistant-lovelace-gen

A lovelace configuration file generator for home assistant
Python
57
star
24

lovelace-player

Lets any browser currently viewing your lovelace interface act as an audio receiver with a media_player interface
JavaScript
50
star
25

mittos64

C
42
star
26

lovelace-theme-maker

JavaScript
42
star
27

lovelace-browser-commander

JavaScript
33
star
28

lovelace-useful-markdown-card

JavaScript
30
star
29

lovelace-gap-card

🔹 A lovelace card that does nothing and looks like nothing. Incredibly useful! No, really.
JavaScript
29
star
30

hass-custom-devcontainer

A devcontainer for developing and testing custom Home Assistant stuff
Shell
28
star
31

homeassistant-2020-presentation

18
star
32

lovelace-markdown-mod

JavaScript
16
star
33

lovelace-gui-sandbox

🔹 Lets you play around with the GUI editors even if you're using YAML mode
JavaScript
16
star
34

lovelace-card-loader

JavaScript
13
star
35

lovelace-color-picker

JavaScript
13
star
36

plejd2mqtt

JavaScript
12
star
37

lovelace-column-card

A columnizing card for lovelace ui for home-assistant.
JavaScript
12
star
38

card-tools

A collection of tools to help develop lovelace cards
JavaScript
11
star
39

os5

My attempts at developing an operating system kernel.
C
11
star
40

lovelace-dummy-entity-row

🔹 An entity row with only icon and name
JavaScript
10
star
41

lovelace-q-card

JavaScript
9
star
42

lovelace-long-press

JavaScript
9
star
43

lovelace-time-input-row

JavaScript
9
star
44

dito

A library and some tools for handling disk image files.
C
8
star
45

nextrevo-mod

How I use the E3D Revo system with Prusa Nextruder
7
star
46

mittsnap

rsnapshot-like tool that leverages btrfs snapshots
Shell
5
star
47

dotfiles

MATLAB
5
star
48

lovelace-color-glance-card

A glance card with a colorized background
JavaScript
3
star
49

lovelace-icon-headers

WIP
JavaScript
2
star
50

lovelace-wbah

Lovelace with bluejays and herons
TypeScript
2
star
51

os4

C
1
star
52

new-hass-config

Python
1
star
53

z80Monitor

Assembly
1
star
54

vim-tstatus

My status line for vim
Vim Script
1
star
55

pyplejd

Python
1
star
56

mittos64-old

C
1
star
57

hass-plant_helper

🔹 Home Assistant Helper for monitoring plant health using MiFlora devices
Python
1
star