Is it possible to create entities from an array, like this one? One for each item named with the key and the number as the value.
[{'TypeScript': 78.03}, {'JavaScript': 21.97}]
Is it possible to create entities from an array, like this one? One for each item named with the key and the number as the value.
[{'TypeScript': 78.03}, {'JavaScript': 21.97}]
If you know which will be there, yes. Dynamically creating entities, probably no. But where do you get the json from? Without more info, it is impossible to help you. But this is how you get at the values if you know what is there. You can test it in the template tool in Developer tools:
{% set j = [{'TypeScript': 78.03}, {'JavaScript': 21.97}] %}
{{ j[0]['TypeScript'] }}
{{ j[1]['JavaScript'] }}
You could add them as attributes in a sensor.
But I highly doubt it’s possible to make dynamic entities
Thanks @Edwin_D and @Hellis81 for your replies.
The data actually comes from Wakapi API, where I’d like to get the name
, percent
and total_seconds
for each of editors
, languages
, machines
and projects
to show in graphs in the frontend. Any suggestions on how to accomplish this from the data I get from the API?
I did save a json as a value_template
with the following code, and why I wanted to parse that data somehow, hence my first post. If you have other suggestions on how to solve this I would be very greatful!
rest:
- authentication: basic
resource: !secret wakapi_url
method: GET
headers:
content-type: "application/json"
Authorization: !secret wakapi_auth
scan_interval: 3600
sensor:
- name: "Wakapi languages"
unique_id: wakapi_languages
icon: mdi:code-block-braces
json_attributes:
- languages
value_template: >-
{% set ns = namespace(arr=[]) %}
{% for item in value_json.data.languages %}
{% set ns.arr = ns.arr + [{item['name']: item['percent']}] %}
{% endfor %}
{{ ns.arr }}
As we both said: you cannot create dynamic entities based on whatever the api spits out. You can create a sensor to look for something specific that you know is there, and use jinja to extract the specific value you are after. As suggested, put the json in an attribute, then create sensors for each explicit value you are after and extract the one value you need for that sensor. But HA will not create entities on the fly when new data comes in. You have to write the code for each sensor.