I want to use the flex-table-card to display fuel prices:
(This is an example, I will have more lines)
But I would like the second column to show the address in lower case and the city (the sensors have a city attribute). Something like - data: address | lower + ", " + city | capitalize
← obviously my syntax is wrong otherwise I wouldn’t post here but you get the idea
Here is the current full yaml:
type: custom:flex-table-card
sort_by: state+
clickable: true
entities:
include:
- sensor.station_carrefour_market_sp95
- sensor.station_carrefour_market_sp98
- sensor.station_carrefour_market_e10
- sensor.station_auchan_e10
- sensor.station_auchan_sp98
columns:
- data: entity_picture
align: center
name: ""
# icon: mdi:gas-station
modify: "'<img src=\"' + x + '\"style=\"height: 35px\">'"
- data: address
align: left
name: ""
- icon: mdi:currency-eur
data: state
name: ""
align: center
- icon: mdi:calendar-clock
name: ""
data: days_since_last_update
align: center
prefix: J+
css:
tbody tr:nth-child(1): "color: #00C62D; font-weight: bold"
tbody tr:nth-last-child(1): "color: #dd2c00"
Any suggestion would be appreciated, even a new card if it has an easier syntax.
EDIT : I looked for answers for hours yesterday but found a partial solution today right after posting (I posted it below). I still need help with the lower case (or, better, capitalizing)
I found how to concatenate two attributes: you just need to separate data sources with a comma : - data: address, city
And you can choose the delimeter with multi_delimiter: ", "
It works but I still need to convert the address to lower case
data: address
modify: x.toLowerCase()
Thanks!
So it has to apply to all the data, I can’t have the address in lower case and the city capitilized?
Reminder, my data for that column is:
- data: address,city
multi_delimiter: ", "
- name: nom station
data: name, address, city
multi_delimiter: ','
modify: x.split(',')[0] + ", " + x.split(',')[1].toLowerCase() + ", " + x.split(',')[2].toUpperCase()
Almost but capitalizing means making the first character uppercase (and the rest lowercase). It should show “Gerzat”, not “GERZAT”
So I stole your code and did this:
data: address,city
multi_delimiter: ", "
modify: x.split(',')[0].toLowerCase() + ", " + x.split(',')[1].charAt(1).toUpperCase() + x.split(',')[1].slice(2).toLowerCase()
Which works 90%. The only issue is that I should get “Clermont-Ferrand” instead of “Clermont-ferrand”. So I tried
modify: >-
x.split(',')[0].toLowerCase() + ", " +
x.split(',')[1].charAt(1).toUpperCase() +
x.split(',')[1].slice(2).toLowerCase().split('-')[0] + "-" +
x.split(',')[1].slice(2).toUpperCase().split('-')[1].charAt(1) +
x.split(',')[1].slice(2).toLowerCase().split('-')[1].slice(1)
Which doesn’t work
I know… but you can do some research yourselves, it is just javascript
And btw…this integration delivers the city already capitalized so not sure what you are looking for
I did, hence the
x.split(',')[1].charAt(1).toUpperCase() +
x.split(',')[1].slice(2).toLowerCase().split('-')[0]
Yeah, I’m probably going to leave the city alone and convert only the address. Or test if the address is in upperCase and convert it only if that’s the case
You do not need to test, you can always convert to lowercase, testing adds more complexity
Most addresses are properly formatted with caps where they need to be. So I would ideally like to leave them alone and convert to lower case only the addresses that are entirely in upper case.
So, something like this:
- data: address,city
multi_delimiter: ", "
modify: >-
if (x.split(',')[0] == x.split(',')[0].toUpperCase()) {
x.split(',')[0].toLowerCase() + ", " +
x.split(',')[1]
}
I don’t know why I get undefinedundefinedundefined when the condition is false. Shouldn’t it ignore the if entirely?
(I don’t know if it shows but I started coding in javascript an hour ago )
Of course it works if I add an else with a fake split:
modify: >-
if (x.split(',')[0] == x.split(',')[0].toUpperCase()) {
x.split(',')[0].toLowerCase() + ", " +
x.split(',')[1]
} else {
x.split(',')[0] + ", " +
x.split(',')[1]
}
But I’d rather understand why it doesn’t work without it than call it fixed.
When the ‘if’ is no met, it will send back nothing if you donot catch that situation…hence I stated to always lowercase…much easier and less code
Yeah.
But an empty modify:
field doesn’t return undefined, it returns the data without modifying it
And why do you think that? Is that anywhere documented? Regarless of…you are doing an IF statement without an ELSE…it is not AI. Again, you are making your life unnecessarily compex and it is also eating (a bit?) performance as with each row it will convert to UPPER and then compare… so what is the added value?
Because I tested it
Yeah, it’s not uncommon. I tried with an empty else
yesterday but the result is the same
Good thing I’m not a developer, then.
modify: >-
if (x.split(',')[0] == x.split(',')[0].toUpperCase()) {
x.split(',')[0].toLowerCase() + ", " +
x.split(',')[1]
} else {
}
should work, then. But it doesn’t.
Seems the new version of the integration may have solved some issues …did not test it though
Yes, I discussed it with the developer and tested different versions. Two improvements:
- if the address is in all upper (or all lower) case, the integration will capitalize it;
- you can override the address with your own for each station, should you wish to.
So that answers my original problem entirely. I still wish I understood why my if()
doesn’t work if I don’t specifically specify something when the statement is false.