Advertisement
tigattack

HASS Script Loop Scenes in Area

Jan 1st, 2025
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 2.63 KB | None | 0 0
  1. alias: Loop Scenes in Area
  2. sequence:
  3.   - variables:
  4.       scene_info: |
  5.        {# Define current scene and list of scenes #}
  6.         {% set current_scene = states(current_scene_entity) %}
  7.         {% set scenes = expand(area_entities(scene_area))
  8.                         | selectattr('domain', 'eq', 'scene')
  9.                         | map(attribute='entity_id')
  10.                         | list | sort %}
  11.  
  12.         {# Find the index of the current scene in the list of scenes #}
  13.         {% set current_index = scenes.index(current_scene)
  14.                                if current_scene in scenes
  15.                                else -1 %}
  16.  
  17.         {# Retrieve the current direction (1 for forward, -1 for reverse) #}
  18.         {% set current_direction = states(scene_direction_entity) | int %}
  19.  
  20.         {# Calculate the next index based on direction #}
  21.         {% set next_index = current_index + current_direction %}
  22.  
  23.         {# Reverse direction if at the beginning or end of the list #}
  24.         {% if next_index >= scenes | length %}
  25.           {% set next_index = scenes | length - 2 %}
  26.           {% set current_direction = -1 %}
  27.         {% elif next_index < 0 %}
  28.           {% set next_index = 1 %}
  29.           {% set current_direction = 1 %}
  30.         {% endif %}
  31.  
  32.         {# Output the next scene entity ID #}
  33.         {{ {
  34.             'next_scene': scenes[next_index],
  35.             'direction': current_direction,
  36.         } }}
  37.   - action: scene.turn_on
  38.     metadata: {}
  39.     data:
  40.       entity_id: "{{ scene_info['next_scene'] }}"
  41.   - action: input_text.set_value
  42.     metadata: {}
  43.     target:
  44.       entity_id: "{{ current_scene_entity }}"
  45.     data:
  46.       value: "{{ scene_info['next_scene'] }}"
  47.   - action: input_number.set_value
  48.     metadata: {}
  49.     target:
  50.       entity_id: "{{ scene_direction_entity }}"
  51.     data:
  52.       value: "{{ scene_info['direction'] | float }}"
  53. fields:
  54.   current_scene_entity:
  55.     selector:
  56.       entity:
  57.         filter:
  58.           domain: input_text
  59.     name: Current Scene Entity
  60.     required: true
  61.     description: Text entity to store the active scene.
  62.   scene_area:
  63.     selector:
  64.       area: {}
  65.     name: Scene Area
  66.     description: >-
  67.       Scenes must be attached to the selected area to be recognised by this
  68.       script.
  69.     required: true
  70.   scene_direction_entity:
  71.     selector:
  72.       entity:
  73.         filter:
  74.           domain: input_number
  75.     name: Scene Direction Entity
  76.     description: Number entity to store the state of direction (-1/1).
  77.     required: true
  78. description: Loop through scenes (a-z then back through z-a) in the specified area.
  79. icon: mdi:lightbulb-multiple-outline
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement