I want to display these values in a table on a dashboard. I took the markdown table route. to put the values in attribute of a sensor and then loop through them in dashboard.
rest_command:
get_values:
url: "https://example/api/times/1"
method: GET
template:
- triggers:
- platform: time_pattern
minutes: "/1"
actions:
- action: rest_command.get_values
response_variable: data
sensor:
- name: "My Schedule"
unique_id: my_schedule_1
state: "{{ data.content | from_json | length if data.content is defined else 0 }}"
attributes:
values: "{{ data.content | from_json if data.content is defined else [] }}"
last_updated: "{{ now() }}"
However this does not add the attribute for some reason. Is there any way to do this where the root element of json is an array.
Is the state populated with the length as expected? Is it only the values attribute that isnāt populated? If you just make the state say 0 and put only data in that attribute, what do you get?
Alternatively, try one of the REST sensor platforms, which I think is a bit better suited for this, but I donāt see an immediate issue with your config, unless the is defined check isnāt working as expected.
I could have sworn that state was defined and had the correct value as I had seen it in the past which equalled length of the array. And It was the values attribute that did not exist. However now I am getting error in the logs
ERROR (MainThread) [homeassistant.helpers.script.trigger_update_coordinator] Trigger Update Coordinator: Error executing script. Service not found for call_service at pos 1: Action rest_command.get_values not found
So I switched to using REST sensor. I had switched to REST command because in the past I was creating more sensors i.e. retrieve first 3 values in the array and put each one in their own sensor. That was not scalable as I wanted more values and the API does not always return 3 values. It can be more or less. Now I am trying REST sensor.
- platform: rest
resource: "https://example/api/times/1"
name: test
value_template: "OK"
json_attributes:
- data
json_attributes_path: "$"
I do see the value āOKā but not attributes. Because the root of JSON is just an array. How do I refer to it using json_attributes_path and then put it in json_attributes. I thought it was not possible.
Your problem is that you used the name values for your attribute. values has a very specific meaning for a dictionary (which is what attributes are). Change the attribute to anything other than values, items, or keys.
I already have it working with that kind of solution where i use array indices like [0], [1] and so on. But I want to move to more dynamic solution which regardless of number of items in array it shows a table on a dashboard.
yeah those didnāt help either. I wish the json attribute parsing logic had something simpler like ā$ā that refers to full json instead of having to have a key at the root level.