Method to select required value from JSON data in an automation

Hiya amazing people!

I’m struggling to find a way to pull a single value from a string of JSON returned by a rest command.

I’m attempting to build an automation that is triggered by a barcode scan and then adds the scanned product to a shopping list. I’m yet to purchase the esphome bardocde scanner (in case I can’t make it work) so I’m triggering the automation with an Atom S3 Lite.

I’ve got a text input field that holds the barcode value as a proxy for the barcode scan. I have a rest command that successfully looks up the barcode and returns a bunch of product detail. Searching through the forums I’m led to believe I need to use value_json to select the value relating to a specified attribute or field.

Initially I tried adding this into the rest command definition but the config checking reported that templates are not supported in rest commands.

Next option was to parse the required value out when passing the automation variable into an input text field as per the automation definition below.

- id: '1709784557149'
  alias: Barcode to Shopping LIst
  description: Triggered by barcode scan, does a lookup and adds the item to the Shopping
    List
  trigger:
  - type: turned_on
    platform: device
    device_id: c9a77481ba8321b7ca2146272752e6e9
    entity_id: 46c255bf6b1d26a106e56642507b2553
    domain: binary_sensor
  condition: []
  action:
  - service: rest_command.barcode_lookup
    data: {}
    response_variable: barcode_product
  - service: input_text.set_value
    metadata: {}
    data:
      value: '{{ value_json.title(''barcode_product'') }}'
    target:
      entity_id: input_text.product_title
  - service: todo.add_item
    metadata: {}
    data:
      item: '{{ states(''input_text.product_title'') }}'
    target:
      entity_id: todo.shopping_list
  mode: single

Looking at the trace for the automation, I’m seeing this error.

Error: Error rendering data template: UndefinedError: 'value_json' is undefined

Is there a different action I should be using to select the required value from the returned data?

Appreciate any direction the brains trust can offer!

You put the response of the rest call in a variable named barcode_product, it stands to reason that you should use that variable to get at the result.

Examples are in the documentation:

Thanks @Edwin_D

The rest command will return a bunch of data as per the example below.

content:
  added_time: "2022-09-04 16:25:16"
  modified_time: "2022-09-04 16:25:16"
  title: Edgell Black Beans
  alias: null
  description: Edgell Black Beans
  brand: Edgell
  manufacturer: null
  msrp: null
  ASIN: null
  category: Food
  categories: >-
    Plant-based foods and beverages, Plant-based foods, Legumes and their
    products, Canned foods, Legumes, Seeds, Canned plant-based foods, Legume
    seeds, Pulses, Common beans, Canned legumes, Black beans, Canned vegetables
  stores: null
  barcode: "9310082108657"
  success: true
  timestamp: 1709777290
  images: null
  metadata:
    stores: Woolworths, Coles
    quantity: 400g
    countries: Australia
    ingredients: >-
      Black beans (60%), water, sugar, salt, tood acid (acetic acid), firming
      agent (509).
  metanutrition:
    fat: "1"
    salt: "0.7493"
    fiber: "9.3"
    energy: "426"
    sodium: "0.29972"
    sugars: "1.9"
    fat_100g: "1"
    fat_unit: g
    proteins: "8.5"
    energy-kj: "426"
    fat_value: "1"
    salt_100g: "0.749"
    salt_unit: mg
    fiber_100g: "9.3"
    fiber_unit: g
    nova-group: "4"
    salt_value: "749.3"
    energy-kcal: "101.82"
    energy_100g: "426"
    energy_unit: kj
    fat_serving: "1"
    fiber_value: "9.3"
    sodium_100g: "0.3"
    sodium_unit: mg
    sugars_100g: "1.9"
    sugars_unit: g
    alcohol_unit: "% vol"
    energy_value: "426"
    salt_serving: "0.7493"
    sodium_value: "299.72"
    sugars_value: "1.9"
    carbohydrates: "12.7"
    fiber_serving: "9.3"
    proteins_100g: "8.5"
    proteins_unit: g
    saturated-fat: "0.2"
    energy-kj_100g: "426"
    energy-kj_unit: kj
    energy_serving: "426"
    proteins_value: "8.5"
    sodium_serving: "0.29972"
    sugars_serving: "1.9"
    energy-kj_value: "426"
    nova-group_100g: "4"
    energy-kcal_100g: "102"
    energy-kcal_unit: kcal
    proteins_serving: "8.5"
    energy-kcal_value: "101.82"
    energy-kj_serving: "426"
    carbohydrates_100g: "12.7"
    carbohydrates_unit: g
    nova-group_serving: "4"
    nutrition-score-fr: "-6"
    saturated-fat_100g: "0.2"
    saturated-fat_unit: g
    carbohydrates_value: "12.7"
    energy-kcal_serving: "101.82"
    saturated-fat_value: "0.2"
    carbohydrates_serving: "12.7"
    saturated-fat_serving: "0.2"
    nutrition-score-fr_100g: "-6"
status: 200

So I’m assuming all of that will be in the variable barcode_product.

I was hoping that by setting the text input for product_title from the variable barcode_product using:

      value: '{{ value_json.title(''barcode_product'') }}'

it would select just the content of the title attribute - namely “Edgell Black Beans”

The value_json component of that value statement is returning the error.

But maybe I’ve misunderstood your comment?

Looking again at the RESTful command documentation again, I noticed a different syntax for working with variables than reading the state of an entity.

So I tried changing the syntax for the value in the command to set the text input field.

value: "{{ barcode_product['title'] }}"

That’s not reporting an error like the previous syntax, but sadly is not returning a value into the required text field. Interestingly, the config above shows up as

value: '{{ barcode_product[''title''] }}'

In the automation traces. Not sure why the extra pair of '' is being added.

I also tried

value: '{{ barcode_product[title] }}'

As other examples on the config page showed not quotes at all. Neither option is successful.

I think you forgot the outer content tag from the rest response.

value: "{{ barcode_product['content']['title'] }}"

@Edwin_D That has solved it! I think I understand the syntax. I’ll need to ponder on that some more to be sure.

Appreciate your assistance.

1 Like