Can I create template sensors from a list of which I don't know the order?

With the RESTful integration, I am pulling all subscriptions from my money software as a list into an attribute of an entity. The order in which the subscriptions are in the list is unknown but each list element contains an ID attribute. Example below.
What I would like to do is create (template-) sensors for some entries in that list, based on the ID attribute, e.g. sensor.money_subscription_ocs for ID 46.

data: 
- type: bills
  id: '46'
  attributes:
    created_at: '2023-05-08T10:17:28+03:00'
    updated_at: '2025-04-03T11:25:46+03:00'
    name: OCS
    object_has_currency_setting: true
    currency_id: '1'
    currency_name: Euro
    currency_code: EUR
    currency_symbol: €
    currency_decimal_places: 2
    primary_currency_id: '1'
    primary_currency_name: Euro
    primary_currency_code: EUR
    primary_currency_symbol: €
    primary_currency_decimal_places: 2
- type: bills
  id: '47'
  attributes:
    [...]

OK, I found how I can pick one list entry by ID:
{{ state_attr("sensor.firefly_iii_bills","data") | selectattr("id", "eq", "46") | list }}

But how do I access the attributes in the resulting list?

I would recommend the command_line with curl approach, this allows to pipe the feedback through templates like above. Alternatively you can pipe it through JQ to do the select and pre-format the output too (a bit complex unless you are familiar)

Found the solution:

  - name: "firefly_iii_bill_rent"
    unique_id: "firefly_iii_bill_rent"
    device_class: "problem"
    state: >-
      {% set data = state_attr("sensor.firefly_iii_bills","data") | selectattr("id", "eq", "4") | list %}
      {{ "off" if data[0]["attributes"]["paid_dates"][0] is defined else "on"}}      
    attributes:
      paid: >-
        {% set data = state_attr("sensor.firefly_iii_bills","data") | selectattr("id", "eq", "4") | list %}
        {{ data[0]["attributes"]["paid_dates"][0]["date"] if data[0]["attributes"]["paid_dates"][0] is defined else none }}      

Well. it is A solution for you so that is good :slight_smile: … but it does not address your initial question by itself. For completeness you should add above on how you resolved this as the final answer is not resttul-only.
Using command_line + curl and jq you can put all in one entity, now you need more

Actually, it does :smiley: . The question was how do I create (template-) sensors from the list which resides in the RESTful sensor attribute. I do use that RESTful sensor. The other issue is that if I do CLI/curl for every subscription, that would be much more traffic on the finance software. The way it is now, the RESTfull sensor makes one API call, pulls the list and that data is used to populate also the binary sensors for the subscriptions.

The OP has a fine solution. How is relying on an external command better? You can use the template in a restful sensor just as well. Your assumption that this requires anything extra is wrong. As is the assumption that needing anything extra is bad. IMHO relying on curl is a huge unneccesary waste of resources and the chances of it breaking at some point are bigger.

Yeah…sorry, I over-interpreted it. Just as additional info (which you may not care at all :slight_smile: ) … With a curl output through JQ you can create a single json selecting only those attributes you want and then use the command_line json_attributes to put it in the single entity.
It would have neen nice that restul had the option to template things as well, guess there are good reasons for not having it

?? I just responded…this is new to me … can one use templates in the restful integration? The doc does not show this
Added, not sure why curl is bad vs rest(ful), do detail …

Yep, sorry, I didn’t see the response while typing. My bad. An example using templates is at the bottom of the docs of the Restful integration. Plus, the state is provided by a value_template so it should be obvious?

Still… please correct with detail any of my misconceptions…learning is alway sgood

It always felt weird to me the docs stuff everything in attributes (even for the multiple sensor example) and then have:

    value_template: "OK"

A truncated example I have from my SSD smart status:

- verify_ssl: false
  scan_interval: 60
  resource: http://192.168.2.88:8888/api/device/00000000000077/details
  sensor:
    - name: "SSD Name"
      unique_id: sensor.ssd_name
      value_template: "{{ value_json.data.device.device_name }}"
    - name: "SSD Device status"
      unique_id: sensor.ssd_device_status
      value_template: "{{ value_json.data.device.device_status }}"
1 Like