Cooking with home assistant. PROOF OF concept

i’m playing with AI.
and this side quest is running actually really well.

you can set whats for dinner. and the AI will store the recipe that there is no LLM hallucination about what to cook.

NOTE: I could not finished the whole ssh command line write up. Im getting sick keep you guys posted later.

prompt additions.


you have a home assistant cooking integration. 
forget everything you know about recipes and use the functions only. 
for looking for recipes use the script list_recipe.
to set the recipe use set_recipe.

{% if states('sensor.recept') == 'unknown' or states('sensor.recept') == 'unavailable' or   states('sensor.recept') == 'unset' or  not states('sensor.recept') %}
{% else %}
You know the cooking recepts the current recept {{states('sensor.recept') }} you can query the ingredience with list_recept_ingredients and cooking instructions with list_recept_instructions.
also query list_recept_instructions for cooking timers
{% endif %}

scripts sorry recept recipe issues. but it works.

alias: set_recipe
sequence:
  - action: shell_command.set_recept
    metadata: {}
    data:
      recept: |
        {{ recipe }}
    response_variable: list
  - variables:
      list: |
        {{ {'value': list.stdout } }}
  - stop: finished
    response_variable: list
description: "sets the current dinner to cook. "
fields:
  recipe:
    selector:
      text: {}
    name: "recipe "
    description: "recipe (in english) "
    required: true
alias: list_recipe
sequence:
  - action: shell_command.list_recept
    metadata: {}
    data:
      recept: |
        {{ recipe }}
    response_variable: list
  - variables:
      list: |
        {{ {'value': list.stdout } }}
  - stop: finished
    response_variable: list
description: "searches for recipes "
fields:
  recipe:
    selector:
      text: null
    name: recipe
    default: the kind of recipe to search for
alias: unset_recipe
sequence:
  - action: shell_command.set_recept
    metadata: {}
    data:
      recept: HA_UNSET_DINNER
    response_variable: list
  - variables:
      list: |
        {{ {'value': list.stdout } }}
  - stop: finished
    response_variable: list
description: |-
  unsets the current dinner to cook. 
  run this script when stopping dinner.
  or stopping the cooking process.
fields: {}
alias: list_recept_ingredients
sequence:
  - variables:
      list: |
        {% set recept = state_attr('sensor.recept','ingredients') %}
        {{ {'value': recept } }}
  - stop: finished
    response_variable: list
description: this script returns a list ingredients of the recipe
alias: list_recept_instructions
sequence:
  - variables:
      list: |
        {% set recept = state_attr('sensor.recept','instructions') %}
        {{ {'value': recept } }}
  - stop: finished
    response_variable: list
description: this script returns a the instructions of the recept

Linux scripts

set_recept

#!/usr/bin/php
<?php
$sensorName = "sensor.recept"; // Replace with your actual sensor name
$haUrl = "https://home.com/api/states/$sensorName"; // Replace with your HA URL
$haToken = "-osVg"; // Replace with your HA token
$localImagePath = "/var/www/html/local/recept.jpg"; for the camera

// Check for a search query
if ($argc < 2) {
    exit("Usage: php list_recipe.php <search_query>\n");
}

$query = urlencode($argv[1]);
$apiUrl = "https://www.themealdb.com/api/json/v1/1/search.php?s=$query";

// If the argument is 'HA_UNSET_DINNER', unset the sensor
if ($query === "HA_UNSET_DINNER") {
    // Prepare the payload to unset the sensor
    $payload = [
        "state" => "unset",
        "attributes" => [
            "ingredients" => [],
            "source" => null,
            "image" => null,
            "instructions" => null
        ]
    ];

    // Make the API call to update the sensor
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $haUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "Authorization: Bearer $haToken",
        "Content-Type: application/json"
    ]);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
    $result = curl_exec($ch);
    curl_close($ch);

    echo "Sensor unset successfully.\n";
    exit;
}


// Fetch data from TheMealDB API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

// Decode the JSON response
$data = json_decode($response, true);
// Check if meals are found
if (isset($data['meals']) && !empty($data['meals'])) {
    $meal = $data['meals'][0]; // Take the first result

    // Create a more readable format for ingredients
    $ingredients = [];
    for ($i = 1; $i <= 20; $i++) {
        $ingredient = $meal["strIngredient$i"] ?? "";
        $measure = $meal["strMeasure$i"] ?? "";
        if (!empty($ingredient)) {
            $ingredients[] = trim("$measure $ingredient");
        }
    }

    $formattedIngredients = implode("\n", $ingredients);


    // Save the recipe image locally
    $imageUrl = $meal["strMealThumb"];
    $imageData = file_get_contents($imageUrl);
    file_put_contents($localImagePath, $imageData);

    // Prepare the sensor update payload

    $payload = [
        "state" => $meal["strMeal"],
        "attributes" => [
            "ingredients" => $formattedIngredients,
            "source" => $meal["strSource"],
            "image" => $meal["strMealThumb"],
            "instructions" => $meal["strInstructions"] ?? "No instructions available."
        ]
    ];

    // Make the API call to update the sensor
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $haUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "Authorization: Bearer $haToken",
        "Content-Type: application/json"
    ]);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
    $result = curl_exec($ch);
    curl_close($ch);

    echo "Sensor updated with recipe: " . $meal["strMeal"] . "\n";
} else {
    echo "No results found for query: $argv[1]\n";
}
?>

list_recept

#!/usr/bin/php
<?php
$query = urlencode($argv[1]);
$apiUrl = "https://www.themealdb.com/api/json/v1/1/search.php?s=$query";

// Fetch data from TheMealDB API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

// Decode the JSON response
$data = json_decode($response, true);

// Check if meals are found
if (isset($data['meals']) && !empty($data['meals'])) {
    $meals = [];

    foreach ($data['meals'] as $meal) {
        // Create a more readable format for ingredients
        $ingredients = [];
        for ($i = 1; $i <= 20; $i++) {
            $ingredient = $meal["strIngredient$i"] ?? "";
            $measure = $meal["strMeasure$i"] ?? "";
            if (!empty($ingredient)) {
                $ingredients[] = trim("$measure $ingredient");
            }
        }

        // Add the meal details to the list
        $meals[] = [
            'name' => $meal['strMeal'] ?? 'Unknown',
            'category' => $meal['strCategory'] ?? 'Unknown',
            'cuisine' => $meal['strArea'] ?? 'Unknown',
            'instructions' => $meal['strInstructions'] ?? '',
            'ingredients' => $ingredients,
            'image' => $meal['strMealThumb'] ?? '',
            'video' => $meal['strYoutube'] ?? ''
        ];
    }

    // Output the list as JSON
    echo json_encode($meals, JSON_PRETTY_PRINT);
} else {
    // No meals found
    echo json_encode(['error' => 'No recipes found'], JSON_PRETTY_PRINT);
}

Get well. :slight_smile:

2 Likes

Yes. The LLM memory can give hallucinations.
But i use an api with json with recepts.

So it cant mix up receipts

Read the article. They are not hallucinations.

Oke bullshitting it is.

Sounds like some people I know. Many of them in politics.

But seriously, the article makes a good point. AI (or people) can’t tell falsehoods if they have no concept of truth. I’m not sure BS is the right term, either. That sort of implies they know they’re BS’ing you. I’d go with “fantasies.”

Not sure I’m ready to let AI into my kitchen just yet. Maybe some day we’ll be asking it “What should we have for dinner tonight?” It can’t come up with any worse ideas than we do now.

It uses an offical cookbook api.
So the AI isnt cooking.