Skip to content

Assigning an array of values as the type of variable #120

@hwndmaster

Description

@hwndmaster

Briefly

I have a scenario where I need:

  • to have an sensor helper or variable that would live during the uptime of HA, which may contain an array of string values.
  • to be able to add new items to the array.
  • to be able to remove items from array, if there are more than X items.
  • to render the item values in a card. For now I'm using Markdown Card with a template which enumerates all the values and renders them in the card.

From other issues of the repository I understand that the add-on supports any type of JSON-like objects, like strings, dictionaries, etc. So I expected to have arrays also supported by the add-on, correct me if I'm wrong.

To reproduce

Here is the variable definition in configuration.yaml:

var:
  example_array:
    friendly_name: "Example Array"
    initial_value: []
    icon: mdi:view-list-outline

Then I'm attempting to set a value to that array variable using the Developer tools / Actions:

action: var.set
data:
  entity_id: var.example_array
  value_template: >-
    {{
      [1, 2, 3]
    }}

This doesn't work with an error reported:

Caution

Failed to perform the action var.set. template value should be a string for dictionary value @ data['value_template']. Got None

I've attempted to assign an array to an attribute, same issue.

If I try to assign an array using value parameter, then it seems it is being assigned as a string. Here is the script:

action: var.set
data:
  entity_id: var.example_array
  value: [1, 2, 3]

Then I validated it with Developer tools / Template:

{% if [1,2,3] is list %}
  [1,2,3] is a list.
{% endif %}
{% if states('var.example_array') is list %}
  var.example_array is a list.
{% endif %}
{% if states('var.example_array') is string %}
  var.example_array is a string.
{% endif %}

It renders only:

  [1,2,3] is a list.
  var.example_array is a string.

Could someone help me to understand how to work with arrays correctly?
Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions