Help with a REST sensor with JSON data

Hi everyone. I used to have a fully functional Mealie integration. Since Mealie upgraded to version 1.0 their API changed quite excessively. Now my integration is broken.

sensor:
  - platform: rest
    resource: https://SCRUBBED/api/groups/mealplans/today
    method: GET
    name: Aandete vanaand
    headers:
      Authorization: Bearer SCRUBBED
      #value_template: '{{ value_json.recipe.name | default }}'
    value_template: '{{ value_json.recipe | default }}'
    scan_interval: 300
    json_attributes:
      - slug
      - name

If I go to developer tools and input the following:

{% set value_json = {
  "date":"2023-07-11","entryType":"dinner","title":"","text":"","recipeId":"6ebd3831-8e33-47c2-b747-0dfca86df4aa","id":1,"groupId":"25bc5258-d60e-4a1b-a526-e664dd65d9cb","userId":"b3b8b456-b62b-4a60-9956-917a152d3c0b","recipe":{"id":"6ebd3831-8e33-47c2-b747-0dfca86df4aa","userId":"542307e7-0480-4145-a500-cbe55fc96e3e","groupId":"25bc5258-d60e-4a1b-a526-e664dd65d9cb","name":"Egg Fried Rice","slug":"egg-fried-rice","image":"egg-fried-rice","recipeYield":"4 serving(s)","totalTime":"15 Minutes","prepTime":"10 Minutes","cookTime":null,"performTime":"5 Minutes","description":"","recipeCategory":[{"id":"6f344325-a30b-4632-a016-7f81e306dc72","name":"Vinnige Aandete","slug":"vinnige-aandete"}],"tags":[],"tools":[],"rating":null,"orgURL":"https://www.foodnetwork.com/recipes/egg-fried-rice-2104339","dateAdded":"2023-01-09","dateUpdated":"2023-07-11T08:40:55.613456","createdAt":"2023-07-11T08:40:55.617002","updateAt":"2023-07-11T08:40:55.617017","lastMade":null}
} %}

{{ value_json.recipe.name }}

then I get the correct output, in this case “Egg Fried Rice”.

Without the default part in my value_template, I get warnings in my logs. With it in there, there are no errors and the thing is, HA actually gets the whole json back from Mealie.

What I would love to do is to have the name displayed in HA on an entity card. I used to have the picture being pulled through as well, but Mealie changed the location of the picture of the recipe, and now I will need more complex requests to get the picture entity of the recipe for the dish of today.

Later on it would be really cool if I can pull the recipe into HA as well to have that displayed. Then I don’t have to go to other locations, and can access everything inside HA itself.

Could someone please tell me what I am doing wrong? Why am I not seeing the name on my entity card for my sensor? It is just empty.

post the contents of the returned json. Also, post errors in your logs.

Just something I noticed based on what you did post, this portion is wrong

and could be the root of your problems.

You’d want it to be

    json_attributes:
      - recipe

Thank you for your response.

2023-07-13 08:07:28.517 DEBUG (MainThread) [homeassistant.components.rest.data] Data fetched from resource: [{"date":"2023-07-13","entryType":"dinner","title":"","text":"","recipeId":"18ed68b9-62a9-4205-b6c5-ea0009ae58af","id":2,"groupId":"25bc5258-d60e-4a1b-a526-e664dd65d9cb","userId":"b3b8b456-b62b-4a60-9956-917a152d3c0b","recipe":{"id":"18ed68b9-62a9-4205-b6c5-ea0009ae58af","userId":"542307e7-0480-4145-a500-cbe55fc96e3e","groupId":"25bc5258-d60e-4a1b-a526-e664dd65d9cb","name":"Lemon-Herb Rice Salad","slug":"lemon-herb-rice-salad","image":"lemon-herb-rice-salad","recipeYield":"6 serving(s)","totalTime":"50 Minutes","prepTime":"45 Minutes","cookTime":null,"performTime":"5 Minutes","description":"","recipeCategory":[{"id":"6f344325-a30b-4632-a016-7f81e306dc72","name":"Vinnige Aandete","slug":"vinnige-aandete"},{"id":"cdee7102-106c-4eb7-a7c4-24a11c0fdc21","name":"Gesond","slug":"gesond"}],"tags":[],"tools":[],"rating":null,"orgURL":"https://www.foodnetwork.com/recipes/food-network-kitchen/lemon-herb-rice-salad-recipe-2107971","dateAdded":"2023-01-09","dateUpdated":"2023-07-11T08:40:55.343611","createdAt":"2023-07-11T08:40:55.347766","updateAt":"2023-07-11T08:40:55.347776","lastMade":null}}]

No errors in the logs as I said.

If I change my config to your example, I can see the recipe dictionary as an attribute in HomeAssistant. Problem is, it is single attribute. I want to extract only the name from this dictionary. In other words, I would like to display only the name of the meal on the card. From the example json returned today, I want to display Lemon-Herb Rice Salad.

value_template: "{{ value_json['recipe']['name'] }}"

Thank you! Unfortunately, I only get Unknown in my entity card.

Logs:

2023-07-13 08:48:28.932 DEBUG (MainThread) [homeassistant.components.rest.data] Updating from https://SCRUBBED/api/groups/mealplans/today

2023-07-13 08:48:28.938 INFO (MainThread) [homeassistant.components.automation.batteries_floating] Automations Reloaded: Running automation actions

2023-07-13 08:48:28.938 INFO (MainThread) [homeassistant.components.automation.batteries_floating] Automations Reloaded: Executing step call service

2023-07-13 08:48:28.938 INFO (MainThread) [homeassistant.components.sensor] Setting up sensor.template

2023-07-13 08:48:29.048 DEBUG (MainThread) [homeassistant.components.rest.data] Data fetched from resource: [{“date”:“2023-07-13”,“entryType”:“dinner”,“title”:“”,“text”:“”,“recipeId”:“18ed68b9-62a9-4205-b6c5-ea0009ae58af”,“id”:2,“groupId”:“25bc5258-d60e-4a1b-a526-e664dd65d9cb”,“userId”:“b3b8b456-b62b-4a60-9956-917a152d3c0b”,“recipe”:{“id”:“18ed68b9-62a9-4205-b6c5-ea0009ae58af”,“userId”:“542307e7-0480-4145-a500-cbe55fc96e3e”,“groupId”:“25bc5258-d60e-4a1b-a526-e664dd65d9cb”,“name”:“Lemon-Herb Rice Salad”,“slug”:“lemon-herb-rice-salad”,“image”:“lemon-herb-rice-salad”,“recipeYield”:“6 serving(s)”,“totalTime”:“50 Minutes”,“prepTime”:“45 Minutes”,“cookTime”:null,“performTime”:“5 Minutes”,“description”:“”,“recipeCategory”:[{“id”:“6f344325-a30b-4632-a016-7f81e306dc72”,“name”:“Vinnige Aandete”,“slug”:“vinnige-aandete”},{“id”:“cdee7102-106c-4eb7-a7c4-24a11c0fdc21”,“name”:“Gesond”,“slug”:“gesond”}],“tags”:,“tools”:,“rating”:null,“orgURL”:“https://www.foodnetwork.com/recipes/food-network-kitchen/lemon-herb-rice-salad-recipe-2107971",“dateAdded”:“2023-01-09”,“dateUpdated”:“2023-07-13T08:29:19.465110”,“createdAt”:“2023-07-11T08:40:55.347766”,“updateAt”:“2023-07-13T08:29:19.471191”,“lastMade”:"2023-07-13T06:29:19.236677”}}]

Current config:

sensor:
  - platform: rest
    resource: https://SCRUBBED/api/groups/mealplans/today
    method: GET
    name: Aandete vanaand
    headers:
      Authorization: Bearer TOKEN_SCRUBBED
     
    value_template: "{{ value_json['recipe']['name'] }}"
    scan_interval: 300

Even changing it to:

sensor:
  - platform: rest
    resource: https://SCRUBBED/api/groups/mealplans/today
    method: GET
    name: Aandete vanaand
    headers:
      Authorization: Bearer TOKEN_SCRUBBED
     
    value_template: "{{ value_json['recipe']['name'] }}"
    scan_interval: 300
    json_attributes:
      - recipe

keeps the value on the card as Unkown.

Thank you again for your input.

This is what I mean with the Unknown part.

I should have noticed: that recipe response is a list. This will return the first one in the list.

value_template: "{{ value_json[0]['recipe']['name'] }}"

From what you pasted:

fetched from resource: [{"date":"2023-07-13",
-----------------------^

Your template editor code should have been:

{% set value_json = [{
  "date":"2023-07-11","entryType":"dinner","title":"","text":"","recipeId":"6ebd3831-8e33-47c2-b747-0dfca86df4aa","id":1,"groupId":"25bc5258-d60e-4a1b-a526-e664dd65d9cb","userId":"b3b8b456-b62b-4a60-9956-917a152d3c0b","recipe":{"id":"6ebd3831-8e33-47c2-b747-0dfca86df4aa","userId":"542307e7-0480-4145-a500-cbe55fc96e3e","groupId":"25bc5258-d60e-4a1b-a526-e664dd65d9cb","name":"Egg Fried Rice","slug":"egg-fried-rice","image":"egg-fried-rice","recipeYield":"4 serving(s)","totalTime":"15 Minutes","prepTime":"10 Minutes","cookTime":null,"performTime":"5 Minutes","description":"","recipeCategory":[{"id":"6f344325-a30b-4632-a016-7f81e306dc72","name":"Vinnige Aandete","slug":"vinnige-aandete"}],"tags":[],"tools":[],"rating":null,"orgURL":"https://www.foodnetwork.com/recipes/egg-fried-rice-2104339","dateAdded":"2023-01-09","dateUpdated":"2023-07-11T08:40:55.613456","createdAt":"2023-07-11T08:40:55.617002","updateAt":"2023-07-11T08:40:55.617017","lastMade":null}
}] %}

{{ value_json[0]['recipe']['name'] }}

(using the safer bracket notation instead of dot notation)

You are a genius! Thank you so very much!

Struggling with something similar here. I have a basic REST API, but I cannot figure out how to do the following.

  1. Create 3 Sensors for Status
    2.Create a button to move the lift up and down

Here is what I have so far.
Configuration.xml

rest:

sensor: !include_dir_merge_list cfg/_component/rest/sensor/

YAML File Locations

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
sensor: !include sensor.yaml
switch: !include switch.yaml

Custom yaml file and location: _compoenent/rest/sensor/tv_lift_sensor.yaml
sensor:

  • platform: rest
    name: “TV Lift”
    resource: http://192.168.xxx.xxx/api/status
    method: get
    value_template: “{{ value_json.STATUS }}”
    device_class: none
    scan_internval: 60
    json_attributes:
    • STATUS
    • VERTICAL
    • HORIZONTAL

Developer Tools:
Here is my testing to ensure I have the correct syntax in my template.

{## Imitate available variables: ##}
{% set my_test_json = {
“STATUS”: “OK”,
“VERTICAL”: “DOWN”,
“HORIZONTAL”: “NA”

} %}

TV lift is status is {{ my_test_json.STATUS }} and in the {{ my_test_json.VERTICAL }} position.

Error: Resource not set for RestData.

I am trying to follow these resources, but I think I am overcomplicating what I need to do in my code. I have tried to look at resources and just need guidance on where I am going wrong with my configuration. Thanks! :pray:

Referenced other threads and documentation already below.

Solved! in what I think is a clunky way, but it works. If anyone knows a better way to create a REST API Switch I would appreciate it.

Here is my test from Developer Tools: Template

{## Imitate available variables: ##}
{% set value_json = {
  "STATUS": "OK",
  "VERTICAL": "DOWN",
  "HORIZONTAL": "NA"
  
} %}

rest:
  - resource: "http://192.168.xxx.xxx/api/status"
    sensor:
      - name: "Status"
        value_template: "{{ value_json.STATUS}}"
 rest_command:
  tv_up:
    url: "http://102.168.xxx.xxx/api/command"
    method: POST
    payload: '{"COMMAND": "UP"}'
  tv_down:
    url: "http://192.168.xxx.xxx/api/command"
    method: POST
    payload: '{"COMMAND": "DOWN"}'

Configuration.XML

rest:
  - resource: "http://192.168.xxx.xxx/api/status"
    sensor:
      - name: "TV Lift Status"
        value_template: "{{ value_json.STATUS}}"
      - name: "TV Lift Vertical"
        value_template: "{{value_json.VERTICAL}}"
      - name: "TV Lift Hortizontal"
        value_template: "{{value_json.HORIZONTAL}}"
rest_command:
  tv_up:
    url: "http://192.168.xxx.xxx/api/command"
    method: POST
    payload: '{"COMMAND": "UP"}'
  tv_down:
    url: "http://192.168.xxx.xxx/api/command"
    method: POST
    payload: '{"COMMAND": "DOWN"}'

image