Template Data Retrieval Fail

so i have a script that… well it’s supposed to be using some template entities which have an attribute of “device_id” referring to the actual entity to switch off based on if the actual template is on… the template for the script i want is as follows:

      {% set entities = [
        "binary_sensor.hue_basement_work_light_notifier",
        "binary_sensor.shelly1_desk_heater_notifier",
        "binary_sensor.zwave_wall_plug_blue_heater_notifier"
      ] %}
      {% set device_ids = [] %}
      {% for entity in entities %}
        {% set entity_state = state(entity) %}
        {% if entity_state.state == 'on' %}
          {% set device_id = entity_state.attributes.device_id %}
          {% set device_ids = device_ids + [device_id] %}
        {% endif %}
      {% endfor %}
      {{ device_ids | join(', ') }}

however it’s giving me blank results…

chatGPT found me this template:

      {% set entities = [
        "binary_sensor.hue_basement_work_light_notifier",
        "binary_sensor.shelly1_desk_heater_notifier",
        "binary_sensor.zwave_wall_plug_blue_heater_notifier"
      ] %}
      {% for entity in entities %}
        {% if is_state(entity, "on") %}
          - service: homeassistant.turn_off
            target:
              entity_id: {{ state_attr(entity,"device_id") }}
        {% endif %}
      {% endfor %}

however, because the service actiuon is within the template i keep getting an error in the configurator… but it displays each of the ‘homeassistant.turn_off’ values correctly.

but simplifying it to create just the list of entities is failing…

anyone have any advice or can solve this riddle? it’s kind driving me nuts…

ChatGPT and other LLMs are bullshit engines and are particularly bad at HA configuration. A perfect example is what you have posted. First, you cannot use Templating to build YAML configuration in situ. Templates can only provide the value for specific configuration variable keys. Second, the convention in HA is that a sensor’s state is controlled by its integration and is not user input… the action homeassistant.turn_off will have no effect on binary sensors.

Please describe what the goal is, not how you are trying to do it. Post the complete script or automation so we have a clear picture.

as stated… each binary sensor has an “attribute of ‘device_id’ referring to the actual entity to switch off”… something like a switch or light.

for example “binary_sensor.hue_basement_work_light_notifier” is a template binary sensor with an attribute of “device_id” labeled as “light.hue_work_light”

so when i fire the script, for each binary sensor with the state of ‘on’, i want it to grab that device id from the attributes, and then run the action to turn all of them off. the whole reason i was writing this is to create a large light of those deviceds and do each one…

so to sum up, the template will use the lists of sensors, determine which ones have a state of ‘on’, grab the ‘device_id’ of each one, then make a list of those. then run the “homeassistant.turn_off” action

First issue: The attribute device_id is already in use in a number of integrations and (as the name indicates) contains a actual device ID. Using a custom attribute with the same name that contains an entity ID will likely come back to haunt you.

There is an existing convention to use the attribute entity_id to hold the entity IDs of related entities, such as those in groups.

You should not need multiple action or a for loop for that. Assuming you make the change above, the action would be as follows:

...
  - action: homeassistant.turn_off
    target:
      entity_id: |
        {% set entities = [
        "binary_sensor.hue_basement_work_light_notifier",
        "binary_sensor.shelly1_desk_heater_notifier",
        "binary_sensor.zwave_wall_plug_blue_heater_notifier"] %}
        {{ expand(entities | select('is_state', 'on'))
        | map(attribute= 'attributes.entity_id') | list }}

i’m not sure what’s up with that particular template, but it gives a result of:

[Undefined]