I am trying to get information from an API call and extract train departure times from the JSON.
So far I was able to get a list from the API with this code:
{{ value_json[‘Departure’]|selectattr(‘directionFlag’,‘eq’,‘2’)|map(attribute=‘time’)|list }}
I asked ChatGPT to take instead create variables for each attribute in the list, and assign every
variable to a new sensor.
This I then wanted to paste into my sensor.yaml file to create one sensor per next train departure
to be able to display them on a screen in my hallway, and update in real time.
ChatGPT came up with this, which does not work.
Can anybody help me how I can fix this?
Do you need more information?
Thank you so much.
- platform: rest
resource: https://api.****** my url for api call
method: GET
scan_interval: 300
sensors:
- platform: template
sensors:
{% for time in value_json['Departure']|selectattr('directionFlag','eq','2')|map(attribute='time') %}
- name: time_{{ loop.index }}
friendly_name: "Time {{ loop.index }}"
value_template: "{{ time }}"
{%- if not loop.last %},{% endif %}
{% endfor %}
ChatGPT can produce amusing nonsense and that’s a good example of it. It’s a fascinating hybrid of REST sensor and a Template Sensor in both modern and legacy format.
Try re-phrasing your question to ChatGPT (i.e. simplify it) and maybe it’ll produce something useful.
This is incredible and we’re going to see more of this.
I’d like to note that users that do this and just say “it doesn’t work” will not be getting much help from me (FWIW). Copy and pasting without effort and insight is not a good idea. ChatGPT can at best assist with something here and there, but it’s not magic and it’s not actually smart in any way. You cannot exchange it for actual expertise.
OP, there are many things you can’t avoid doing if you want some help here.
Firstly, we don’t know what the data looks like you’re parsing.
Secondly, while the three of us that have responded so far can see obvious problems, you need to show us the specific errors you got. While sometimes tricky to unpack, it will be telling you what the issue is. You need to show that you’ve actually tried something.
Thirdly, never take anything from anywhere without testing. Even working solutions may not work for others since the context could be different. You can, e.g. take an example JSON response from that API and make a variable in the developer tools. You test small parts of your code/config.
Fourthly, docs. Did you read any of it? Do you even know what you are using and have you compared what you’re doing with any of the thousands of similar posts?
There’s one very fundamental mistake in your code, which is that templating code cannot generate YAML itself. It can only provide values to YAML keys. If this doesn’t make sense to you, you have a lot of reading to do.
And before you think I’m not being helpful: I’ve spent a fair bit of time on this and have given you a ton of hints, but it will now require a bit of effort from your side. No spoonfeeding, sorry, unless there’s a very clear indication that you deserve it.
yeah, I had my first experience with helping someone a short while ago that got info from chatgpt and it screwed up the code (I can’t find the thread right now tho). I didn’t know that’s where it came from at first. But in the end I think I got it working.
it seems that this is the next iteration of “zero effort home automation”.
first it was the UI editor:
“I want to make very complex automations but the UI editor can’t do what I want. Why not?” or “I wrote an automation thru the UI but it’s not working correctly. Here’s a huge screenshot of the UI editor. Can you fix it for me?”
then there were blueprints:
“I don’t want to learn how how to write any automations at all. Can someone write a blueprint from scratch for me for this specific function?”
Now a chat AI:
“nothing in this garbled mess is working. Can you rewrite the entire thing for me and make it work?”
I’m surprised it can even get as close as it does TBH but unless the user has some idea how it’s supposed to look there’s no way they can fix all the mistakes.
There really is no shortcut to just learning this stuff for yourself and putting in at least some minimum effort.
Why did you use chatgpt and subsequently create new post when @Troon gave you the answer in another thread? Closing this. Please continue your support in the original thread.