Creating Table in markdown card with for loop

Hello,
I am using a markdown card to show gas prices which looks like this:


the code is

type: markdown
content: >-
  # Diesel: 

  {% for entity in expand('group.all_fuel_sensors') | sort(attribute='state') |
  map(attribute='entity_id') | map('string') | list %}

    {% if 'ZG' in state_attr(entity,'station_name') %}
    ![Image](/local/img/ZG.png) 
    {%elif 'bft' in state_attr(entity,'station_name') %}
    ![Image](/local/img/bft.png)
    {%elif 'Vimbuch' in state_attr(entity,'station_name') %}
    ![Image](/local/img/bft.png)
    {%elif 'Aral' in state_attr(entity,'station_name') %}
    ![Image](/local/img/Aral.png)
    {% endif %}

    {{ state_attr(entity,'station_name')  }}
    {% if 'unknown' in states(entity) %} 
    ## Geschlossen 
    {% else %}
    ## {{ states(entity) }} €
    {% endif %}
    ***
  {% endfor %}

Now I want to get this shown in a table which is like:
| Icon | Name of gas station | price |
How is it possible, to get the results in the for loop formated to be table rows?
Do I have to push the results into variables and print them at the end? The table should be sorted at the end?
I am a new in home assistant and having no clue how to proceed here.

This may help…you need the , and the


1 Like

Thank you, yes it helped, now I solved it like this:

btw, this is also a nice solution

type: custom:flex-table-card
clickable: true
sort_by: state
max_rows: 15
title: Gasoil
entities:
  include: sensor.station*gazole
columns:
  - name: nom station
    data: friendly_name, address
    modify: x.replace('Gazole', '').replace('Station', '')
    multi_delimiter: <br />
  - name: dist.
    data: distance
  - name: prix
    data: state
  - name: Valid.
    data: days_since_last_update
    align: right
css:
  tbody tr:nth-child(1): 'color: #00ff00'
  tbody tr:nth-child(15): 'color: #f00020'
style: null

1 Like

Thank you, that looks great also. I want to add an additonal column for the price of e10 of the stations. the name of of the sensor is the same, but has the suffix _e10 instead of _diesel. Do you have an idea how I could get this addes?

I can only guess but try this
include: sensor.*e10

EDIT: ah no…you want it in a column… can you send a sample of that sensor and explain me wich columns you want?

Given this a bit more thought… I doubt that one can put both diesel and e10 on the same line for the same station UNLESS these are in a single entity.
Else, on a db level as example, you would need grouping on station name and put value 1 in column A and value 2 in column B, or do a join… but I have not yet seen this type of actions for a card …which does not mean that it does not exist.
Maybe with a markdown this could work

  1. sort all objects on name and petrol type
  2. run through them 1 by 1, if previous name is not current name then print data in column A (name) , column B (price1) else in column C (price2)

This is doable but I donot see me doing this (timewise and lack of data)

EDIT: or… you have to write a jonja code combining both sensors in a sable and then another one splitting them out…if you really need this then I suggest to go to the discord forum on templates…there are some great wizards out there,you’d have to provide the data and an example of course, be very specific with the question

1 Like

Thank you very much again. Of course I do not want to write you the code, the hints are all I need. At the moment I have no idea what to search for. At the moment I try to sort out for me, when using using which language oder formating and what I can do there.
Your last thought is, what I think would be the best, I will go for this approach. I will print the code here, when I figured it out :slight_smile:

I can share this with you as markdown iterating through data, not sure if it will help.
it iterates through a list of objects (dates)
if date[i] <> date[i-1) . [if date now is not equal to previous date ]
then print previous sensor subject and previous aqname
else print current sensor subject and aqname

but… try discord #templates too

<table>
{% set dates = state_attr('sensor.name_sensor','date')%}
{% for i in range(0, dates | count, 1) %}

{% if (dates[i] == dates[i-1]) and ( state_attr('sensor.name_sensor','subject')[i] == state_attr('sensor.name_sensor','subject')[i-1]) %} 
<td>{{ state_attr('sensor.name_sensor','acquisition_name')[i-1] }}</td>

{% else %} <tr><td colspan="5"><b>{{ strptime(dates[i],"%Y/%m/%d").strftime("%m/%d") }} 
{{ state_attr('sensor.name_sensor','subject')[i]}} </td>
</tr>
<td>{{ state_attr('sensor.name_sensor','acquisition_name')[i] }}</td>
{% endif %}


<td width="8%">
{% if state_attr('sensor.name_sensor','acquisition_level')[i] == 'Expert' %} 🟢+
{% elif state_attr('sensor.name_sensor','acquisition_level')[i] == 'Maîtrise satisfaisante' %} 🟢
{% elif state_attr('sensor.name_sensor','acquisition_level')[i] == "En cours d'acquisition" %} 🟡
{% elif state_attr('sensor.name_sensor','acquisition_level')[i] == 'Maîtrise insuffisante'  %} 🟠
{% elif state_attr('sensor.name_sensor','acquisition_level')[i] == 'Absent'  %} A {% else %} ? 
{% endif %}</td>
</tr>
{% endfor %}
1 Like