Hey there,
probably this problem ist easy to solve, but after hours of trying, reading and talking to ChatGPT you are my last chance!
I would like to write some data into a json string and display this data ina table in my dashboard. However, I cannot create a working json variable. It always stays a string and I cannot properly access the entries. Here is the code of my test automatisation, which is working but not as I want to
One of these variables is supposed output json, the other is not. Later you want to combine them as is, but the types differ.
Besides the fact that the first template will give an error (always test in developer tools: it is to_json, not tojson), it shouldn’t be needed because variables and attributes can be any type. They do nit need to be json. So try to remove both filters and try to keep them objects. Only use to_json and from_json if you need it to be string and convert back from string.
If you do not have a defined trigger, it is best practice to use a Script not an Automation. There is a long history of users experiencing reliability issues using automations without defined triggers.
The hass-variables/home-assistant-variables custom integrations haven’t received any major updates in years and are now pretty far behind the abilities of the core Template integration.
Removed to_json and from_json in the code. However, now I get the error message:
TypeError: can only concatenate list (not “str”) to list
for this part of the code
If the attribute is currently a string you either must discard it or do a from_json once and store it as data .that is much easier to work with if you want to create a markdown table.
Maybe put this in temporarily above where you add the new and old log together.:
{% if old_log is string %}
{% set old_log = old_log | from_json %}
{% endif %}
Let me explain, what I want to do, maybe that makes it more clear:
I have an irrigation system. Now I want to track every irrigation process. The last 10 watering sessions should then be displayed in a table in the dashboard.
I don’t know what the best way to implement this is. At first I thought via a input_text helper, but it can only store 256 characters in the state, which is not enough for 10 entries. That’s why I thought it is a good idea to store the entries as an attribute of a variable.
“old_log:” is always empty at the beginning, but of course it should grow with each execution of the script until it has 10 entries, and then delete the oldest one whenever a new one is added.
If it is empty at start (as in empty list, not undefined) then you are fine, but your initial post showed a log that looked like it was a string, representing json.
Yes, that was already after some iterations.But now: If it is empty first time executing the script works. Second time I get the same error message as posted above…
Test the append template in developer tools. See what type the old data is there. According the the error you got it is still a string, not an empty list. Maybe an empty string?
You do not need to use from_json to assign a dictionary as the value of a variable and attributes can hold non-string data types, so you shouldn’t need to do any conversions.
Please post a link to the custom integration you are actually using, there are a number of versions out there some of which use non-standard configuration syntax…
I think I have to change the approach… I now try to use the attribute of a template sensor. However, I think I have a similar problem, which might be in line with the problem I had already.
This code works in the template-editor:
{% set temp = '{"temperature": 25, "unit": "°C"}'|from_json %}
The temperature is {{ temp.temperature }}{{ temp.unit }}
This code does not:
{% set temp = '{"start": states('input_datetime.bewaesserung_start')}'|from_json %}
Start at {{ temp.start }}
Any idea why this does not work and how to solve it?