Custom: button-card : mise en forme conditionnelle en fonction de l'état de 3 capteurs différents

Bonjour,

J’ai un problème d’écriture en yaml par rapport à une carte « custom:button-card »

Je voudrais que cette carte qui me permet d’ouvrir et fermer le garage indique l’état de la porte du garage en fonction de deux capteurs d’ouverture et d’un capteur de vibration.

Je m’explique :

  • j’ai sur ma porte de garage un capteur d’ouverture qui est fermé quand la porte est fermé, un autre capteur qui est fermé quand la porte est complètement ouverte et un capteur de vibration qui déclenché quand la porte est en mouvement.
  • actuellement, j’ai mis un custom_fields avec la propriété « name » égale à « (fermé) » écrit en vert statique (rgba) avec le canal alpha à 0 quand le capteur d’ouverture porte fermée est ouvert et le canal alpha à 255 quand ce capteur est fermé et un autre custom_fields qui fonctionne de la même manière (mais avec la propriété « name » égale à « (ouvert) » clignotant en rouge en fonction de l’état du capteur d’ouverture porte ouverte.

Je voudrais pouvoir n’avoir qu’un seul custom_fields m’indiquant 4 états possibles pour la porte du garage :

  • garage (fermé) avec "fermé écrit en vert fixe (état du capteur d’ouverture porte fermée = fermé)
  • garage (ouvert) avec « ouvert » clignotant en rouge (état du capteur d’ouverture porte ouverte = fermé)
  • garage (porte en mouvement) avec « porte en mouvement » cligotant en orange (états des deux capteurs d’ouverture = fermé et capteur de vibration activé)
  • garage (partiellement ouvert) avec « partiellement ouvert » écrit en orange fixe (états des deux capteurs d’ouverture = fermé et capteur de vibration inactif)

Ci-dessous le code de la carte actuel :

type: custom:mod-card
card:
  type: custom:layout-card
  layout_type: vertical
  cards:
    - type: custom:button-card
      aspect_ratio: 2/1
      custom_fields:
        fond:
          card:
            type: custom:button-card
            name: Maison
            styles:
              name:
                - font-size: 24px
                - font-weight: bold
                - color: black
                - justify-self: start
                - padding-left: 5%
              card:
                - background-color: rgba(255,255,255,0.0)
            tap_action:
              action: none
            hold_action:
              action: none
            style: |
              ha-card {
                box-shadow: none;
                border: none;
              }
        garage:
          card:
            type: custom:button-card
            name: Garage
            styles:
              name:
                - font-size: 16px
                - font-weight: bold
                - color: rgba(0,0,0,0.7)
              card:
                - background-color: rgba(255,255,255,0.0)
            tap_action:
              action: none
            hold_action:
              action: none
            style: |
              ha-card {
                box-shadow: none;
                border: none;
              }

        garage_ferme:
          card:
            type: custom:button-card
            entity: >-
              binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_fermee_contact
            show_icon: false
            name: (fermé)
            styles:
              name:
                - font-size: 16px
                - font-weight: bold
              card:
                - background-color: rgba(255,255,255,0.0)
            state:
              - value: 'off'
                styles:
                  name:
                    - color: rgba(0,128,0,0.7)
              - value: 'on'
                styles:
                  name:
                    - color: rgba(0,128,0,0.0)
            tap_action:
              action: none
            hold_action:
              action: none
            style: |
              ha-card {
                box-shadow: none;
                border: none;
              }
        garage_ouvert:
          card:
            type: custom:button-card
            entity: >-
              binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_ouverte_contact
            show_icon: false
            name: (ouvert)
            styles:
              name:
                - font-size: 16px
                - font-weight: bold
              card:
                - background-color: rgba(255,255,255,0.0)
            state:
              - value: 'off'
                styles:
                  name:
                    - color: rgba(255,0,0,0.7)
                    - animation: blink 2s ease infinite
              - value: 'on'
                styles:
                  name:
                    - color: rgba(255,0,0,0.0)
            tap_action:
              action: none
            hold_action:
              action: none
            style: |
              ha-card {
                box-shadow: none;
                border: none;
              }

        nlampe:
          card:
            type: custom:button-card
            name: Lampe extérieure
            styles:
              name:
                - font-size: 12px
                - color: black
              card:
                - background-color: rgba(255,255,255,0.0)
            tap_action:
              action: none
            hold_action:
              action: none
            style: |
              ha-card {
                box-shadow: none;
                border: none;
              }
        lampe:
          card:
            type: custom:button-card
            entity: light.entree_lampe_exterieure
            icon: phu:wall-fuzo
            show_name: false
            state:
              - value: 'off'
                styles:
                  icon:
                    - color: Dimgray
                  card:
                    - box-shadow: none
                    - background-color: rgba(242,242,242,0.5)
              - value: 'on'
                styles:
                  icon:
                    - color: rgb(255,255,128)
                  card:
                    - box-shadow: 0px 0px 6px 3px rgba(255,255,128,0.8)
                    - background-color: rgba(105,105,105,0.5)
        garageup:
          card:
            type: custom:button-card
            icon: mdi:arrow-up-bold-box
            show_name: false
            size: 70%
            styles:
              icon:
                - color: Dimgray
              card:
                - box-shadow: none
                - background-color: rgba(242,242,242,0.5)
            tap_action:
              action: call-service
              service: script.ouvrir_garage
        garagestop:
          card:
            type: custom:button-card
            icon: mdi:square-rounded
            show_name: false
            size: 70%
            styles:
              icon:
                - color: Dimgray
              card:
                - box-shadow: none
                - background-color: rgba(242,242,242,0.5)
            tap_action:
              action: call-service
              service: script.stopper_garage
        garagedown:
          card:
            type: custom:button-card
            icon: mdi:arrow-down-bold-box
            show_name: false
            size: 70%
            styles:
              icon:
                - color: Dimgray
              card:
                - box-shadow: none
                - background-color: rgba(242,242,242,0.5)
            tap_action:
              action: call-service
              service: script.fermer_garage
      styles:
        custom_fields:
          fond:
            - background-image: url(/local/images/maison.png)
            - position: absolute
            - left: 0
            - top: 0
            - width: 100%
            - height: 100%
            - background-size: cover
            - background-position: center
            - opacity: 0.7
          garage:
            - width: 15%
            - position: absolute
            - left: 50%
            - top: 10%
          garage_ferme:
            - width: 15%
            - position: absolute
            - left: 65%
            - top: 10%
          garage_ouvert:
            - width: 15%
            - position: absolute
            - left: 65%
            - top: 10%
          portail:
            - width: 15%
            - position: absolute
            - left: 50%
            - top: 60%
          nlampe:
            - width: 25%
            - position: absolute
            - left: 5%
            - top: 25%
          lampe:
            - width: 15%
            - position: absolute
            - left: 10%
            - top: 35%
          garageup:
            - width: 15%
            - position: absolute
            - left: 43%
            - top: 25%
          garagestop:
            - width: 15%
            - position: absolute
            - left: 60%
            - top: 25%
          garagedown:
            - width: 15%
            - position: absolute
            - left: 77%
            - top: 25%

image

Bon, je progresse : j’arrive maintenant à modifier l’affichage en fonction de l’état des capteurs

type: custom:mod-card
card:
  type: custom:layout-card
  layout_type: vertical
  cards:
    - type: custom:button-card
      aspect_ratio: 2/1
      custom_fields:
        fond:
          card:
            type: custom:button-card
            name: Maison
            styles:
              name:
                - font-size: 24px
                - font-weight: bold
                - color: black
                - justify-self: start
                - padding-left: 5%
              card:
                - background-color: rgba(255,255,255,0.0)
            tap_action:
              action: none
            hold_action:
              action: none
            style: |
              ha-card {
                box-shadow: none;
                border: none;
              }
        garage:
          card:
            type: custom:button-card
            name: Garage
            styles:
              name:
                - font-size: 16px
                - font-weight: bold
                - color: rgba(0,0,0,0.7)
              card:
                - background-color: rgba(255,255,255,0.0)
            tap_action:
              action: none
            hold_action:
              action: none
            style: |
              ha-card {
                box-shadow: none;
                border: none;
              }
        garage_pos:
          card:
            type: custom:button-card
            entity: input_number.pos_garage
            show_icon: false
            name: |
              [[[
                if (states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_fermee_contact'].state == 'off' && states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_ouverte_contact'].state == 'on')
                  return "(fermé)";
                else if (states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_fermee_contact'].state == 'on' && states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_ouverte_contact'].state == 'off')
                  return "(ouvert)";
                else if (states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_fermee_contact'].state == 'on' && states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_ouverte_contact'].state == 'on' && states['binary_sensor.garage_detecteur_de_vibrations_porte_vibration'].state == 'on')
                  return "(en mouvement)";
                else if (states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_fermee_contact'].state == 'on' && states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_ouverte_contact'].state == 'on' && states['binary_sensor.garage_detecteur_de_vibrations_porte_vibration'].state == 'off')
                  return "(partiellement ouvert)";
              ]]]
            styles:
              name:
                - font-size: 16px
                - font-weight: bold
                - color: green
              card:
                - background-color: rgba(255,255,255,0.0)
            tap_action:
              action: none
            hold_action:
              action: none
            style: |
              ha-card {
                box-shadow: none;
                border: none;
              }

        nlampe:
          card:
            type: custom:button-card
            name: Lampe extérieure
            styles:
              name:
                - font-size: 12px
                - color: black
              card:
                - background-color: rgba(255,255,255,0.0)
            tap_action:
              action: none
            hold_action:
              action: none
            style: |
              ha-card {
                box-shadow: none;
                border: none;
              }
        lampe:
          card:
            type: custom:button-card
            entity: light.entree_lampe_exterieure
            icon: phu:wall-fuzo
            show_name: false
            state:
              - value: 'off'
                styles:
                  icon:
                    - color: Dimgray
                  card:
                    - box-shadow: none
                    - background-color: rgba(242,242,242,0.5)
              - value: 'on'
                styles:
                  icon:
                    - color: rgb(255,255,128)
                  card:
                    - box-shadow: 0px 0px 6px 3px rgba(255,255,128,0.8)
                    - background-color: rgba(105,105,105,0.5)
        garageup:
          card:
            type: custom:button-card
            icon: mdi:arrow-up-bold-box
            show_name: false
            size: 70%
            styles:
              icon:
                - color: Dimgray
              card:
                - box-shadow: none
                - background-color: rgba(242,242,242,0.5)
            tap_action:
              action: call-service
              service: script.ouvrir_garage
        garagestop:
          card:
            type: custom:button-card
            icon: mdi:square-rounded
            show_name: false
            size: 70%
            styles:
              icon:
                - color: Dimgray
              card:
                - box-shadow: none
                - background-color: rgba(242,242,242,0.5)
            tap_action:
              action: call-service
              service: script.stopper_garage
        garagedown:
          card:
            type: custom:button-card
            icon: mdi:arrow-down-bold-box
            show_name: false
            size: 70%
            styles:
              icon:
                - color: Dimgray
              card:
                - box-shadow: none
                - background-color: rgba(242,242,242,0.5)
            tap_action:
              action: call-service
              service: script.fermer_garage
      styles:
        custom_fields:
          fond:
            - background-image: url(/local/images/maison.png)
            - position: absolute
            - left: 0
            - top: 0
            - width: 100%
            - height: 100%
            - background-size: cover
            - background-position: center
            - opacity: 0.7
          garage:
            - width: 15%
            - position: absolute
            - left: 50%
            - top: 10%
          garage_pos:
            - width: 15%
            - position: absolute
            - left: 65%
            - top: 10%
          nlampe:
            - width: 25%
            - position: absolute
            - left: 5%
            - top: 25%
          lampe:
            - width: 15%
            - position: absolute
            - left: 10%
            - top: 35%
          garageup:
            - width: 15%
            - position: absolute
            - left: 43%
            - top: 25%
          garagestop:
            - width: 15%
            - position: absolute
            - left: 60%
            - top: 25%
          garagedown:
            - width: 15%
            - position: absolute
            - left: 77%
            - top: 25%

Maintenant, je bloque avec le changement de couleur du texte…

Bonjour,
ta plusieur façon pour la couleur de l’état.
Avec state_display:, exemple:

            - type: custom:button-card
              entity: switch.0xa4c138c9cc1b6472
              name: M. à laver
              icon: mdi:washing-machine
              size: 110%
              template: device2
              state_display: |
                [[[
                  if (states['sensor.0xa4c138c9cc1b6472_power'].state > 3)
                  return '<font color= "green">Allumé</font>' ; else return '<font color= "red">Éteint</font>';
                ]]] 

ou par le styles:, exemple:

              styles:
                state:
                  - color: |
                      [[[
                        if (states['sensor.0xa4c138c9cc1b6472_power'].state > 3)
                        return "green"; else return "red";
                      ]]]

ou par le state:, exemple:

    state:
      - value: 'close'
        styles:
          state:
            - color: green
      - value: 'open'
        styles:
          state:
            - color: red

Merci WarC0zes mais j’ai réussi pendant que tu me donnais une solution. Ca fonctionne maintenant (changement de texte, de couleur de texte et clignotement quand la porte est en mouvement et quand elle est grande ouverte et ça en fonction de l’état des 3 capteurs - ouverture - fermeture et vibration) :

type: custom:mod-card
card:
  type: custom:layout-card
  layout_type: vertical
  cards:
    - type: custom:button-card
      aspect_ratio: 2/1
      custom_fields:
        fond:
          card:
            type: custom:button-card
            name: Maison
            styles:
              name:
                - font-size: 24px
                - font-weight: bold
                - color: black
                - justify-self: start
                - padding-left: 5%
              card:
                - background-color: rgba(255,255,255,0.0)
            tap_action:
              action: none
            hold_action:
              action: none
            style: |
              ha-card {
                box-shadow: none;
                border: none;
              }
        garage:
          card:
            type: custom:button-card
            name: Garage
            styles:
              name:
                - font-size: 16px
                - font-weight: bold
                - color: rgba(0,0,0,0.7)
              card:
                - background-color: rgba(255,255,255,0.0)
            tap_action:
              action: none
            hold_action:
              action: none
            style: |
              ha-card {
                box-shadow: none;
                border: none;
              }
        garage_pos:
          card:
            type: custom:button-card
            entity: input_number.pos_garage
            show_icon: false
            name: |
              [[[
                if (states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_fermee_contact'].state == 'off' && states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_ouverte_contact'].state == 'on')
                  return "(fermé)";
                else if (states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_fermee_contact'].state == 'on' && states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_ouverte_contact'].state == 'off')
                  return "(ouvert)";
                else if (states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_fermee_contact'].state == 'on' && states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_ouverte_contact'].state == 'on' && states['binary_sensor.garage_detecteur_de_vibrations_porte_vibration'].state == 'on')
                  return "(en mouvement)";
                else if (states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_fermee_contact'].state == 'on' && states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_ouverte_contact'].state == 'on' && states['binary_sensor.garage_detecteur_de_vibrations_porte_vibration'].state == 'off')
                  return "(partiellement ouvert)";
              ]]]
            styles:
              name:
                - font-size: 16px
                - font-weight: bold
                - justify-self: start
                - color: |
                    [[[
                      if (states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_fermee_contact'].state == 'off' && states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_ouverte_contact'].state == 'on')
                        return 'rgba(0,128,0,0.7)';
                      else if (states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_fermee_contact'].state == 'on' && states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_ouverte_contact'].state == 'off')
                        return 'rgba(255,0,0,0.7)';
                      else if (states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_fermee_contact'].state == 'on' && states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_ouverte_contact'].state == 'on' && states['binary_sensor.garage_detecteur_de_vibrations_porte_vibration'].state == 'on')
                        return 'rgba(255,140,0,0.7)';
                      else if (states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_fermee_contact'].state == 'on' && states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_ouverte_contact'].state == 'on' && states['binary_sensor.garage_detecteur_de_vibrations_porte_vibration'].state == 'off')
                        return 'rgba(255,140,0,0.7)';
                    ]]]
                - animation: |
                    [[[
                      if (states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_fermee_contact'].state == 'on' && states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_ouverte_contact'].state == 'off')
                        return 'blink 2s ease infinite';
                      else if (states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_fermee_contact'].state == 'on' && states['binary_sensor.garage_detecteur_d_ouverture_porte_du_garage_ouverte_contact'].state == 'on' && states['binary_sensor.garage_detecteur_de_vibrations_porte_vibration'].state == 'on')
                        return 'blink 2s ease infinite';
                    ]]]
              card:
                - background-color: rgba(255,255,255,0.0)
            tap_action:
              action: none
            hold_action:
              action: none
            style: |
              ha-card {
                box-shadow: none;
                border: none;
              }
        nlampe:
          card:
            type: custom:button-card
            name: Lampe extérieure
            styles:
              name:
                - font-size: 12px
                - color: black
              card:
                - background-color: rgba(255,255,255,0.0)
            tap_action:
              action: none
            hold_action:
              action: none
            style: |
              ha-card {
                box-shadow: none;
                border: none;
              }
        lampe:
          card:
            type: custom:button-card
            entity: light.entree_lampe_exterieure
            icon: phu:wall-fuzo
            show_name: false
            state:
              - value: 'off'
                styles:
                  icon:
                    - color: Dimgray
                  card:
                    - box-shadow: none
                    - background-color: rgba(242,242,242,0.5)
              - value: 'on'
                styles:
                  icon:
                    - color: rgb(255,255,128)
                  card:
                    - box-shadow: 0px 0px 6px 3px rgba(255,255,128,0.8)
                    - background-color: rgba(105,105,105,0.5)
        garageup:
          card:
            type: custom:button-card
            icon: mdi:arrow-up-bold-box
            show_name: false
            size: 70%
            styles:
              icon:
                - color: Dimgray
              card:
                - box-shadow: none
                - background-color: rgba(242,242,242,0.5)
            tap_action:
              action: call-service
              service: script.ouvrir_garage
        garagestop:
          card:
            type: custom:button-card
            icon: mdi:square-rounded
            show_name: false
            size: 70%
            styles:
              icon:
                - color: Dimgray
              card:
                - box-shadow: none
                - background-color: rgba(242,242,242,0.5)
            tap_action:
              action: call-service
              service: script.stopper_garage
        garagedown:
          card:
            type: custom:button-card
            icon: mdi:arrow-down-bold-box
            show_name: false
            size: 70%
            styles:
              icon:
                - color: Dimgray
              card:
                - box-shadow: none
                - background-color: rgba(242,242,242,0.5)
            tap_action:
              action: call-service
              service: script.fermer_garage
      styles:
        custom_fields:
          fond:
            - background-image: url(/local/images/maison.png)
            - position: absolute
            - left: 0
            - top: 0
            - width: 100%
            - height: 100%
            - background-size: cover
            - background-position: center
            - opacity: 0.7
          garage:
            - width: 15%
            - position: absolute
            - left: 50%
            - top: 10%
          garage_pos:
            - width: 25%
            - position: absolute
            - left: 65%
            - top: 10%
          nlampe:
            - width: 25%
            - position: absolute
            - left: 5%
            - top: 25%
          lampe:
            - width: 15%
            - position: absolute
            - left: 10%
            - top: 35%
          garageup:
            - width: 15%
            - position: absolute
            - left: 43%
            - top: 25%
          garagestop:
            - width: 15%
            - position: absolute
            - left: 60%
            - top: 25%
          garagedown:
            - width: 15%
            - position: absolute
            - left: 77%
            - top: 25%

image

3 « J'aime »

Bravo

Code de carte finale et visuel final pour que chacun puisse se projeter sur le rendu c’est top :+1: