globals: # Global variable tracking the phase of the voice assistant (defined above). Initialized to not_ready - id: voice_assistant_phase type: int restore_value: no initial_value: "2" - id: stt_vad_started type: bool restore_value: no initial_value: "false" i2s_audio: id: i2s_audio_bus i2s_lrclk_pin: GPIO${WS} #WS i2s_bclk_pin: GPIO${SCK} #sck microphone: - platform: i2s_audio id: echo_microphone i2s_din_pin: GPIO${SD} #SD adc_type: external i2s_audio_id: i2s_audio_bus #pdm: False channel: right sample_rate: 16000 bits_per_sample: 16bit #i2s_mode: primary correct_dc_offset: true voice_assistant: id: va microphone: microphone: echo_microphone channels: 0 gain_factor: 4 noise_suppression_level: 2 auto_gain: 31dBFS on_start: - light.turn_on: id: led red: 0% green: 100% blue: 0% brightness: 15% effect: none on_listening: - light.turn_on: id: led blue: 100% red: 0% green: 0% effect: none - lambda: id(voice_assistant_phase) = 2; - lambda: id(stt_vad_started) = false; - script.execute: listening_timeout on_stt_vad_start: # Turn off the script if speech is detected - lambda: id(stt_vad_started) = true; on_stt_vad_end: - lambda: id(voice_assistant_phase) = 3; # recording finished, HA is processing #- logger.log: "===============================VAD is the END ==================================" - script.stop: listening_timeout - light.turn_on: id: led blue: 100% red: 0% green: 100% effect: none on_stt_end: - lambda: id(voice_assistant_phase) = 3; - script.stop: listening_timeout - logger.log: format: "STT text: %s" args: ["x.c_str()"] on_tts_start: then: - light.turn_on: id: led blue: 100% red: 100% green: 100% brightness: 30% effect: none on_tts_end: - homeassistant.action: action: media_player.play_media data: entity_id: ${SPKR} media_content_id: !lambda "return x;" media_content_type: music announce: "false" on_end: # No local speaker/media_player, so ESPHome never reports playback as finished # and HA keeps the satellite in "responding". Report it ourselves. - lambda: |- auto *conn = id(va).get_api_connection(); if (conn != nullptr) { api::VoiceAssistantAnnounceFinished msg; msg.success = true; if (!conn->send_message(msg)) { ESP_LOGW("va", "Could not send announce-finished to HA"); } } - lambda: id(voice_assistant_phase) = 1; - lambda: id(stt_vad_started) = false; - script.execute: start_wake_word #- voice_assistant.start: on_error: - if: condition: lambda: |- return message.find("duplicate_wake_up_detected") != std::string::npos; then: - voice_assistant.stop: - delay: 350ms - script.execute: start_wake_word else: - lambda: id(voice_assistant_phase) = 4; - light.turn_on: id: led red: 100% green: 0% blue: 0% brightness: 100% #effect: "Slow Pulse" - delay: 2s - script.execute: rboot #- script.execute: reset_led on_client_connected: - lambda: id(voice_assistant_phase) = 1; # connected again: clear "disconnected" state - script.stop: rboot # cancel the pending reboot - delay: 1s # Give the api server time to settle - script.execute: start_wake_word on_client_disconnected: - lambda: id(voice_assistant_phase) = 4; - script.execute: stop_wake_word - script.execute: rboot interval: - interval: 30s then: - if: condition: and: - api.connected: - not: - voice_assistant.is_running: - switch.is_off: stop_listen then: - script.execute: start_wake_word script: - id: rboot mode: restart then: - delay: 180s - if: condition: and: - lambda: 'return id(voice_assistant_phase) == 4;' - not: - api.connected: # only reboot if HA is still not connected then: - switch.turn_on: id: rbt - id: listening_timeout mode: restart then: - delay: ${listenTimeout} # - logger.log: # format: "In Script >>>> C >>>> voice_assistant_phase=%d " # args: [ 'id(voice_assistant_phase)' ] - if: condition: lambda: |- return id(voice_assistant_phase) == 2; then: - if: condition: lambda: 'return id(stt_vad_started);' then: # Speech is still going after the max time: stop the mic. # HA processes the audio received so far; on_end restarts the wake word. - logger.log: "Max listen time reached, stopping audio stream" - lambda: id(voice_assistant_phase) = 3; - voice_assistant.stop: else: # No speech at all: abort and return to wake word listening - logger.log: "No speech detected, back to wake word" - light.turn_off: id: led - voice_assistant.stop: - delay: 1s - script.execute: start_wake_word - id: start_wake_word then: - if: condition: #and: - not: - voice_assistant.is_running: then: - lambda: id(va).set_use_wake_word(true); - voice_assistant.start_continuous: - id: stop_wake_word then: - lambda: id(va).set_use_wake_word(false); - voice_assistant.stop: switch: - platform: template name: Stop Listen id: stop_listen optimistic: true restore_mode: RESTORE_DEFAULT_OFF #entity_category: config on_turn_on: # Stop streaming to HA completely instead of sending silence - script.execute: stop_wake_word - light.turn_off: led on_turn_off: - script.execute: start_wake_word start_wake_word script: add the switch.is_off condition. - id: start_wake_word then: - if: condition: - not: - voice_assistant.is_running: - switch.is_off: stop_listen then: - lambda: id(va).set_use_wake_word(true); - voice_assistant.start_continuous: