I’m running Grocy and I want to get the current meal of the day via the Grocy API. It puts out a JSON list with all the options. So I want to filter the result to get only the json of today.
I’ve used http://jsonpath.com/ to get working filter: $..[?(@.day=="2019-09-07")]
It gets the right JSON for me. But how to implement this JSONPath expression? Using a rest sensor with a value_template should do the trick. But I keep running into a configuration error for the filter character ‘$’ :
Invalid config for [sensor.rest]: invalid template (TemplateSyntaxError: unexpected char '$' at 15) for dictionary value @ data['value_template']. Got '{{ value_json.[$..[?(@.day=="2019-09-07")]] }}'. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.rest/
So how to implement the JSONpath expression in a value_template? It’s probably just a formatting way, but it’s one that’s hard to guess.
Below my code:
- platform: rest
name: "Grocy Meal plan"
resource: https://grocy.org/api/objects/meal_plan
headers:
GROCY-API-KEY: !secret GROCY-API-KEY
accept: application/json
json_attributes:
- day
- recipe_id
- servings
value_template: '{{ value_json.[$..[?(@.day=="2019-09-07")]] }}'
And below the JSON output of the API:
[
{
"id": "1",
"day": "2019-07-09",
"recipe_id": "2",
"row_created_timestamp": "2019-07-09 11:27:10",
"servings": "3"
},
{
"id": "2",
"day": "2019-07-10",
"recipe_id": "7",
"row_created_timestamp": "2019-07-09 11:27:21",
"servings": "3"
},
{
"id": "3",
"day": "2019-07-11",
"recipe_id": "9",
"row_created_timestamp": "2019-07-09 11:27:29",
"servings": "3"
},
{
"id": "4",
"day": "2019-07-08",
"recipe_id": "11",
"row_created_timestamp": "2019-07-09 11:27:53",
"servings": "3"
},
{
"id": "5",
"day": "2019-07-07",
"recipe_id": "10",
"row_created_timestamp": "2019-07-09 11:28:12",
"servings": "2"
},
{
"id": "6",
"day": "2019-07-06",
"recipe_id": "8",
"row_created_timestamp": "2019-07-09 11:28:24",
"servings": "3"
},
{
"id": "7",
"day": "2019-08-03",
"recipe_id": "4",
"row_created_timestamp": "2019-08-03 08:43:55",
"servings": "3"
},
{
"id": "8",
"day": "2019-08-04",
"recipe_id": "11",
"row_created_timestamp": "2019-08-03 08:44:28",
"servings": "3"
},
{
"id": "9",
"day": "2019-08-05",
"recipe_id": "30",
"row_created_timestamp": "2019-08-03 08:44:44",
"servings": "3"
},
{
"id": "10",
"day": "2019-08-06",
"recipe_id": "2",
"row_created_timestamp": "2019-08-03 08:45:11",
"servings": "3"
},
{
"id": "11",
"day": "2019-08-07",
"recipe_id": "12",
"row_created_timestamp": "2019-08-03 08:45:25",
"servings": "3"
},
{
"id": "12",
"day": "2019-08-08",
"recipe_id": "3",
"row_created_timestamp": "2019-08-03 08:45:40",
"servings": "3"
},
{
"id": "15",
"day": "2019-08-14",
"recipe_id": "8",
"row_created_timestamp": "2019-08-10 12:51:36",
"servings": "3"
},
{
"id": "16",
"day": "2019-08-12",
"recipe_id": "2",
"row_created_timestamp": "2019-08-10 12:51:53",
"servings": "4"
},
{
"id": "18",
"day": "2019-08-15",
"recipe_id": "7",
"row_created_timestamp": "2019-08-10 12:52:40",
"servings": "3"
},
{
"id": "19",
"day": "2019-08-22",
"recipe_id": "10",
"row_created_timestamp": "2019-08-17 09:33:04",
"servings": "3"
},
{
"id": "20",
"day": "2019-08-21",
"recipe_id": "12",
"row_created_timestamp": "2019-08-17 09:33:13",
"servings": "3"
},
{
"id": "21",
"day": "2019-08-20",
"recipe_id": "2",
"row_created_timestamp": "2019-08-17 09:33:36",
"servings": "3"
},
{
"id": "22",
"day": "2019-08-19",
"recipe_id": "4",
"row_created_timestamp": "2019-08-17 09:33:52",
"servings": "3"
},
{
"id": "24",
"day": "2019-08-18",
"recipe_id": "3",
"row_created_timestamp": "2019-08-17 09:43:36",
"servings": "3"
},
{
"id": "25",
"day": "2019-08-28",
"recipe_id": "11",
"row_created_timestamp": "2019-08-23 19:09:49",
"servings": "3"
},
{
"id": "26",
"day": "2019-08-26",
"recipe_id": "2",
"row_created_timestamp": "2019-08-23 19:10:03",
"servings": "3"
},
{
"id": "27",
"day": "2019-08-29",
"recipe_id": "10",
"row_created_timestamp": "2019-08-23 19:10:29",
"servings": "3"
},
{
"id": "28",
"day": "2019-08-27",
"recipe_id": "7",
"row_created_timestamp": "2019-08-23 19:12:10",
"servings": "3"
},
{
"id": "29",
"day": "2019-08-25",
"recipe_id": "25",
"row_created_timestamp": "2019-08-23 21:56:42",
"servings": "4"
},
{
"id": "30",
"day": "2019-09-02",
"recipe_id": "2",
"row_created_timestamp": "2019-08-30 19:39:14",
"servings": "3"
},
{
"id": "31",
"day": "2019-09-04",
"recipe_id": "8",
"row_created_timestamp": "2019-08-30 19:41:13",
"servings": "3"
},
{
"id": "32",
"day": "2019-09-05",
"recipe_id": "10",
"row_created_timestamp": "2019-08-30 19:41:24",
"servings": "3"
},
{
"id": "33",
"day": "2019-09-03",
"recipe_id": "36",
"row_created_timestamp": "2019-08-30 19:48:15",
"servings": "3"
},
{
"id": "34",
"day": "2019-09-09",
"recipe_id": "2",
"row_created_timestamp": "2019-09-07 08:59:52",
"servings": "3"
},
{
"id": "35",
"day": "2019-09-11",
"recipe_id": "12",
"row_created_timestamp": "2019-09-07 09:00:02",
"servings": "3"
},
{
"id": "36",
"day": "2019-09-10",
"recipe_id": "30",
"row_created_timestamp": "2019-09-07 09:00:16",
"servings": "3"
},
{
"id": "37",
"day": "2019-09-07",
"recipe_id": "4",
"row_created_timestamp": "2019-09-07 09:00:41",
"servings": "3"
},
{
"id": "38",
"day": "2019-09-13",
"recipe_id": "40",
"row_created_timestamp": "2019-09-07 09:04:02",
"servings": "4"
},
{
"id": "40",
"day": "2019-09-08",
"recipe_id": "42",
"row_created_timestamp": "2019-09-07 09:10:57",
"servings": "1"
},
{
"id": "41",
"day": "2019-09-12",
"recipe_id": "41",
"row_created_timestamp": "2019-09-07 09:11:37",
"servings": "3"
}
]