flex-table-card icon indicating copy to clipboard operation
flex-table-card copied to clipboard

SHARE YOUR FORMATTER - your `modify:` line everybody should have!

Open daringer opened this issue 2 years ago • 9 comments

Just today the fmt property was introduces and even 2 lines documentation made it into the repository, BUT there is just an shameful small amount of formatters available. Now it's your turn, share your favorite modify: lines, or even better, directly put them into the right spot and make a pull-request - but if course, feel also free to share yours inside this issue:

  • relevant code piece: https://github.com/custom-cards/flex-table-card/blob/52ca1b73c2b77fd8b7c827505b955b8f24208b7b/flex-table-card.js#L53-L75 I'ts really that easy, just put your modify line there, submit a PR so that your fellow homeassistant collegues will benefit from each other!
  • Alternatively share it directly here, maybe someone else will introduce them on the way :dancers:

daringer avatar Apr 20 '22 18:04 daringer

A simple table which shows if core/ os/ supervisor/ addon updates are available (including, via HACS).

type: custom:flex-table-card
sort_by:
  - state+
  - friendly_name+
entities:
  include:
    - update.*
    - sensor.hacs
columns:
  - name: Name
    data: friendly_name
    modify: x.split(":")[0];
  - name: Update
    data: state
    modify: x.replace("on", "Yes"),x.replace("off", "No")

Result looks like this:

image

I would like to modify it so that the state can also replace 0 (from HACS) with "No" but it looks like modify only allows 2 modifiers for now.

Would also like to modify it so that rows with update other than No are highlighted but this cannot be done currently.

illuzn avatar May 24 '22 06:05 illuzn

@illuzn

I too would like to highlight rows based on content, as for your states would

  - name: Update
    data: state
    modify: ( x == 1 || x == "on" ? '<div style="color:yellow;">Yes</div>' :"No")

work for you?

edited: to add colour to the text :) Highlight the cell text "Yes" in yellow.

Carl

carlywarly avatar Jul 26 '22 20:07 carlywarly

~~Here is a modifier that converts a value that contains seconds into a duration format (hh:mm:ss), eg: 1:23:45 I would love it if this could be a standard modifier.~~

~~modify: >- let y = (x > 3600) ? Math.floor(x / 3600).toString() + ':' : ''; let m = (x > 60) ? Math.floor((x % 3600) / 60).toString().padStart(2, 0) + ':' : ''; let s = (x > 0) ? Math.floor((x % 3600) % 60).toString() : ''; if (m) s = s.padStart(2, 0); y + m + s;~~

UPDATE: I put a separate ticket for this: #85

Veejer avatar Sep 09 '22 13:09 Veejer

How to get a date in a format "DD/MM/YYYY hh:mm:ss" from a timestamp value:

    - name: last_changed
      data: last_changed
      modify: |-
        if(x.length == 0){"-"} else {
          var date = new Date(x);
          date.getDate()+"/"+
          (String(date.getMonth()+ 1).padStart(2,'0'))+"/"+
          date.getFullYear()+" "+
          String(date.getHours()).padStart(2,'0')+":"+
          String(date.getMinutes()).padStart(2,'0')+":"+
          date.getSeconds()
        }

image

ildar170975 avatar Sep 14 '22 18:09 ildar170975

How to create a weblink from an attribute:

    - name: object
      data: mapurl
      modify: '''<a href="'' + x + ''">Route</a>'''

image

ildar170975 avatar Sep 14 '22 18:09 ildar170975

How to draw batteries:

  - name: battery
    data: state
    modify: >-
      function getColor(aLevel) {
        if ((parseFloat(aLevel)||0) == 0)
          return 'brown';
        else
        {
          if (parseFloat(aLevel) <= 33)
            return 'red';
          else
          if (parseFloat(aLevel) <= 66)
            return 'rgb(250,218,67)';
          else
            return 'green';
        }
      }
      function getBatteryIcon(aLevel) {
        var icon;
        if ((parseFloat(aLevel)||0) == 0)
          icon = 'mdi:battery-alert';
        else if (aLevel == 100)
          icon = "mdi:battery";
        else if (aLevel >= 90)
          icon = "mdi:battery-90";
        else if (aLevel >= 80)
          icon = "mdi:battery-80";
        else if (aLevel >= 70)
          icon = "mdi:battery-70";
        else if (aLevel >= 60)
          icon = "mdi:battery-60";
        else if (aLevel >= 50)
          icon = "mdi:battery-50";
        else if (aLevel >= 40)
          icon = "mdi:battery-40";
        else if (aLevel >= 30)
          icon = "mdi:battery-30";
        else if (aLevel >= 20)
          icon = "mdi:battery-20";
        else if (aLevel >= 10)
          icon = "mdi:battery-10";
        else
          icon = "mdi:battery-outline";
        return (icon);
      }
      '<ha-icon icon="' + getBatteryIcon(x) + '" style="width: 20px; height: 20px; color: ' + getColor(x) + ';"></ha-icon>
      <span style="color: '+ getColor(x) +';">' + x + '%</span>'

image

ildar170975 avatar Sep 14 '22 18:09 ildar170975

Here's one I'm using with Environment Canada integration for a daily forecast:

image


  - name: ' '
    align: left
    icon: mdi:weather-cloudy-clock
    data: forecast
    modify: >-
      function getWeatherData(condition) {
          /*console.log('condition', condition);*/
          var weather_data = {
              sunny: {
                  name: 'Sunny',
                  colour: 'yellow',
                  icon: 'mdi:weather-sunny',
              },
              cloudy: {
                  name: 'Cloudy',
                  colour: 'CadetBlue',
                  icon: 'mdi:weather-cloudy',
              },
              fog: {
                  name: 'Foggy',
                  colour: 'Gainsboro',
                  icon: 'mdi:weather-fog',
              },
              hail: {
                  name: 'Hail',
                  colour: 'SlateGray',
                  icon: 'mdi:weather-hail',
              },
              partlycloudy: {
                  name: 'Partly Cloudy',
                  colour: 'LightSlateGray',
                  icon: 'mdi:weather-partly-cloudy',
              },
              partlyrainy: {
                  name: 'Partly Rainy',
                  colour: 'CornflowerBlue',
                  icon: 'mdi:weather-partly-rainy',
              },
              partlysnowy: {
                  name: 'Partly Snowy',
                  colour: 'LightCyan',
                  icon: 'mdi:weather-partly-snowy',
              },
              rainy: {
                  name: 'Rainy',
                  colour: 'RoyalBlue',
                  icon: 'mdi:weather-rainy',
              },
              snowy: {
                  name: 'snowy',
                  colour: 'PowderBlue',
                  icon: 'mdi:weather-snowy',
              },
              windy: {
                  name: 'windy',
                  colour: '',
                  icon: 'mdi:weather-windy',
              },
              _undefined: {
                  name: 'undefined',
                  colour: '',
                  icon: 'mdi:alert',
              },
              _unknown: {
                  name: condition,
                  colour: 'Red',
                  icon: 'mdi:help',
              },
          };
          if (undefined === condition) {
              return weather_data._undefined;
          } else if (condition in weather_data) {
              return weather_data[condition];
          } else {
              return weather_data._unknown;
          }
      }
      function getName(condition) {
          return getWeatherData(condition).name;
      }
      function getColor(condition) {
          return getWeatherData(condition).colour;
      }
      function getIcon(condition) {
        return getWeatherData(condition).icon;
      }
      function getPrecipitationProbability(probability) {
        if(undefined === probability){
          return ' ';
        }
        if(0 === probability){
          return ' ';
        }
        else{
          return probability + '%';
        }
      }
      '<ha-icon icon="' + getIcon(x.condition) + '" style="width: 20px; height: 20px; color: ' + getColor(x.condition) +';"></ha-icon>' +
      ' <span style="font-size:0.75rem;color:'+getColor(x.condition)+';display:inline-block;width:30px;">' + getPrecipitationProbability(x.precipitation_probability) + '</span>'+
      ' <span>' + getName(x.condition) + '</span>'


teskanoo avatar Jun 10 '23 23:06 teskanoo

Using the Anniversaries (https://github.com/pinkywafer/Anniversaries) integration for a list of birthdays ...

This gives name, birthdate (month and day), current age, and days until their next birthday.

      - type: custom:flex-table-card
        title: Birthdays
        entities:
          include: sensor.anniversary_*bday
        sort_by: friendly_name+
        columns:
          - name: Name
            data: friendly_name
            modify: x.replace(' BDay', '')
          - name: Birthdate
            data: date
            modify: >-
              var date = new Date(x);
              const months = ['January ','February ','March ','April ','May ','June ','July ','August ','September ','October ','November ','December '];
              months[date.getMonth()] + String(date.getDate())
          - name: Age
            data: current_years
            align: right
          - name: Days
            data: state
            align: right

image

JeffCrum1 avatar Nov 04 '23 17:11 JeffCrum1

modify: x.replace("on", "Yes"),x.replace("off", "No")

I would like to modify it so that the state can also replace 0 (from HACS) with "No" but it looks like modify only allows 2 modifiers for now.

If I am not mistaken the second x.replace should be simply attached to the first like modify: x.replace("eQ-3 ", "").replace(" Climate", "")

So replace the ,x. with .. This will replace both string partials and I think you can add as many as you like. At least I was able to just add more like modify: x.replace("eQ-3 ", "").replace(" Climate", "").replace("Living", "")

AleXSR700 avatar Dec 21 '23 21:12 AleXSR700