#How can i dynamically generate a list of switch entities to turn on/off, based on various criteria?

1 messages · Page 1 of 1 (latest)

solar aurora
#

Hi, i'm looking to see how (if it's even possible) to generate a list of switch entities that i can pass to something like a service.turn_off. Here's my use cases:

  1. I have 10 smart outlets, with a left and right switch entity, which i want to incorporate into this list. They all follow the entity naming scheme of switch.outdoor_outlet_(number 1 through 10)_(left/right).
  2. These outlets all have a helper boolean which is used to define if it's enabled (basically if it'll be used in this automation). These all follow the naming scheme of input_boolean.outdoor_outlet_(number 1 through 10)_(left/right)_enabled_christmas.
  3. These outlets also all have an input select to set what side of the house the outlet provides power to. I have a Road Side and Drive Side option. These all follow the naming scheme of input_select.outdoor_outlet_(number 1 through 10)_(left/right)_location_christmas
  4. Depending on the trigger ID of the automation, the switches will be selected if they meet any of the following criteria:
    a. Triggered by Turn all on AND input_boolean.outdoor_outlet_(number 1 through 10)_(left/right)_enabled_christmas is true.
    b. Triggered by Turn drive side on AND input_boolean.outdoor_outlet_(number 1 through 10)_(left/right)_enabled_christmas is true AND input_select.outdoor_outlet_(number 1 through 10)_(left/right)_location_christmas is Drive Side.
    c. Triggered by Turn road side on AND input_boolean.outdoor_outlet_(number 1 through 10)_(left/right)_enabled_christmas is true AND input_select.outdoor_outlet_(number 1 through 10)_(left/right)_location_christmas is Road Side.

Basically, i've tried to set them up in the easiest way possible to automate with something like a for loop written in jinja that counts from 1 to 10 and then check the criteria, but i'm struggling to see how it can be applied to make a list of entities. I'm semi-experienced with syntax of jinja/YAML but if anyone could point me in the right direction on how to make this, it'd be appreciated. Thanks!

#

Other (maybe) useful information:

  • I don't want to hard-write this simply as it changes every year, i have a dashboard where i configure all of these switches and have it hard-written and used it for years, but it's getting more and more tedious to edit as i add more outlets and stuff (used to only have 5, now i have double that). It's became one of those bodged automations that's a mess but it sorta works if you just don't ever touch it, it's finally time to do it properly.
  • The splitting of road/drive is needed as we have 2 "switch ons", one for the neighbours' side, and one for the public-facing side of the house. different people do each one and it's tied to a Zigbee/Sonoff button which i configure on the fly to do each side.
spice hound
#

you can always ask chatgpt to generate a template for this purpose, that you can copy paste in your developer tools inside ha -> dev tools -> templates

the output should be your desired information

#

i do this all the time.. works great

solar aurora
#

thanks. i tried this before, and i get an error saying access to attribute "append" of "list" object is unsafe. i have asked it to try and fix this but it seems HA can't handle appending to an object? this is the code it spat out

{% set trigger_id = "turn_all_on" %}
{% set sides = ['left', 'right'] %}
{% set switches = [] %}

{% for i in range(1, 11) %}
  {% for side in sides %}
    {% set enabled = 'input_boolean.outdoor_outlet_' ~ i ~ '_' ~ side ~ '_enabled_christmas' %}
    {% set location = 'input_select.outdoor_outlet_' ~ i ~ '_' ~ side ~ '_location_christmas' %}
    {% set switch = 'switch.outdoor_outlet_' ~ i ~ '_' ~ side %}
    
    {% if is_state(enabled, 'on') %}
      {% if trigger_id == 'turn_all_on' %}
        {% set _ = switches.append(switch) %}
      {% elif trigger_id == 'turn_drive_side_on' and is_state(location, 'Drive Side') %}
        {% set _ = switches.append(switch) %}
      {% elif trigger_id == 'turn_road_side_on' and is_state(location, 'Road Side') %}
        {% set _ = switches.append(switch) %}
      {% endif %}
    {% endif %}
  {% endfor %}
{% endfor %}

{{ switches }}
#

i got it to spit some new code out, but i don't think it's actually working. from what i can tell it's trying to add an array to an array, unless this is just jinja's format for Array.push()?

{% set trigger_id = "turn_all_on" %}
{% set sides = ['left', 'right'] %}
{% set switches = [] %}

{% for i in range(1, 11) %}
  {% for side in sides %}
    {% set enabled = 'input_boolean.outdoor_outlet_' ~ i ~ '_' ~ side ~ '_enabled_christmas' %}
    {% set location = 'input_select.outdoor_outlet_' ~ i ~ '_' ~ side ~ '_location_christmas' %}
    {% set switch = 'switch.outdoor_outlet_' ~ i ~ '_' ~ side %}

    {% if is_state(enabled, 'on') %}
      {% if trigger_id == 'turn_all_on' %}
        {% set switches = switches + [switch] %}
      {% elif trigger_id == 'turn_drive_side_on' and is_state(location, 'Drive Side') %}
        {% set switches = switches + [switch] %}
      {% elif trigger_id == 'turn_road_side_on' and is_state(location, 'Road Side') %}
        {% set switches = switches + [switch] %}
      {% endif %}
    {% endif %}
  {% endfor %}
{% endfor %}

{{ switches }}
#

screenshot 1 shows it not working, 2nd screenshot is all the outlets that are enabled, so those should appear in that list

#

if i add {{switch}} into where it's attempting to add to the array, it returns all of the outlets that are needed, but the array is still empty. i think my issue here is just trying to make a list, the other stuff has been worked out by burning dinosaurs

ionic tangle
solar aurora
#

hi, that seems to work, but only on the left outlets. been staring at it for a while trying to work out why, but it only seems to run 10 iterations and all of them are the left ones

ionic tangle
#

ow, yeah. Make it do 20 runs 😄

solar aurora
#

yeah i just worked that out, i did all the maths in excel coz im dumb and just got to the same conclusion haha

ionic tangle
#

😄

solar aurora
#

just for my ocd's sake, how can i make them do it in socket order, left then right, like this?
i'm thinking just change the template variables to:

variables:
  outlet_number: "{{ ((repeat.index + 1) // 2) }}"
  outlet_side: "{{ 'left' if repeat.index % 2 == 1 else 'right' }}"
#

worked it out myself, yep that worked!

much easier than all that jinja stuff, and robust enough to just update on the fly. exactly what i needed, thank you

ionic tangle
#

yeah, that would work as well 🙂

thick marsh
#

If you would have prefixed the numbers below 10 with a zero, you could have just sorted the list of entities

solar aurora
thick marsh