I’m trying to get data from a local JSON file into the attributes of a sensor. Unfortunately everything I’ve tried either does not work at all or only works to put the data into the state value, which sometimes fails as it is greater than 255 characters.
My JSON file contains two fields, a date and an event. I have validated the file and can select the elements I need but can’t seem to get them into a concatenated string as an attribute.
Here is a test version of the file. The event descriptions differ from the actual data, but varieties of text length and punctuation are represented.
{
"SPECIAL_EVENT": {
"2/23/2025": "Event number one",
"2/28/2025": "Another event on this day",
"3/1/2025": "First Sat of March",
"3/7/2025": "An event with a single quoue Concours d'Elegance",
"3/8/2025": "Event number two",
"3/9/2025": "Daylight savings time starts",
"3/14/2025": "Yet another event",
"3/15/2025": "No work today",
"3/16/2025": "No work today",
"3/20/2025": "First day of Spring",
"4/5/2025": "April showers",
"4/6/2025": "Bring May flowers",
"4/13/2025": "Todays event has a bit longer text description than others",
"4/21/2025": "All descriptions are < 45 characters",
"4/22/2025": "Earth Day",
"4/23/2025": "Two events today, One & Two"
}
}
The code I’m using to parse the data is
- platform: rest
resource: http://192.168.xx.xx:8123/local/json/special_events.json
name: special_events_upcoming
unique_id: "special_events_upcoming"
scan_interval: 14000
value_template: >-
{% for i in range(0,14) -%}
{% set nextdate = (now()) + timedelta(days=i) -%}
{% set nextdate = as_timestamp(nextdate) | timestamp_custom("%-m/%-d/%Y") -%}
{% set this_event = value_json.SPECIAL_EVENT[nextdate]|default('') -%}
{% if this_event != '' -%}
{{ nextdate }}, {{ this_event }},
{% endif -%}
{% endfor %}
I need to collect 4 or 5 events, starting on the current date, on days where there is an event, skipping non-event days. The above code works only if the events do not exceed 255 characters. I’m not sure why, but I can not manipulate the data within the IF loop. It automatically seems to concatenate, but I can’t trim the final string outside the IF loop (to reduce it to <255 Chrs), and inside the IF loop the string lengths are that of the specific field, not the concatenated result.
Unfortunately my brain just does not see how to get my JSON into the attributes. I’ve looked at dozens of posts about JSON, REST and RESTful sensors and can’t wrap my head around to process, using my data.
In addition, many of the solutions only included a snippet of code or a lack of full explanation on how it worked.
** I really don’t want to create 14 individual sensors (modifying my code above). Yes it would work, but it’s not efficient and I really want to learn how to do it correctly. If anyone has a solution that they are willing to share in its entirety (complete code), I’d be grateful.
** I would be OK with non-event dates in the attributes field if the total number of attributes included at least 4 actual events. In other words, Items 1 through 10 (or more) in the attributes and 4 of those had actual event data.
friendly_name: Special Events Upcoming
item1: "2/22/2025,"
item2: "2/23/2025, event one"
item3: "2/24/2025,"
item4: "2/25/2025, Today is Tuesday"
item5: "2/26/2025,"
item6: "2/27/2025,"
item7: "2/28/2025, Another event on this day"
item8: "3/1/2025, First day of March"
... and so on
** Lastly, I would also be willing to rebuild the JSON file using a different structure if that helps accomplish the goal.
Art