Markdown tables

Hey guys,

I’m trying to create a markdown table for a mealplan - unfortunately it’s only working almost perfectly.

Important to me is the following:

  • alignment (always the same gap between weekdays and meals)
  • hide the weekdays, where there is no meal planned (yet)
  • start the week on Tuesday
  • weekday in bold & meal normal
  • custom text, if no meals are set

I’ve recently came across a thread containing some info regarding markdown tables and came up with the following spaghetti code:

content: >-
    |  
    :---|:---
  {% if is_state('input_text.essen_dienstag', '') %}{% else %}  
  **Dienstag:**        | {{
  states.input_text.essen_dienstag.state }} {% endif %} {% if
  is_state('input_text.essen_mittwoch', '') %}{% else %}

    **Mittwoch:**        | {{
  states.input_text.essen_mittwoch.state }} {% endif %} {% if
  is_state('input_text.essen_donnerstag', '') %}{% else %}

    **Donnerstag:**        | {{
  states.input_text.essen_donnerstag.state }} {% endif %} {% if
  is_state('input_text.essen_freitag', '') %}{% else %}

    **Freitag:**        | {{
  states.input_text.essen_freitag.state }} {% endif %} {% if
  is_state('input_text.essen_samstag', '') %}{% else %}

    **Samstag:**        | {{
  states.input_text.essen_samstag.state }} {% endif %} {% if
  is_state('input_text.essen_sonntag', '') %}{% else %}

    **Sonntag:**        | {{
  states.input_text.essen_sonntag.state }} {% endif %} {% if
  is_state('input_text.essen_montag', '') %}{% else %}

    **Montag:**        | {{
  states.input_text.essen_montag.state }} {% endif %}

  {% if is_state('input_text.essen_montag', '') and
  is_state('input_text.essen_dienstag', '') and
  is_state('input_text.essen_mittwoch', '') and
  is_state('input_text.essen_donnerstag', '') and
  is_state('input_text.essen_freitag', '') and
  is_state('input_text.essen_samstag', '') and
  is_state('input_text.essen_sonntag', '') %}

  Essensplan erstellen! {% else %} {% endif %}

style:
  .: |
    ha-card {
      background: {% if is_state('input_boolean.essen', 'on') %} #444444 {% endif %};
    }
  ha-markdown:
    $: |
      th {
        display: none;
      }
      table {
        border-spacing: 0px;
      }
type: markdown

Here’s how it looks like, when everyday is set:

mealplan_full

So far, so good… but when sunday is the first non-empty one, the table does not get recognized as such and looks like that:

mealplan_sunday

I know this is very specific, but I hope someone might still be able to help out.

Thank you very much in advance!

Finally got it working - rewrote the whole thing. If anyone has a similar project, here’s the code:

content: >-
    |  
    :---|:---
  {% if states('input_text.essen_dienstag') != "" %}    **Dienstag:**
         |
  {{states.input_text.essen_dienstag.state }}

  {% endif %}{% if states('input_text.essen_mittwoch') != "" %}   
  **Mittwoch:**        |
  {{states.input_text.essen_mittwoch.state }}

  {% endif %}{% if states('input_text.essen_donnerstag') != "" %}   
  **Donnerstag:**        |
  {{states.input_text.essen_donnerstag.state }}

  {% endif %}{% if states('input_text.essen_freitag') != "" %}   
  **Freitag:**        |
  {{states.input_text.essen_freitag.state }}

  {% endif %}{% if states('input_text.essen_samstag') != "" %}   
  **Samstag:**        |
  {{states.input_text.essen_samstag.state }}

  {% endif %}{% if states('input_text.essen_sonntag') != "" %}   
  **Sonntag:**        |
  {{states.input_text.essen_sonntag.state }}

  {% endif %}{% if states('input_text.essen_montag') != "" %}   
  **Montag:**        |
  {{states.input_text.essen_montag.state }} {% endif %}

  {% if is_state('input_text.essen_montag', '') and
  is_state('input_text.essen_dienstag', '') and
  is_state('input_text.essen_mittwoch', '') and
  is_state('input_text.essen_donnerstag', '') and
  is_state('input_text.essen_freitag', '') and
  is_state('input_text.essen_samstag', '') and
  is_state('input_text.essen_sonntag', '') %} Essensplan erstellen! {% endif %}
style:
  .: |
    ha-card {
      background: {% if is_state('input_boolean.essen', 'on') %} #444444 {% endif %};
    }
  ha-markdown:
    $: |
      th {
        display: none;
      }
      table {
        border-spacing: 0px;
      }
type: markdown

markdown_check

I think, the important part is where to put the line breaks. Or else the unused line breaks accumulate too much and break the table.

If you want to add spacing between markdown table columns, you can simply use blank columns instead of repeated non-breaking spaces    etc. It’s much neater and easier to use. There’s an example of its use here:

3 Likes

That’s a lot better indeed - thank you very much!

This used to work, but seems like since 2022.6, has this broken for anyone and no longer displays anything?