al-one / hass-xiaomi-miot

Automatic integrate all Xiaomi devices to HomeAssistant via miot-spec, support Wi-Fi, BLE, ZigBee devices. 小米米家智能家居设备接入Hass集成
https://miot-spec.com
Apache License 2.0
4.46k stars 627 forks source link

Access Miot_action_result #1172

Open Drealine opened 1 year ago

Drealine commented 1 year ago

Hi, How can I get the value from miot_action_result in automation after call service and throw result ? I can use the attribute entity of the parent but if some automations are called at the same time, it give me some false results when read the attribute. Tk a lot!

al-one commented 1 year ago

You must know that the HA service has no return value (maybe the official is trying to support it). So this component uses events to publish the result of the service:

description: Example of obtaining miot action result
mode: queued
max: 10
trigger:
  - platform: state
    entity_id:
      - input_button.action
    id: miot_action
    alias: When button been pressed
  - platform: event
    event_type: xiaomi_miot.call_miot_action
    event_data:
      # filter this event
      entity_id: sensor.your_miot_entity_id
      siid: 2
      aiid: 1
    id: miot_event
    alias: When the action event is fired
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: miot_action
        sequence:
          - service: xiaomi_miot.call_action
            data:
              entity_id: sensor.your_miot_entity_id
              siid: 2
              aiid: 1
              params: []
              throw: false
      - conditions:
          - condition: trigger
            id: miot_event
        sequence:
          - service: system_log.write
            data:
              level: warning
              message: "{{ trigger.event.data }}"
Drealine commented 1 year ago

Tk @al-one! So if I understand correctly, this automation use the id set by user in the automation. So if I've another automations, it's possible that the automation get a false result of miot_action_result ? For example, I use three times automation to set program of my petfeeder which has the same siid and aiid.

No possible to filter event with params set in call_service ?

al-one commented 1 year ago

The action params are not currently fired to the event data, maybe you can use did, it can be customized to any string.

https://github.com/al-one/hass-xiaomi-miot/blob/1caa990014dc07133dd1b6160eb671fabf87a587/custom_components/xiaomi_miot/__init__.py#L1931-L1937

description: Example of obtaining miot action result
mode: queued
max: 10
trigger:
  - platform: state
    entity_id:
      - input_button.action
    id: miot_action
    alias: When button been pressed
  - platform: event
    event_type: xiaomi_miot.call_miot_action
    event_data:
      # filter this event
      did: my-action-1
    id: miot_event
    alias: When the action event is fired
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: miot_action
        sequence:
          - service: xiaomi_miot.call_action
            data:
              entity_id: sensor.your_miot_entity_id
              did: my-action-1
              siid: 2
              aiid: 1
              params: []
              throw: false
      - conditions:
          - condition: trigger
            id: miot_event
        sequence:
          - service: system_log.write
            data:
              level: warning
              message: "{{ trigger.event.data }}"
Drealine commented 1 year ago

Tk. Using did not be good because I want to have all results from same siid and aiid with the same device. It's possible to implement fire event with params on the code ?

Drealine commented 1 year ago

The idea is for example:

Fire event from an automation which get siid, aiid and params (ex : set 10 grams at 6'oclock) to have the result if the command is ok or not and turn on/off a virtual boolean. So add fire event from params coupled to siid and aiid should be great :)

al-one commented 1 year ago

params has been added to the event data.

Drealine commented 1 year ago

Tk a lot! I appreciate :)

Drealine commented 1 year ago

When event fired, params return like this :

params: []

No list return so can't filtering event.

al-one commented 1 year ago

xiaomi miot call action event

Drealine commented 1 year ago

Hi Using this : image

Didn't work : image