Picture Elements - conditional type background color based on battery level

Hi,

I have an empty png file and want to change the background color based on the battery level of an entity. What should the condition look like to make it work? The way I tried it does not work.

- type: conditional
            conditions:
              - entity: sensor.hue_dimmer_switch_1_batterie
                state: '>5%'
            elements:
              - type: image
                image: /local/floorplan/leer.png
                tap_action: none
                hold_action: none
                style:
                  top: 34%
                  left: 36.85%
                  width: 70px
                  height: 70px
                  border-radius: 80px
                  background: '#93bf85'

Thanks for your help.

I’m using dynamic background for my person card. I use for this custom-config-template card with variable that changes name of picture de[pending on conditions (in my case name of zone). Here is part of card code responsible for this:

      - type: custom:config-template-card
        variables:
          # this is name of zone from iclud3 integration, it is later referred to as vars[0].
          # It could be 'Home', 'Mirek work', etc
          - states['sensor.mirek_icloud_iphone_zone_name'].attributes.zone_fname
        entities:
          - device_tracker.mirek_icloud_iphone
        card:
          type: picture-elements
          # this is backgroud picture element name defined using zone name defined previously
          # So this line will return '/local/Home.jpg' and such picture need to exist in www folder. Case sensitive!
          image: ${'/local/' + vars[0] + '.jpg'}
          elements:

So you can define several images with different colors and use them as background of picture element card. Tricky part probably would be to define additional template sensor, that would give this color name based on some battery level treshold, to be used as variable.

Placing a whole picture-elements inside config-template-card (CTC) is a very bad practice. Place a particular element (image overflowing the whole card) inside CTC instead.

This is my setup. I want to change the green circle to red, when the battery level is under 5%.

# Batteriestatus
  - title: Batteriestatus
    path: batteriestatus
    icon: mdi:battery-70
    type: panel
    subview: true
    cards:
      - type: picture-elements
        image: /local/floorplan/ipad_Batteriestatus.png
        elements:
  # Navigation
          - type: state-icon
            entity: input_button.visu_raumauswahl
            icon: mdi:home
            tap_action:
              action: navigate
              navigation_path: /dashboard-ipad/startseite
            style:
              top: 90%
              left: 6%
              transform: translate(-50%, -50%) scale(3, 3)
              '--paper-item-icon-color': black
              opacity: 0.2
  # Anzeige Batterielevel
    # Schalter Esstisch
          - type: conditional
            conditions:
              - entity: sensor.hue_dimmer_switch_1_batterie
                state: '>5%'
            elements:
              - type: image
                image: /local/floorplan/leer.png
                tap_action: none
                hold_action: none
                style:
                  top: 34%
                  left: 36.85%
                  width: 70px
                  height: 70px
                  border-radius: 80px
                  background: '#93bf85'
          - type: image
            image: /local/floorplan/leer.png
            tap_action: none
            hold_action: none
            style:
              top: 34%
              left: 36.85%
              width: 70px
              height: 70px
              border-radius: 40px
              background: '#93bf85'   
          - type: state-label
            entity: sensor.hue_dimmer_switch_1_batterie
            action: none
            tap_action: none
            hold_action: none
            style:
              top: 34%
              left: 37%
              font-size: 25px
              font-weight: bold
              color: black
              opacity: 1

Impossible, if background image is templated. Or element would need to be created, that covers entire area of the card, which might in return create issues with z-order of elements.
Otherwise I have several cards configured like that and have no problems whatsoever. What might be the implications of such setup?

As I already said - an element overflowing the whole card; ofc with a proper z-index (depends on you only).
Funny, I said clearly about an element and yet you say “impossible”.

Guess why this is not recommended by the author of CTC? Because changing of any of monitored entity will cause flickering of whole card.
But may be you have not declared ALL inner entities as monitored - then no wonder that you do not see a flickering; or these entities change rarely.

I had in mind very backgroung picture… responding too fast, thinking about alternatives later :slight_smile:

Well, I do observe some redraws of entire card (if this is what you mean by flickering) once in a while, but indeed data displayed in overlay elements is changing not too frequently, so not a big issue. Perhaps’ i’ll do some test rewriting my card and check the difference…

On CTC github there are registered issues (all closed as “nothing to fix”) with complains like “flooplan is flickering”, “floorplan is slow” etc.

The 3rd method: card-mod.

Sorry to interrupt your discussion, but I couldn’t use any of your comments for my problem. I would still need help with my code please.

Was the image element selected over a state-badge for a specific reason?

The following code would produce this and eliminate the sensor label overlay. I included code to change the background color to red if the sensor drops below 5%

image

  - type: state-badge
    entity: sensor.hue_dimmer_switch_1_batterie
    tap_action: none
    hold_action: none
    style:
      top: 34%
      left: 36.85%
      width: 70px
      height: 70px
      border-radius: 40px
    card_mod:
      style:
        ha-state-label-badge:
          $:
            ha-label-badge:
              $: |
                .badge-container .label-badge .label span {
                 color:white;
                 background-color: black;
                 }
        ha-state-label-badge $ ha-label-badge $ .badge-container: |
          .title {
            display: none;
           }
        .: |
          :host {
            --label-badge-background-color: {{ '#93bf85' if states(config.entity) | int >= 5 else 'red'}} ;
            --label-badge-text-color: black;
            --label-badge-red: {{ '#93bf85' if states(config.entity) | int >= 5 else 'red'}} ;
            font-weight:600 !important
          }
1 Like

I just rtied to rewrite the card following your suggestion, but it does not work. Image is just element of picture element card, not the card itself, so seems cannot be templated with CTC :frowning: Or I’m doing it wrong:

type: picture-elements
image: /local/overlay1.png
elements:
  - type: custom:config-template-card
    entities:
      - device_tracker.mirek_icloud_iphone_zone_name
    variables:
      - states['sensor.mirek_icloud_iphone_zone_name'].attributes.zone_fname
    card:
      type: image
      image: ${'/local/' + vars[0] + '.jpg'}
    style:
      top: 180px
      left: 50%
      width: 500px

This code generates error:
Screenshot 2024-05-04 at 12.48.34

This method though (templating other individual elements, like markdown) works fine (though does not makes sense, as markdown can take jinja templates directly).

Try “element”.

1 Like

Tested, works like a charm. Now I’ll try to oserve what is the impact on redrawing the card, will it filcker as it was in the past :slight_smile:

wow, super cool. Thanks. Works great. I tried to also change the code to only have a colored circle without the badge and the “%” behind the number to make it look like the rest and turn the border red when battery percentage is low. I failed yet. What should it look like? Also, how can I change the circle size? It does not work with height and width.

          - type: state-badge
            entity: sensor.hue_dimmer_switch_1_batterie
            tap_action: none
            hold_action: none
            style:
              top: 70%
              left: 40%
              width: 100px
              height: 100px
              border-radius: 50px
            card_mod:
              style:
                #ha-state-label-badge:
                #  $:
                 #   ha-label-badge:
                  #    $: |
                   #     .badge-container .label-badge .label span {
                    #    color:white;
                     #   background-color: black;
                      #  }
                ha-state-label-badge $ ha-label-badge $ .badge-container: |
                  .title {
                    display: none;
                  }
                .: |
                  :host {
                    --label-badge-background-color: '#93bf85'
                    --label-badge-border-color: {{ '#93bf85' if states(config.entity) | int >= 5 else 'red'}};
                    --label-badge-text-color: black;
                    --label-badge-red: {{ '#93bf85' if states(config.entity) | int >= 5 else 'red'}};
                    font-weight:800 !important
                  }

This is another circle which has only states but no percentage.

- type: image
            image: /local/floorplan/leer.png
            tap_action: none
            hold_action: none
            style:
              top: 67%
              left: 24%
              width: 70px
              height: 70px
              border-radius: 40px
              background: '#be8bea'

          - type: conditional
            conditions:
              - entity: binary_sensor.fenstergriffsensor_battery_3
                state: unavailable
            elements:
              - type: image
                image: /local/floorplan/leer.png
                tap_action: none
                hold_action: none
                style:
                  top: 67%
                  left: 24%
                  width: 70px
                  height: 70px
                  border-radius: 40px
                  border: 5px solid red

          - type: conditional
            conditions:
              - entity: binary_sensor.fenstergriffsensor_battery_3
                state: off
            elements:
              - type: state-label
                entity: binary_sensor.fenstergriffsensor_battery_3
                tap_action: none
                hold_action: none
                style:
                  top: 67%
                  left: 24%
                  font-size: 18px
                  font-weight: bold
                  color: black
                  opacity: 1
          
          - type: conditional
            conditions:
              - entity: binary_sensor.fenstergriffsensor_battery_3
                state: unavailable
            elements:
              - type: state-label
                entity: binary_sensor.fenstergriffsensor_battery_3
                tap_action: none
                hold_action: none
                style:
                  top: 67%
                  left: 24%
                  font-size: 9px
                  font-weight: bold
                  color: black
                  opacity: 1 

huge card mod thread → 1st post → link at the bottom → picture elements
Or - this tutorial.

1 Like