custom-cards / flex-table-card

Highly Flexible Lovelace Card - arbitrary contents/columns/rows, regex matched, perfect to show appdaemon created content and anything breaking out of the entity_id + attributes concept
GNU General Public License v3.0
216 stars 26 forks source link

Data from rest_command appears double #145

Open KTHconsult opened 1 month ago

KTHconsult commented 1 month ago

Im using the rest_command to get a json data array. Rest_command add this response to a dictionary where data as json is kept in the item called content alongside the item status that has the response status code. For easy reproduction i added a script with hardcoded result that shows the issue.

Flex Table Card configuration

type: custom:flex-table-card
title: Recipes from script
service: script.recipes
entities: []
columns:
  - name: Id
    data: content
    modify: x.id
  - name: Name
    data: content
    modify: x.name

Output from flex-table-card

Recipes from script

Id | Name

1 | Production Sprint of Painkiller Syringe
2 | Final Sprint of Association Council
3 | Final Testing of Association Syringe
1 | Production Sprint of Painkiller Syringe
2 | Final Sprint of Association Council
3 | Final Testing of Association Syringe

I would expect to only see 3 rows instead of 6

Script that reproduces rest_command wrapping logic

alias: Recipes
sequence:
  - variables:
      recipes: |
        {% set myrecipes = {"content": [ 
          {
            "name": "Production Sprint of Painkiller Syringe",
            "description": "a",
            "id": 1,
            "created": "2024-09-30T09:15:19.203"
          },
          {
            "name": "Final Sprint of Association Council",
            "description": "b",
            "id": 2,
            "created": "2024-09-30T09:15:19.417"
          },
          {
            "name": "Final Testing of Association Syringe",
            "description": "c",
            "id": 3,
            "created": "2024-09-30T09:15:19.64"
          }
        ] , "status":200} %} {{ myrecipes }}
  - stop: All done
    response_variable: recipes

Result from rest_command action

content:
  - name: Production Sprint of Painkiller Syringe
    description: >-
      a
    id: 1
    created: "2024-09-30T09:15:19.203"
  - name: Final Sprint of Association Council
    description: >-
      b
    id: 2
    created: "2024-09-30T09:15:19.417"
  - name: Final Testing of Association Syringe
    description: >-
      c
    id: 3
    created: "2024-09-30T09:15:19.64"
status: 200

How can I configure the flex-table-card to only render 3 rows and thereby reflect the actual data result from the content section?

EdLeckert commented 1 month ago

The logic normally used to iterate on entities is looping on the two top-level keys in the response: content and status.

The , "status":200 is what's causing the duplication, so you'll need to find a way to remove it.

EdLeckert commented 1 month ago

You could use a script like this to strip the status from the response:

call_recipes:
  alias: Call Recipes
  sequence:
    - action: script.recipes
      response_variable: rest_response
    - variables:
        correct_reponse: >
          {{ { "content" : rest_response["content"] } }}
    - stop: All done
      response_variable: correct_reponse