#Remove square brackets from result
1 messages ยท Page 1 of 1 (latest)
Is it correct?
value_template: "{{ value_json.data.sensors.0.meters.1.vals|first }}"
Thank you very much for idea. ๐ I did such way.
value_template: "{{ value_json.data.sensors.0.meters.1.vals.0 }}"
I'm sure it should.
This result is the value on the water meter. I didn't expect to see a list here. There should be a single meaning here. And your idea helped me understand the situation. It was very helpful.
Let me one more question ))
I would like set result of template to NaN if authorization is not OK. And set value_json.data[now().strftime('%Y-%m-%d')] if any other case.
Is it template correct or not?
value_template: > {% if states('sensor.saures_login_status') != 'ok' %} {{ NaN }} {% else %} {{ value_json.data[now().strftime('%Y-%m-%d')] }} {% endif %}
you need to indent the template block
otherwise, it's fine
{% if not is_state('sensor.saures_login_status', 'ok') %} is another way to write the first test
or you can use iif()
sensor.saures_login_status is not realy sensor. It is string result of API answer
doesn't matter, it's an entity
I see
Suddenly it worked without indent ๐ฏ
I don't understand this language absolutely ))
I do it always
"great that it worked for you"
you initally asked if it was correct seemingly without trying it, and yet it just worked
so there wasn't really a question
Because I saw different variants. For example
value_template: > {% if states('sensor.saures_login_status') != 'ok' %} NaN {% else %} {{ value_json.data[now().strftime('%Y-%m-%d')] }} {% endif %}
Is it correct (without brackets)?
I don't want string or 0 (zero). I would like Not-a-Number ))
what are you planning to do with NaN?
When authorization is successful, the water meter value will be saved in the database. If authorization is not successful, NaN will be saved.
who's consuming NaN and cares that it's literally NaN and not the string?
in other words, what is your goal for this data such that it matters?
in any case, I don't know if you can actually return NaN from Jinja
When authorization is NOT successful API returns this answer:
{
"status": "bad",
"errors": [
{
"name": "WrongSIDException",
"msg": "Wrongsid"
}
],
"data": {}
}
And I saw in the log: ValueError: could not convert string to float: ''
I'm not following the connection at all
you started with a template and now you're talking about an error from some API call. How are they related?
value_template: "{{ value_json.data.sensors.0.meters.1.vals.0 }}"
can returns 309.112 (authorization is successful) - volume
OR
can returns such JSON (authorization is not successful)
{
"status": "bad",
"errors": [
{
"name": "WrongSIDException",
"msg": "Wrongsid"
}
],
"data": {}
}
NaN is nowhere there
hence my question
I don't know what you're trying to solve
if you're just asking how to parse stuff rrom that JSON response, that's the question
outputting NaN doesn't seem to have any relationship to the problem
In case of unsuccessful authorization I would like replace JSON to NaN
I khow how to parse JSON
- 1st stage:
{
"status": "ok",
"errors": [],
"data": {
"sid": "xxxxx-xxxx-xxxx-xxxx-xxxx",
"role": 1,
"api": 1,
"telegram": true
}
}
**2a) If status is "ok" **
2nd stage answer will be :
{
"status": "ok",
"errors": [],
"data": {
"sensors": [
{
"meters": [
{"vals": [174.219] }, โฆ
**2b) If status is not "ok" **
2nd stage answer will be :
{
"status": "bad",
"errors": [
{
"name": "WrongSIDException",
"msg": "Wrongsid"
}
],
"data": {}
}