#Remove square brackets from result

1 messages ยท Page 1 of 1 (latest)

desert kite
#

Hi!

I apologize in advance for a simple question.
I have got:

value_template: "{{ value_json.data.sensors.0.meters.1.vals }}"

I got the result:
[309.112]
I did not find answer on https://jinja.palletsprojects.com/en/latest/
Help me please remove square brackets.

keen sphinx
#

that is a list

#

if you just want the first item, add |first

desert kite
#

Is it correct?
value_template: "{{ value_json.data.sensors.0.meters.1.vals|first }}"

keen sphinx
#

yes

#

you can always just try it ๐Ÿ™‚

desert kite
# keen sphinx yes

Thank you very much for idea. ๐Ÿ‘ I did such way.

value_template: "{{ value_json.data.sensors.0.meters.1.vals.0 }}"

keen sphinx
#

that's one way

#

what I suggested should also work

desert kite
# keen sphinx what I suggested should also work

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 %}
keen sphinx
#

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()

desert kite
#

sensor.saures_login_status is not realy sensor. It is string result of API answer

keen sphinx
#

doesn't matter, it's an entity

desert kite
desert kite
keen sphinx
#

ok

#

you can always just try it ๐Ÿ™‚

desert kite
desert kite
keen sphinx
#

"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

desert kite
# keen sphinx 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)?

keen sphinx
#

it's fine, it's just a string

#

again, does it work?

desert kite
#

I don't want string or 0 (zero). I would like Not-a-Number ))

keen sphinx
#

what are you planning to do with NaN?

desert kite
keen sphinx
#

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

desert kite
keen sphinx
#

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?

desert kite
#

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": {}
}

keen sphinx
#

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

desert kite
desert kite
# keen sphinx I don't know what you're trying to solve
  1. 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": {}
}