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!
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.