Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hwndmaster opened this issue Jan 17, 2025 · 1 comment
Open

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

hwndmaster opened this issue Jan 17, 2025 · 1 comment

Comments

@hwndmaster
Copy link

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.

@hwndmaster
Copy link
Author

I forgot to mention, that I've attempted to use data_template as well, such as:

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

The result is all the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant