Getting a nested entity

I have this entity:-

calendar.rental_control_spare_room

message: Spare Room Reserved
all_day: false
start_time: '2023-02-08 11:00:00'
end_time: '2023-02-09 11:00:00'
location: ''
description: >-
  Reservation URL:
  https://www.airbnb.com/hosting/reservations/details/XYZ

  Phone Number (Last 4 Digits): 1234
friendly_name: Rental Control Green Room

How would I go about extracting the Reservation URL out into a template?

I’ve worked out I could do it like this:-

{{state_attr('calendar.rental_control_ensuite_room','description').split(': ')[1].split('Phone')[0]}}

But is there a better way ?

The template you posted extracts the phone number. Do you want a better way to extract the phone number or the URL?

Two ways to get the URL:

{{ state_attr('calendar.rental_control_ensuite_room','description') | regex_findall_index(find='https://.*') }}

{{ state_attr('calendar.rental_control_ensuite_room','description').split('\n')[1] }}

According to dev tools, I’m getting the URL from my attempt:-

I’m splitting and getting the second element after the ‘:’ which strips off ‘Reservation URL:’, and getting the first element from a split of 'Phone.which gets me the URL.

Your first alternative is brilliant, I wasn’t aware of that function, another piece of learning for me.

The second you posted gets the Phone number unless I change it the split from [0] to [1], then it returns

Reservation URL: https://www.airbnb.com/hosting/reservations/details/XYZ

And I didnt make it clear that I dont want the word ‘Reservation URL’ as I am going to use the URL in:

This confusion is down to the way you’ve pasted the calendar entry above, with an apparent newline after “URL:”. I think it’s actually this:

description: >-
  Reservation URL: https://www.airbnb.com/hosting/reservations/details/XYZ

  Phone Number (Last 4 Digits): 1234

Regardless, the regex solution is the way to go.

Thanks @Troon

In my dev tools, its showing the URL on a second line:-

image

But I’ve learnt some new things today with use of regex.

When I tried to simulate the data you posted, for testing purposes, your template reported the phone number.

If your template actually reports the URL then it means I didn’t simulate your data correctly.

Nevertheless, the two techniques I posted are capable of extracting the URL.


Keep in mind that none of the techniques presented so far are fault tolerant (neither mine or yours). In other words, if the data isn’t structured exactly as expected, all the techniques will fail with an error (for example, if regex_findall_index doesn’t find the pattern or when attempting to reference a non-existent item in a list).

To make it fault-tolerant, you can check the list’s length before attempting to reference a specific item in the list. However it’s a bit more challenging for bomb-proofing regex_findall_index.