Extract value from json

so where does the index come into it in Tom’s and Petro’s examples? seems to not need it?

The default is to return the zeroth index (i.e. numerically first matching result).

If the capturing group were to find multiple matches, it will return a list of them. Then you need to specify index to indicate which one you want.

In this example, the capturing group returns three matches:

If I wanted the second result in the list (bat) I need to indicate index=1 (the list is zero-based so 0, 1, 2, 3, etc).

{{ my_test_json | regex_findall_index('\"state\" : (\d+)', index=1)  }}
1 Like

ahhhhh so in Tom’e one the first digit would be the “code” and the second, index=1 would be “state” which is why Petro’s example is better. What was doing my head in in Tom’s one was nowhere did he specify state but now it’s clicking into place.

Thanks Taras!

1 Like

Did you try the json attributes field in the rest sensor? That would make this a ton easier if it works.

Yes I used your rest sensor as per your post and it works. Thanks again.

If that’s the case, grabbing info out of the attributes would be:

states.sensor.motion_test.attributes['0'].cmd

Maybe I misunderstood you. I’m only getting the “state” attribute with that rest command and it is coming in as the state.

we really need to update the rest sensor to have json_attributes_template like the MQTT sensor. We wouldn’t need regex.

value_template: '{{ value_json["value"]["state"] }}'

1 Like

it’s a list, he’s trying to parse it in another sensor, not the rest one :wink:

Hi @DavidFW1960 I am just playing around with the same thing and I assume you have a Reolink camera trying to figure out when it detects motion. May I ask to post your latest and greatest configuration here. Thanks, Thomas

I’m stunned I posted this thread! I don’t have any cameras. Maybe I was just posting that first post as an example as I was trying to work out how to extract something from a Json string… Anyway, I can’t really help sorry. This is a pretty old thread actually and my memory isn’t that good to determine what I was trying to do at the time.

Hi DavidFW1960, I think you could do something like this:

x = my_test_json|to_josn
x[0].value.state

Could you tell me how you got the json from the device in the first place?

I just looked at my first post and I must have been using an example from someone else there because I don’t have any cameras. Actually it might be from a client I was doing some work for. Anyway at the time Petro solved it for me but thanks

Hello i have problem too:

my json:

{“settingsVer”:492786,“editableParamsVer”:90300,“schedulesVer”:2,“remoteMenuVer”:{},“curr”:{“T4”: 21.68, “T2”: 24.359999999999999, “T3”: 43.829999999999998, “Uzysk_ca_kowity”: 12210.049999999999, “T1”: 33.039999999999999}, “currUnits”:{“T4”: 1, “T2”: 1, “T3”: 1, “Uzysk_ca_kowity”: 64, “T1”: 1}, “currNumbers”:{“T4”: 4, “T2”: 2, “T3”: 3, “Uzysk_ca_kowity”: 15, “T1”: 1}, “schemaParams”:{“schema_T1”: [[[“41.4”, 1, 0]], true, true], “schema_T3”: [[[“43.8”, 1, 0]], true, true], “schema_T2”: [[[“24.4”, 1, 0]], true, true], “schema_T5”: [[["-246.9", 1, 0]], false, true], “schema_T4”: [[[“21.7”, 1, 0]], true, true], “schema_T6”: [[[“24.2”, 1, 0]], true, true], “schema_H”: [[[“0.0”, 0, 0]], true, true], “schema_P2”: [[[“0.0”, 2, 0]], true, true], “schema_P1”: [[[“77.0”, 2, 0]], true, true], “KolektorA_1”: [[[“77.0”, 2, 0]], true, true], “KolektorA_2”: [[[“77.0”, 2, 0]], false, true], “KolektorA_3”: [[[“77.0”, 2, 0]], false, true]}, “tilesParams”:[[[[“24.4”, 1, 0]], true, true, [[“70.0”, 1, 0]], “|”, 0, 5001], [[[“43.8”, 1, 0]], true, false, [[“30.0”, 1, 0]], “|”, 0, 5002], [[[“33.0”, 1, 0]], true, true, null, null, null, null], [[[“43.8”, 1, 0]], true, false, null, null, null, null], [[[“0”, 0, 1]], true, true, null, null, 1, 5005], [[[77, 2, 0]], true, false, null, null, null, null], [[[“1”, 0, 1]], true, false, null, null, 1, 5007], [[[“1”, 0, 1]], true, false, null, null, 1, 5008], [[[“2.5”, 10, 0]], true, true, null, null, null, null], [[[“off”, 0, 1]], true, false, null, null, null, null], [[[“0”, 0, 1]], true, false, null, null, 1, 5011]]}

my code:

  • platform: rest

    name: Steam System Data

    resource: http://xxxx:[email protected]/econet/regParams

    json_attributes_path: “$.schemaParams”

    scan_interval: 15

    value_template: “OK”

    json_attributes:

    - "schema_T1"
    
    - "schema_T2"
    
    • platform: template

      sensors:

      steam_temp:

      friendly_name: Steam Temp
      
      value_template: "{{ state_attr('sensor.steam_system_data', 'schema_T1')}}"
      
      device_class: temperature
      
      unit_of_measurement: "°C"
      

the result of the action is: Steam Temp [[[‘41.4’, 1, 0]], True, True] °C
but I would like to Steam Temp 41,4 °C

value_template: "{{ state_attr('sensor.steam_system_data', 'schema_T1')[0][0][0] }}"

Thank You!

I did it with that simple:
value_template: '{{ value_json[0]["value"]["state"] }}'

Hi there. Please, can somebody help me?
I want to get all title-values from json-file about roadworks https://verkehr.autobahn.de/o/autobahn/A2/services/roadworks and presenting in a card.

Actually, I don’t know to start in configuration.yaml (rest sensor) and in card (maybe markdown card).

Have somebody expierence?

Add this to your configuration.yaml

rest:
  - resource: 'https://verkehr.autobahn.de/o/autobahn/A2/services/roadworks'
    scan_interval: 20
    sensor:
      - name: value0
        value_template: '{{ value_json.roadworks[0].title }}'
      - name: value1
        value_template: '{{ value_json.roadworks[1].title }}'

etc.

You can use the sensors in any card you like.
The scan_interval can be changed in a time frame you want.