CONDITIONAL STYLING

Conditional styling allows for simple dynamic styling of the header, buttons, and tabs depending on an entity's state. For more complex conditions (multiple conditions for one element, using entity attributes, dynamically hiding tabs, etc.) use conditional styling templates.




Example:

  conditional_styles:
    - entity: input_boolean.boolean1    # entity to watch
      condition:                     
        state: "off"                    # condition to match
      button:                           # type of item to style
        menu:                           # item to style
          color: yellow                 # what to style



Entity:

Condition:

Items to style

Styling


Full Example:

cch:
  conditional_styles:
    - entity: input_boolean.boolean1
      condition:
        state: off                      # When input_boolean is off...
      background: url('/local/bg.jpg')  # change background image and...
      tab:                              # change color and icon of tab 3
        3:
          on_icon: mdi:death-star
          off_icon: mdi:death-star-variant
          color: yellow
    - entity: notifications
      condition:
        state: false      # When there are no notifications...
      button:             # hide notifications button.
        notifications:
          hide: true
    - entity: input_number.slider1
      condition:          # When slider is between 10 & 20...
        above: 10
        below: 20
      tab:                # change color and icon of tab 0.
        0:
          color: yellow
          off_icon: mdi:emoticon-dead
          on_icon: mdi:emoticon-excited



Conditional Styling Templates

You can use JavaScript for complex conditional styling of header background, tabs, and buttons. Tabs and buttons can style icon, color, and display (display will show or hide buttons or tabs).

All templates must return a string.

Formatting is VERY important here.

Example:

This is just an example, you can use any valid JavaScript (return statements, ternaries, etc. would work fine as well).

cch:
  conditional_styles:
    template:
      - background: >
          if (states["input_boolean.living_room"].state == "off") "#000";
          else if (states["input_boolean.living_room"].state == "on") "url('/local/bg.jpg')";
      - tab:
          3:
          - icon: >
              if (states["device_tracker.galaxys8"].state == "home") "mdi:account";
              else "mdi:account-off";
          - color: >  # Color for tab or button icon.
              if (states["device_tracker.galaxys8"].state == "home") "blue";
              else "#ffffff";
      - tab:
          'media':
            display: >  # Show or hide buttons or tabs.
              if (states["media_player.living_room"].state == "off") "hide";
              else if (states["media_player.living_room"].state == "playing") "show";
      - button:
          menu:
            icon: >
              if (states["device_tracker.galaxys8"].state == "home") "blue";
              else if (states["device_tracker.galaxys8"].state == "not_home") "#ffffff";
      - button:
          notifications:
            display: >
              if (notifications) "show";
              else "hide";