in short, I wont to build a report for a clothes washer. My problem is to store data to create the report. I have a plug to monitor the power usage of the clothes washer. I use a automation to find out if it is washing, trying, etc. The result is represented by a helper. This part is working.
Now, I want to record the power consumption and time whenever the (calculated) state of the clothes washer changes. I have not found a easy solution to do this by the automation.
My current Idea is to add a template via configuration.yaml. If I understand correctly I can use a attribute to store the data.
Question 1:
Is it possible to set the attribute via an automatization to avoid to have a template with logic.
Question 2:
How to pars the data? I want to store a set of triples. A triple is created if the state of the clothes washer changes. It holds the time, state, and consumption. So it looks like this [“standby”, 1, 0]. A list wit 3 different entries. In configuration.yaml I store the data in tmp and try to store it in the attribute. So have an attribute like this:
{{ [ tmp ] | to_json }}
So, the attribute should be json formatted list withe 1 list with 3 entries. If I open the template via the HA GUI it looks like this (log is the name of the attribute):
log:
- - standby
- 1
- 0
The structure looks ok, but it is yaml not json syntax. If I try to reuse the attribute I can not append data. Something like this fails: {{ (states.sensor.template_name.attributes.log | from_json) + tmp }}
So, howe to store the json correctly, or how to fix the problem otherwise.
You may want to take a look at the custom integrations WashData or MeasureIt… either of them may already do something similar to what you are trying to do.
The 2nd option gives an configuration error. The 1st is not working, I need to rad the date from sensors, so I need the {{}} to have interpretation of the variables.
Your mistake is converting it to json or anything like that. The value is already properly typed as an attribute. You just need to use it. i.e. you can use this in the template editor to see it “work”
{% set tmp = ['test', 0, 0] %}
{{ (state_attr('sensor.template_name', 'log') or []) + [tmp] }}
Works! Thanks!
I have no idea what’s the difference. The DevTools → Template testing in the HA GUI gives the same output for your solution and my my not working idea.
(I mean the difference between state_attr() and states. I tried different versions with and without parsing to json.)
or [] this is a very helpful advice. I ignored some cases because a messed up report is not a big problem.
I still not understand why {{ (state_attr('sensor.template_name', 'log')) }}
and {{ states.sensor.template_name.attributes.log }}
give the same result in the HA GUI and the 2nd fails in configuration.yaml?
I don’t know if I missed an problems with the lists but my version just failed in reading the attribute. So the attribute disappeared and I could not check correct it.
Sorry if the question sounds strange, I try to understand some concepts.