Regex on >255 chars value?

Hello,
before I start asking difficult things, is it even possible to get values with regex out of a source value that is >255 chars?
Thanks!

Yes, the 255 character limit is on the eventual entity state. You can take a much larger input — a REST response, perhaps — and process it down to size.

What’s the difficult question?

Thanks @Troon for your reply.
I’ve managed to put a JSON formatted reply > 255 chars into a attribute of a sensor.
The difficulty I face is to get a value out of that chunk of text.
I’m looking to get the last value of “end_time”. Note, the nr of reservations can vary (its not always 2). I’ve been trying with templating and regex but so far not successful.
The JSON response (already derived from a bigger one):

schedule: 
court_id: 10227288
number_of_reservations: 2
court: Padel 2
sport: Padel
in_outdoor: O
reservations:
  - reservation_id: 47763869
    start_time: 05/07/2023 10:00
    end_time: 05/07/2023 12:00
    duration: 120
    reservation_type: A
    reservation_info: Tenniskamp jeugdweek juli
    activity_type: 4
    players: []
    access_code: null
  - reservation_id: 47763879
    start_time: 05/07/2023 13:00
    end_time: 05/07/2023 16:00
    duration: 180
    reservation_type: A
    reservation_info: Tenniskamp jeugdweek juli
    activity_type: 4
    players: []
    access_code: null

friendly_name: TV Padel Veld 2 Schedule

Would be brilliant if you would have the solution.

This should pull that value out, as a string:

value_template: "{{ value_json['reservations'][-1]['end_time'] }}"

The [-1] is Python list indexing, and says “take the last value” (or the first value counting backwasrds, if you prefer to think of it like that). See here.

You could then use strptime() to turn it into a datetime object if needed (docs).

I’m willing to see if there’s an easier way to get what you want from that. Regex should not be needed for processing JSON responses. They can be helpful in processing a final string result, but if you’re using them to do indexing / selection then you have two problems.

1 Like

Excellent, thanks very much.
It’s working!

With the format you gave I’ll try to get it indeed in one go. Will let you know.

In the meantime

      tv_padel_veld_1_endtime:
        friendly_name: "TV Padel Veld 1 EndTime"
        value_template: >
          {{ as_timestamp(strptime(states.sensor.tv_padel_veld_1_schedule.attributes['schedule']['reservations'][-1]['end_time'],'%d/%m/%Y %H:%M')) | timestamp_local  }}
        device_class: timestamp

is being recognized as a timestamp.

However now() - states.sensor.tv_padel_veld_1_endtime doesn’t want to work.

That’s normal. The sensor state is always a string, and additionally you’re referencing the state object with that syntax. To access the state itself, you need states.sensor.tv_padel_veld_1_endtime.state, or better, states('sensor.tv_padel_veld_1_endtime'). Docs are here.

For comparing with now(), you need:

{{ now() - states('sensor.tv_padel_veld_1_endtime')|as_datetime }}

Time functions and filters are explained here.

1 Like

thanks a million, with all these time and date functions and templating I lost it.
Is there a way I can thank you?

You just did, a million times.

1 Like

Hi @Troon , I’d like to make an appeal on your knowledge again.
In the mean time I have advanced and have the schedules now nicely into a sensor attribute.
Now I’d like to create calendar events from them. Probably using a loop with for and endfor.
Do you have any experience with that?

No. Best asked as a separate topic, including explicit details of the data you’re starting with.

These will probably help: