I’m struggling with a Rest Sensor, or more its data structure that isn’t very home assistant friendly.
The json response has a leading hidden char that I’ve managed to remove, that made the struct to parse correctly.
The json object have an array, in that array I want to find a specific object by its id, this I’ve also solved.
Problem comes when I want to add more of the data (from the same level) as attributes.
I’ve tried a few things but I guess the json_attributes
parse the original data and not the output from the value_template
? From some googling I figured out a way that might have worked if the original data didn’t have that leading crap char but as it is now it won’t parse.
I’m not 100% sure this is the problem, but it looks like it.
Is there any way around it?
This is what I have so far (I can’t share the entire sensor due to the api not being public).
** sensor:
- name: "test_status"
value_template: >
{% set cleaned_value = value | replace('\ufeff', '') %}
{% set lifts = cleaned_value | from_json %}
{% set lift = lifts.lifts | selectattr('id', 'equalto', '252') | first %}
{% if lift %}
{{ lift.status }}
{% else %}
"Unknown"
{% endif %}
json_attributes_path: "$.lifts[?(@.id=='252')]"
json_attributes:
- name
- updatedAtUtc
Here’s a subset of the (cleaned) data.
{
"xid": "newyork",
"lifts": [
{
"id": "252",
"name": "Empire State",
"shortName": "ES",
"type": "building",
"status": "active",
"updatedAtUtc": "2025-01-07T07:11:59Z"
}
}