Fallback to another Ollama Based Voice Assistant

I have two Ollama based Voice Assistants. One Gaming PC and another server with a smaller GPU.

I couldn’t get the pipeline to fall back to the secondary Ollama VA when it was offline. Satellites just went dead. I see a few other people had this issue too. I thought about using a load balancer / proxy at the network/http layer and maybe I’ll get there.

But for now I have an automation which periodically does a health check of the Preferred Ollama server, and if unavailable, sets the ESP Satellite index to select the other VA. It reverses this when the preferred one returns.

Now, when I shutdown my PC the kids can still “Hey Mycroft” the TV on in the morning :slight_smile:

Though it might be useful. Feel free to offer suggestions. It looks a little complicated for multiple Satellites and edge cases. But it’s straightforward in architecture and implementation.

It assumes you have ESP Home Satellites. I haven’t tried on Wyoming. Only other requirement is a shell script in your configuration.yaml to curl the Primary Ollama Server status.

Need to fix:

  • Doesn’t work for Companion App, where user appears to have to select VA manually.
  • I can’t for the life of me figure out how to rename my states(‘select.my_assistant_assistant_index’) id - the first name I registered my Satellite with (it’s now, “Pi4” in every other place I can think of including .storage but it remains cached).

Shell command

shell_command:
  # Poll Ollama preferred server for health / availability
  ollama_preferred_hc: "bash -c 'curl -s --max-time 5 http://192.168.0.XXX:11434/ || echo Server is down; exit 0'"

Automation

alias: Ollama Health Check
description: >
  Periodic health check for Ollama server and device selection based on
  response.
triggers:
  - minutes: /5
    trigger: time_pattern
conditions: []
actions:
  - data: {}
    response_variable: ollama_preferred_hc
    action: shell_command.ollama_preferred_hc
    alias: Poll Preferred Ollama Server WIth curl
  - data_template:
      name: Debug Ollama Health Check
      message: >
        Ollama: {{ ollama_preferred_hc.stdout }}; Select1: {{
        states('select.pi3_assistant_index') }}; Select2: {{
        states('select.my_assistant_assistant_index') }}
    action: logbook.log
    alias: Log Ollama and Satellite States
  - alias: Fallback to backup Ollama VA if Needed
    choose:
      - conditions:
          - alias: Preferred Ollam is Down
            condition: template
            value_template: |
              {{ ollama_preferred_hc.stdout | trim == "Server is down" }}
        sequence:
          - device_id: 5446524fd34986e8a2abbb953d210515
            domain: select
            entity_id: 7c81903a1fb7c6e5b1b018e3971c03f5
            type: select_option
            option: Ollama 3070ti
            alias: Set Pi4 ESP Satellite VA to backup Ollama 3070ti
          - device_id: 59caf315ff7e210bbf03fd5aba73051b
            domain: select
            entity_id: 6ee96beaf3f91624d88c67553ffc538f
            type: select_option
            option: Ollama 3070ti
            alias: Set Pi3 ESP Satellite VA to backup Ollama 3070ti
        alias: Preferred Ollama is down - Fallback to backup
      - conditions:
          - condition: and
            conditions:
              - condition: template
                value_template: |
                  {{ ollama_preferred_hc.stdout | trim == "Ollama is running" }}
                alias: Preferred Ollama is running
              - alias: >-
                  Any Satellite State != "preferred" (Ignoring unavailable and
                  unknown)
                condition: template
                value_template: >
                  {% set s1 = states('select.pi3_assistant_index') %}
                  {% set s2 = states('select.my_assistant_assistant_index') %}
                  {{ ((s1 != 'preferred') and (s1 != 'unknown') and (s1 !=
                  'unavailable')) or
                     ((s2 != 'preferred') and (s2 != 'unknown') and (s2 != 'unavailable')) }}
        sequence:
          - device_id: 59caf315ff7e210bbf03fd5aba73051b
            domain: select
            entity_id: 6ee96beaf3f91624d88c67553ffc538f
            type: select_option
            option: preferred
            alias: Set Pi3 ESP Satellite VA to Preferred
          - device_id: 5446524fd34986e8a2abbb953d210515
            domain: select
            entity_id: 7c81903a1fb7c6e5b1b018e3971c03f5
            type: select_option
            option: preferred
            alias: Set Pi4 ESP Satellite VA to Preferred
        alias: >-
          Preferred Ollama is Up & Satellites are Still Pointing at Backup -
          Restore
mode: single

Picture if the yaml is a bit rough on the eyes…