This is my first time messing with jsonpath (I really wish we could just use a jq filter in REST sensor! – yes I know I can use a command_line
sensor) and I’m struggling to find a way to use the GitHub API to get the friendly version string of the most recent raspberrypi4-64-homeassistant
container that is tagged stable
.
It’s trivial with jq, so I might just make it a command line sensor, but wanted to see if anyone could help me make an appropriate jsonpath filter to use as a REST sensor.
The curl command to get the response:
curl \
-sL \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Authorization: Bearer YOUR_TOKEN_HERE" \
"https://api.github.com/orgs/home-assistant/packages/container/raspberrypi4-64-homeassistant/versions"
Piped to this jq filter: jq -r '.[] | .metadata.container.tags | select(any(. == "stable"))[] | select(. != "stable")'
produces this desired output: 2024.1.5
The json output: response.json · GitHub
The basic logic is simple: look for the object in this array whose tags
array contains "stable"
, then output its tags (ideally skipping "stable"
).
Using this jsonpath tester (but not the recommended one) I’m able to get the following jsonpath to work pretty well:
$..metadata.container[?(@.tags contains "stable")].tags[?(@ != "stable")]
But it looks like hass is using a version that doesn’t support contains
or in
:
$ sudo docker exec hass bash -c 'python -m pip freeze | grep jsonpath'
jsonpath==0.82.2
So I installed that version in a little virtualenv for testing:
# !/usr/bin/env python3
import json
from jsonpath import jsonpath
def main():
with open("response.json") as f:
content = json.load(f)
json_attrs_path = r'$..metadata.container.tags'
# json_attrs_path = r'$..metadata.container[?(@.tags contains "stable")].tags[?(@ != "stable")]'
result = jsonpath(content, json_attrs_path)
print(json.dumps(result, indent=4))
if __name__ == "__main__":
main()
Using $..metadata.container.tags
I’m able to filter the output quite a bit – all tags for all versions – which I thought was perhaps enough to do some additional filtering with jinja
, but the REST sensor gives an error about the content being too long, so I never make it there.
Any suggestions on a supported jsonpath syntax to filter an array of objects for the first object (or all objects) whose metadata.container.tags
array contains "stable"
?
Thanks for any suggestions! As noted, I’ll probably just use a command line sensor with the above jq
, but just curious if the REST sensor has a solution for this. TIA!