Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

小爱对话记录实体使用场景及示例 #537

Closed
al-one opened this issue Apr 24, 2022 · 33 comments
Closed

小爱对话记录实体使用场景及示例 #537

al-one opened this issue Apr 24, 2022 · 33 comments
Labels
FAQ Frequently Asked Questions

Comments

@al-one
Copy link
Owner

al-one commented Apr 24, 2022

插件v0.6版本新增了获取小爱音箱对话记录的实体,该实体可以实现通过小爱直接控制HA中的设备。
以下是一个自动化示例:

alias: 小爱对话自动化
description: 关键词匹配和小爱对话内容并操作对应的设备
mode: single
trigger:
  - platform: state
    entity_id: sensor.xiaomi_x08c_xxxx_conversation # 小爱对话实体ID
condition:
  - condition: template
    value_template: |
      {{ (trigger.to_state|default(0)) and as_timestamp(now()) -
      as_timestamp(trigger.to_state.attributes.timestamp) < 60 }}
      {# 限制60秒内的对话 #}
action:
  - choose:
      # 选项1
      - conditions:
          - condition: template
            value_template: |
              {{ '打开某某开关' in trigger.to_state.state }}
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.your_switch_in_hass
      # 选项2
      - conditions:
          - condition: template
            value_template: |
              {{ '打开某某插座' in trigger.to_state.state }}
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.your_outlet_in_hass
    default: []
  - service: system_log.write
    data:
      level: info
      logger: custom_components.xiaomi_miot.xiaoai
      message: |
        {{ trigger.entity_id }}: {{ trigger.to_state.state }}

打开 HA自动化 ,添加自动化并以YAML编辑,粘贴上面的自动化yaml
小爱对话自动化


主动询问场景自动化示例:

alias: 让小爱询问我是否开灯
mode: single
trigger:
  - platform: state
    entity_id: binary_sensor.motion_sensor_entity_id # 人体传感器
    from: 'off'
    to: 'on'
condition:
  - condition: numeric_state
    entity_id: sensor.illumination_entity_id # 光照度传感器
    below: 10 # 光照度传感器低于10
action:
  - service: xiaomi_miot.intelligent_speaker
    data:
      entity_id: media_player.xiaoai_entity_id # 小爱音箱ID
      text: 欢迎回家,需要打开客厅灯吗?
      execute: false
  - delay:
      seconds: 3 # 建议等待时长改为上一步TTS语音时长
  - service: xiaomi_miot.xiaoai_wakeup
    data:
      entity_id: media_player.xiaoai_entity_id # 小爱音箱ID
  - delay:
      seconds: 2
  # 等待2秒后更新对话实体
  - service: homeassistant.update_entity
    target:
      entity_id: sensor.xiaomi_x08c_xxxx_conversation # 小爱对话实体
  # 60秒内回答小爱以确认操作
  - timeout: 60
    wait_template: |
      {% set sta = states.sensor.xiaomi_x08c_xxxx_conversation %}
      {{ (sta.state|regex_findall('[好是]的?|打开'))[0] | default(false) != false
      and as_timestamp(now()) - as_timestamp(sta.attributes.timestamp) < 60 }}
  - service: light.turn_on
    target:
      entity_id: light.your_light_entity_id

修改小爱对话实体的轮询时长:

# configuration.yaml
homeassistant:
  customize: !include customize.yaml
  customize_glob:
    # 修改所有小爱的对话实体
    "sensor.xiaomi_*_conversation":
      interval_seconds: 3 # 默认5秒

# 修改单个小爱的对话实体
# customize.yaml
sensor.xiaomi_x08c_xxxx_conversation:
  interval_seconds: 3 # 默认5秒

小爱音箱播放器使用mushroom卡片带背景:

需要先安装以下卡片:

type: custom:mushroom-media-player-card
entity: media_player.xiaoai_x08c_d135
use_media_info: true
use_media_artwork: true
volume_controls:
  - volume_buttons
  - volume_set
media_controls:
  - play_pause_stop
  - next
tap_action:
  action: more-info
card_mod:
  style: |
    {% set eid = 'media_player.xiaoai_x08c_d135' %}
    {% set img = state_attr(eid, 'entity_picture') %}
    {% if img %}
    ha-card {
      background-image: linear-gradient(rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.5)),
        url({{ img }});
      background-size: cover;
    }
    {% endif %}

xiaomi miot xiaoai mushroom card

@al-one al-one added the FAQ Frequently Asked Questions label Apr 24, 2022
@tty228
Copy link

tty228 commented Apr 24, 2022

这个功能太棒了!

@ilongjiang
Copy link

红米小爱音箱没有封面

@al-one
Copy link
Owner Author

al-one commented Apr 25, 2022

@ilongjiang 更新到master分支。

@mikeinblue
Copy link

大赞

@DenneyTang
Copy link

DenneyTang commented May 2, 2022

我只能说卧槽!这个功能简直是重磅更新!解决了两个跨越式的难题:
1、小爱同学没办法控制非米家生态的设备
2、小爱同学没有交互式对话能力显得特别傻

以上两个问题我找了很多方案不可得,没想到被你搞定了╰(´︶`)╯
大佬,你这个实现方式真的值得拿出去吹牛皮了😄

@al-one
Copy link
Owner Author

al-one commented May 3, 2022

大佬,你这个实现方式真的值得拿出去吹牛皮了😄

@DenneyTang 其实这个在Node-Red中已经有人实现过了:

@vincenthsing
Copy link

大佬,第一个开关非小米生态硬件的时候,开关确实是可以被控制,但是小爱还是会跟你说设备不存在bla bla,这个情况如何避免呢?

@al-one
Copy link
Owner Author

al-one commented May 26, 2022

小爱还是会跟你说设备不存在bla bla,这个情况如何避免呢?

@vincenthsing 可以在小爱音箱APP训练小爱或者在米家创建手动场景让小爱以指定的方式回答,也可以在巴法云创建一个虚拟设备,同步到米家。

@lin-calvin
Copy link

大佬,第一个开关非小米生态硬件的时候,开关确实是可以被控制,但是小爱还是会跟你说设备不存在bla bla,这个情况如何避免呢?

试试控制后静音?

@moxuec
Copy link

moxuec commented Jul 19, 2022

回复“不要打开”,是不是也会开灯。包含了“打开”

@al-one
Copy link
Owner Author

al-one commented Jul 20, 2022

回复“不要打开”,是不是也会开灯。包含了“打开”

@moxuec 会打开,yaml中的正则表达式只是个例子,你可以根据自己的需求去改。

@louisslee
Copy link

大佬:)可否将小爱音箱APP小爱训练计划接入MIOT AUTO,这样就可以在HA中自定义小爱回复内容了

@al-one
Copy link
Owner Author

al-one commented Dec 2, 2022

大佬:)可否将小爱音箱APP小爱训练计划接入MIOT AUTO,这样就可以在HA中自定义小爱回复内容了

不支持。

@MagicStarTrace
Copy link

关闭米家的联动自动化怎么操作? 对小爱说晚安以后,让某些场景关闭

@al-one
Copy link
Owner Author

al-one commented Dec 20, 2022

关闭米家的联动自动化怎么操作? 对小爱说晚安以后,让某些场景关闭

@MagicStarTrace 在米家APP创建批量控制(手动场景),执行关闭指定的场景/自动化,并开启用小爱来控制,设置好关键词和目标小爱后,在HA执行对应的小爱语音命令。

@keibb3tk
Copy link

大佬太厉害了,直接能识别小爱同学的语音就大有可为了。请教大佬怎么让小爱播放HA中或nas中本地的音乐呢?试了试dlna好像完全不行

@al-one
Copy link
Owner Author

al-one commented Jan 31, 2023

@keibb3tk 如果是触屏小爱,可以刷开发版系统并安装AirReceiver.apk以播放本地音乐。其他小爱除了DLNA或通过蓝牙作为播放器,暂时没有其他方法。

@MagicStarTrace
Copy link

关闭米家的联动自动化怎么操作? 对小爱说晚安以后,让某些场景关闭

@MagicStarTrace 在米家APP创建批量控制(手动场景),执行关闭指定的场景/自动化,并开启用小爱来控制,设置好关键词和目标小爱后,在HA执行对应的小爱语音命令。

和晚安还有冲突,我换了一个关键字测了下可以了 但是我想用晚安关键字 有办法吗

@keibb3tk
Copy link

keibb3tk commented Feb 5, 2023

@keibb3tk 如果是触屏小爱,可以刷开发版系统并安装AirReceiver.apk以播放本地音乐。其他小爱除了DLNA或通过蓝牙作为播放器,暂时没有其他方法。

谢谢。我的不是触屏小爱,想利用dlna播放音乐,ha中添加了dlna小爱但是播放不了。看网上说小爱的dlna只支持网易云和qq音乐,是的么?

@yzlnew
Copy link

yzlnew commented Mar 11, 2023

小爱还是会跟你说设备不存在bla bla,这个情况如何避免呢?

@vincenthsing 可以在小爱音箱APP训练小爱或者在米家创建手动场景让小爱以指定的方式回答,也可以在巴法云创建一个虚拟设备,同步到米家。

@al-one 请问巴法云的主题没有设备订阅的话会小爱会提示设备离线,怎么解决?

@al-one
Copy link
Owner Author

al-one commented Mar 13, 2023

@yzlnew 在小爱音箱APP训练小爱或者在米家创建手动场景让小爱以指定的方式回答

@ghost
Copy link

ghost commented May 8, 2023

大佬,想请问一下为什么我每次在xiaomi_miot_auto这一步都会延迟6s? 这是执行过程timeline
image

@redyuan43
Copy link

大佬,第一个开关非小米生态硬件的时候,开关确实是可以被控制,但是小爱还是会跟你说设备不存在bla bla,这个情况如何避免呢?

是的。如何让执行后不要让小爱执行。

@4277427
Copy link

4277427 commented Jul 15, 2023

小爱wakeup唤醒目前是失效的吗?

@uleatest
Copy link

uleatest commented Aug 3, 2023

请教下大佬,1代小爱音箱,通过miot auto接入到HA后,为什么找不到conversation这个sensor?其他功能比如TTS播放都没有问题。筛选小爱音箱的实体,只有switch,button,media_player, text开头的实体。

@al-one
Copy link
Owner Author

al-one commented Aug 6, 2023

请教下大佬,1代小爱音箱,通过miot auto接入到HA后,为什么找不到conversation这个sensor?其他功能比如TTS播放都没有问题。筛选小爱音箱的实体,只有switch,button,media_player, text开头的实体。

需要在小爱音箱APP中能看到对话记录才能读取到。

@dxh000130
Copy link

dxh000130 commented Nov 2, 2023

请教大佬,延迟过高怎么办,调整interval为1秒仍然有7秒延迟。跟小米音响型号有关系吗?我的是第一代小爱

@zraxx
Copy link

zraxx commented Nov 29, 2023

不管回答什么内容,客厅灯都打开

@zraxx
Copy link

zraxx commented Nov 29, 2023

插件v0.6版本新增了获取小爱音箱对话记录的实体,该实体可以实现通过小爱直接控制HA中的设备。
以下是一个自动化示例:

alias: 小爱对话自动化
description: 关键词匹配和小爱对话内容并操作对应的设备
mode: single
trigger:
  - platform: state
    entity_id: sensor.xiaomi_x08c_xxxx_conversation # 小爱对话实体ID
condition:
  - condition: template
    value_template: |
      {{ (trigger.to_state|default(0)) and as_timestamp(now()) -
      as_timestamp(trigger.to_state.attributes.timestamp) < 60 }}
      {# 限制60秒内的对话 #}
action:
  - choose:
      # 选项1
      - conditions:
          - condition: template
            value_template: |
              {{ '打开某某开关' in trigger.to_state.state }}
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.your_switch_in_hass
      # 选项2
      - conditions:
          - condition: template
            value_template: |
              {{ '打开某某插座' in trigger.to_state.state }}
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.your_outlet_in_hass
    default: []
  - service: system_log.write
    data:
      level: info
      logger: custom_components.xiaomi_miot.xiaoai
      message: |
        {{ trigger.entity_id }}: {{ trigger.to_state.state }}

打开 HA自动化 ,添加自动化并以YAML编辑,粘贴上面的自动化yaml
小爱对话自动化


主动询问场景自动化示例:

alias: 让小爱询问我是否开灯
mode: single
trigger:
  - platform: state
    entity_id: binary_sensor.motion_sensor_entity_id # 人体传感器
    from: 'off'
    to: 'on'
condition:
  - condition: numeric_state
    entity_id: sensor.illumination_entity_id # 光照度传感器
    below: 10 # 光照度传感器低于10
action:
  - service: xiaomi_miot.intelligent_speaker
    data:
      entity_id: media_player.xiaoai_entity_id # 小爱音箱ID
      text: 欢迎回家,需要打开客厅灯吗?
      execute: false
  - delay:
      seconds: 3 # 建议等待时长改为上一步TTS语音时长
  - service: xiaomi_miot.xiaoai_wakeup
    data:
      entity_id: media_player.xiaoai_entity_id # 小爱音箱ID
  - delay:
      seconds: 2
  # 等待2秒后更新对话实体
  - service: homeassistant.update_entity
    target:
      entity_id: sensor.xiaomi_x08c_xxxx_conversation # 小爱对话实体
  # 60秒内回答小爱以确认操作
  - timeout: 60
    wait_template: |
      {% set sta = states.sensor.xiaomi_x08c_xxxx_conversation %}
      {{ (sta.state|regex_findall('[好是]的?|打开'))[0] | default(false) != false
      and as_timestamp(now()) - as_timestamp(sta.attributes.timestamp) < 60 }}
  - service: light.turn_on
    target:
      entity_id: light.your_light_entity_id

修改小爱对话实体的轮询时长:

# configuration.yaml
homeassistant:
  customize: !include customize.yaml
  customize_glob:
    # 修改所有小爱的对话实体
    "sensor.xiaomi_*_conversation":
      interval_seconds: 3 # 默认5秒

# 修改单个小爱的对话实体
# customize.yaml
sensor.xiaomi_x08c_xxxx_conversation:
  interval_seconds: 3 # 默认5秒

小爱音箱播放器使用mushroom卡片带背景:

需要先安装以下卡片:

type: custom:mushroom-media-player-card
entity: media_player.xiaoai_x08c_d135
use_media_info: true
use_media_artwork: true
volume_controls:
  - volume_buttons
  - volume_set
media_controls:
  - play_pause_stop
  - next
tap_action:
  action: more-info
card_mod:
  style: |
    {% set eid = 'media_player.xiaoai_x08c_d135' %}
    {% set img = state_attr(eid, 'entity_picture') %}
    {% if img %}
    ha-card {
      background-image: linear-gradient(rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.5)),
        url({{ img }});
      background-size: cover;
    }
    {% endif %}
xiaomi miot xiaoai mushroom card

不回答,客厅灯也会打开。
Screenshot_2023-11-29-21-34-25-038_io.homeassistant.companion.android.png

@emzbsoft
Copy link

不回答也打开,我把上面的拆分了
更新对话实体后面的不要,新建个自动化匹配“打开”就能用了,但是只能控制少量设备,因为只要字符含“打开”都执行,
可以要看客厅灯,只用'客厅' in trigger.to_state.state

@lazybabyz
Copy link

@keibb3tk 如果是触屏小爱,可以刷开发版系统并安装AirReceiver.apk以播放本地音乐。其他小爱除了DLNA或通过蓝牙作为播放器,暂时没有其他方法。
现在可以不刷开发版系统播放本地音乐

https://github.com/hanxi/xiaomusic

@al-one
Copy link
Owner Author

al-one commented Oct 28, 2024

目前已支持在小爱音箱播放流媒体音频及TTS:
media_player.play_media

service: media_player.play_media
data:
  entity_id: media_player.xiaoai_x08c_d135
  media_content_id: https://cdn1.suno.ai/8830d063-71a4-4f02-8855-6aa3008899a7.mp3
  media_content_type: music

@parkghost
Copy link
Contributor

小爱音箱 万能遥控版 用 media_player.play_media 會循環播放,用 tts.speak 也是循環播放,能控制只播放一次嗎?

Repository owner locked and limited conversation to collaborators Oct 29, 2024
@al-one al-one converted this issue into discussion #1906 Oct 29, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
FAQ Frequently Asked Questions
Projects
None yet
Development

No branches or pull requests