Issues with filling array (rooms for roomcleaning)

Hi,

Somehow my selected_rooms is not getting filled. The room: - Boolean - Value gives me correct values. But I’m not able to get the selected_rooms good

{% set selected_rooms = [] %}
{% for room in ['badkamer', 'master', 'gang', 'keuken', 'living_room', 'joosarne', 'bentjesse', 'berging', 'bureau', 'toilet'] %}
  {% set room_boolean_state = states('input_boolean.roborock_room_' + room) %}
  {% set room_value = states('input_number.roborock_room_enum_' + room) | int %}
  Room: {{ room }} - Boolean: {{ room_boolean_state }} - Value: {{ room_value }}

  {% if room_boolean_state == 'on' %}
    {% if room_value is not none %}
      {% set selected_rooms = selected_rooms + [room_value] %}
    {% endif %}
  {% endif %}
{% endfor %}
selected_rooms = {{ selected_rooms }}

Can anyone help ?

Kr,

Bart

You need to use namespace.

{% set ns = namespace(selected_rooms = []) %}
{% for room in ['badkamer', 'master', 'gang', 'keuken', 'living_room', 'joosarne', 'bentjesse', 'berging', 'bureau', 'toilet'] %}
  {% set room_boolean_state = states('input_boolean.roborock_room_' ~ room) %}
  {% set room_value = states('input_number.roborock_room_enum_' ~ room) | int(0) %}
  Room: {{ room }} - Boolean: {{ room_boolean_state }} - Value: {{ room_value }}

  {% if room_boolean_state == 'on' and room_value is not none %}
    {% set ns.selected_rooms = ns.selected_rooms + [room_value] %}
  {% endif %}
{% endfor %}
selected_rooms = {{ ns.selected_rooms }}

That worked !! Thanks a lot !

1 Like

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

Nothing to consider, really helpfull. Marked as solutin as requested

Kr,

Bart

1 Like