Extracting data from Response Variable

I try to work with a value in a Response Variable, but I don’t get it to work.

I define a variable called status in an automation, send the command and this is the result as found under Traces > Changed variables:

status:
  results:
    - device_id: xxx
      device_name: OpenWrt1
      success: true
      command: ubus call network.interface.wg_wan status
      stdout:
        - '{'
        - "\t\"up\": false,"
        - "\t\"pending\": false,"
        - "\t\"available\": true,"
        - "\t\"autostart\": false,"
        - "\t\"dynamic\": false,"
        - "\t\"proto\": \"wireguard\","
        - "\t\"data\": {"
        - "\t\t"
        - "\t}"
        - '}'
      stderr: []
      code: 0

I just need the boolean value of up checked, something like this:

{{ status['stdout']['\t\"up\'] == false }}

Then I would set a switch accordingly and/or send out a command to up the interface.

But I’m a bit confused how to handle all those \ and " in the response - obviously not like this - but I tried several variants but to no success. Can somebody point me in the right direction :slight_smile:

Anybody who can help, I just want to extract if up is false or true :slight_smile:

You are missing a couple steps to get to stdout

{{ status['results'][0]['stdout'] }}

Then you have to deal with that garbled mess of a list… If that’s what is returned consistently, the quick and dirty way is just to search through the list items for “up” and check it for “false”:

{{ 'false' in status['results'][0]['stdout']|select('search', 'up')|first }}

Or you could rebuild the json by joining the list items and replacing the tabs:

{{ (status['results'][0]['stdout']|join|replace('\t','')|from_json)['up'] == false }}

Thanks a lot @Didgeridrew

Both your solutions work perfect :slight_smile:

Maybe you could help me out another time @Didgeridrew

I want to get some information from a attribute as well, and store that.

Prepare for an even more garbled mess…

wan:
  results:
    - device_id: xxx
      device_name: OpenWrt1
      success: true
      command: ubus call network.interface.wan status
      stdout:
        - '{'
        - "\t\"up\": true,"
        - "\t\"pending\": false,"
        - "\t\"available\": true,"
        - "\t\"autostart\": true,"
        - "\t\"dynamic\": false,"
        - "\t\"uptime\": 177408,"
        - "\t\"l3_device\": \"eth1\","
        - "\t\"proto\": \"dhcp\","
        - "\t\"device\": \"eth1\","
        - "\t\"metric\": 0,"
        - "\t\"dns_metric\": 0,"
        - "\t\"delegation\": true,"
        - "\t\"ipv4-address\": ["
        - "\t\t{"
        - "\t\t\t\"address\": \"192.168.178.212\","
        - "\t\t\t\"mask\": 24"
        - "\t\t}"
        - "\t],"
        - "\t\"ipv6-address\": ["
        - "\t\t"
        - "\t],"
        - "\t\"ipv6-prefix\": ["
        - "\t\t"
        - "\t],"
        - "\t\"ipv6-prefix-assignment\": ["
        - "\t\t"
        - "\t],"
        - "\t\"route\": ["
        - "\t\t{"
        - "\t\t\t\"target\": \"0.0.0.0\","
        - "\t\t\t\"mask\": 0,"
        - "\t\t\t\"nexthop\": \"192.168.178.1\","
        - "\t\t\t\"source\": \"192.168.178.212/32\""
        - "\t\t}"
        - "\t],"
goes on and on...

I want to store the wan address (still testing, so it’s a local address atm).

First I came up with:

{{ wan['results'][0]['stdout']|select('search', 'ipv4-address')|first }}

Result: “ipv4-address”: [

Not sure why it would output this (not just [, or the whole block below), but ok, ipv4-address is listed before address, I get that part.

So, next try with unique attribute source: with some cosmetic surgery:

{{ wan['results'][0]['stdout']|select('search', 'source')|first|replace('"source": "','')|replace('/32"','') }}

This works, original result was “source”: “.168.178.212”.

But still I’d like to be able to directly get the ipv4-address > address. And maybe even understand how, but could not find a good explanation yet.

| select('search', 'ipv4-address') | first

That told it to select the first item from the list that had ‘ipv4-address’ in it, so that’s the item it gave you…

You’re going to need to use the rebuilding method. Just FYI, all I’m doing to figure these out is converting the response to json and setting it as a variable in the Template tool.

{% set stdout = wan['results'][0]['stdout'] | join | replace('\t','')|from_json %}
{{ stdout['pv4-address'][0]['address'] }}

You may also want to take a look at:

http://thomasloven.com/blog/2018/08/YAML-For-Nonprogrammers/

1 Like

Thanks again @Didgeridrew

I’ve read and bookmarked your link, it’s very usefull :slight_smile: