Configuration using loop

Hello,
I dont know if its possible but I encounter some problem with utilization of templates.
Indeed I have rest API that give me a status of several captors (open/close) and I would like to create sensor for each one.

I’ve created following configuration :

sensor:
  - name: Door captors
    platform: rest
    resource: https://192.168.1.100:5000/alarm/door-captors
    method: GET
    verify_ssl: false
    username: livingroom
    password: !secret alarm_api_token

binary_sensor:
  - platform: template
    sensors: >-
      {% for dc in state.sensor.door_captors.value_json %}
      {{ dc.name }}:
        value_template: "{{ dc.status }}"
        device_class: opening
      {% endfor %}   

my rest request give me the following json :

[{"name":"Garden Door 2","status":"close"},{"name":"Front Door","status":"close"},{"name":"Garden Door 1","status":"open"}]

I have folowing error :

Invalid config for [binary_sensor.template]: expected dictionary for dictionary value @ data['sensors']. Got '{% for dc in state.sensor.door_captors.value_json %} {{ dc.name }}: \n  value_template: "{{ dc.status }}"\n  device_class: opening\n{% endfor %}    '. (See ?, line ?). 

can you help me with this plz ?

Regards Marc.

Templates return a string…always. This needs a dictionary or list.

These 2 are not the same thing. I spent so long trying to do something similar…adding the little list symbol in front ‘-’…nothing.

Your only hope here is a python script or AppDaemon. From there, you can parse the json and create any number of sensors you want…all in python.

What are you expecting that template to do? To generate something like this?

binary_sensor:
  - platform: template
    sensors: >-
      Garden Door 2:
        value_template: close
        device_class: opening
      Front Door:
        value_template: close
        device_class: opening
      Garden Door 1:
        value_template: close
        device_class: opening

That won’t work for several reasons.

The first is that the template is executed after Home Assistant reads the YAML configuration. So you can’t define entities that way and expect them to be found after the configuration has already been processed.

The second is that, even if they could be generated afterwards, the entities are in the wrong format. You can’t create entity names with spaces or capitalized letters.

Thirdly, the value_template is incorrect. It’s a binary_sensor so, regardless of its device_class, its state is on or off and not open or close (that’s reserved for how it reports the state in the UI). Plus, it would look like this "{{off}}".

OK thanks for responses, I understand better why This kind of stuff not work.
So I’ll generate my config files using pre-script as suggested, using python or ansible.

Regards Marc.