Flex-table-card: do math based on another sensor

Hi all,

I’m working again on this table: Flex-table-card: concatenate data and change its case?

Now that I have an integration that gives me the state of my fuel tank (sensor.ux250h_fuel_level which returns a value between 0 and 100), I’d like to add a column that displays the price of a fuel top-up (if the gas tank is 30% full, It should display 70% of the previous column).

Below is my code that doesn’t work but should show you what I expect to be able to do. How can I “access” the value of ‘sensor.ux250h_fuel_level’ inside the flex-table-card?

type: custom:flex-table-card
sort_by: state+
clickable: true
title: UX 250h
max_rows: 5
entities:
  include:
    - sensor.station_*E10
    - sensor.station_*SP95
    - sensor.station_*SP98
columns:
  - data: entity_picture
    align: center
    name: ""
    modify: "'<img src=\"' + x + '\" style=\"height: 35px; max-width: 100px;\">'"
  - data: fuel_type
    align: center
    name: ""
    icon: mdi:gas-station
  - data: address,city
    multi_delimiter: ", "
    align: left
    name: ""
  - icon: mdi:currency-eur
    data: state
    name: " "
    align: center
    modify: >-
      Intl.NumberFormat('fr',{ minimumFractionDigits:3,maximumFractionDigits:3
      }).format(parseFloat(x))
    suffix: €/l
  - data: state
    name: Plein
    align: center
    modify: >-
      Intl.NumberFormat('fr',{ minimumFractionDigits:2,maximumFractionDigits:2
      }).format(parseFloat(x)*43)
    suffix: €
  - data: state
    name: Appoint
    align: center
    modify: >-
      Intl.NumberFormat('fr',{ minimumFractionDigits:2,maximumFractionDigits:2
      }).format(parseFloat(x)*43*(1-{states['sensor.ux250h_fuel_level'].state}/100))
    suffix: €
  - data: distance
    modify: >-
      Intl.NumberFormat('fr',{ minimumFractionDigits:1,maximumFractionDigits:1
      }).format(parseFloat(x))
    suffix: " km"
    name: Distance
  - 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"
layout_options:
  grid_columns: 8

Only if an attribute is added = states(another entity).

Nothing is obvious to me when it comes to HA.

This doesn’t work:

    modify: >-
      Intl.NumberFormat('fr',{ minimumFractionDigits:2,maximumFractionDigits:2
      }).format(parseFloat(x)*43*(1-states(sensor.ux250h_fuel_level)/100))

I’m sorry I need so much hand holding :frowning:
But I’m doing my best, I swear. From your message (and the fact the code doesn’t work :grimacing:), I understand I could do that only if my sensor had an attribute on top of its state, which isn’t the case:

I believe I cannot add an attribute to an entity created by an integration, so I shall create a custom sensor with a “liters_to_full” attribute, correct?

And then, how can I pull that data into my flex-table-card?

The “vanilla” way is to create a template sensor.
The hacky way - try using a CustomUI plugin, it allows adding an attribute with a dynamic content, kind of:

homeassistant:
  customize:
    sensor.xxx:
      new_attr: >-
        [[[ ... use JS ... ]]]

Check examples in Docs (also, check a syntax in my code, I may miss smth since do not use Custom UI for a long time).

I like vanilla (trying to avoid increasing the number of custom add-ons etc.).

So I have my template sensor:

  - sensor:
      - name: "UX250h Carburant en litres"
        unique_id: ux250h_fuel_in_liters
        state: "{{ states('sensor.ux250h_fuel_level') | float(0.1) * 43/100}}"
        unit_of_measurement: "L"
        device_class: volume
        attributes:
          tank_capacity: 43
          liters_to_full: "{{ (100 - states('sensor.ux250h_fuel_level') | float(0.1)) * 43 / 100 }}"

So how do I use my liters_to_full attribute inside the table, now?

The 1st question was “how can I use another entity in flex-table”.
Now the question is SOLVED - the value became an attribute.
Rest of other things is about “check docs for the card, ask in the dedicated flex-table-card thread” - there are plenty of examples about “how to show/use attributes in the card”.

Oh, come on, man, the question was “how to do math based on another sensor?”, not “what type of sensor should I create to be able to do math with it later?”

Which is answered - “you can only do it with attributes, not with another entity”.
And how to do it with attributes - explained in docs or in the dedicated thread.

It’s not. It’s a start but it’s a very small part of the answer.

The answer to “How do I install shelves?” is not “Use a drill and read the doc”

Ok, suggest to use a dedicated thread for this card (there is a huge one in “Share your projects” with simple examples in the 1st post & lots of nice & complex examples in other posts). And suggest to at least have a quick glance on Docs since EVERY example is related to using attributes (each column IS an attribute) and a huge amount of examples are about using a “modify” option which supports JS to address attributes & do math/etc operations on them.

But why are you willing to spend so much time telling me I should seek help elsewhere? Instead of being helpful?

I’m pretty bad in Python but JS is a whole other level: I understand nothing of it. If I could find the answer in the doc, I wouldn’t ask here but it’s like reading Chinese.

I am done. Several times suggested you to read info in ONE place & ask there if any questions are left.
A general schema:

  - data: attr_name
    modify: >-
      ... any JS code for x, where x = a value of the attr
    name: any name

And if a column for attribute X needs values from attribute X & attribute Y - then this will only work when both values are sub-attributes of some Z attribute - because you only have access to the CURRENT attribute (“data” option), whether it is a single value or a complex structure like list/dictionary:

  - data: Z
    modify: >-
      ... any JS code for Z.X & Z.Y
    name: any name

This is all described in the dedicated thread & docs. That is why these “main threads” exist - to accumulate questions & answers. Otherwise we will have 100500 threads about the same. Please, go there into that thread and at least check links in the 1st post, they will help you to learn basics.

A hint: tables may be created in Markdown (w/o sorting) - and you can define ANY complex formula for every column (not bound to that “Z.X / Z.Y” limitation)