To explain what the MIoT-Spec-V2 event parameter is, we take examples of some typical devices that have event instances with event parameters.
Lock
For example, xiaomi.lock.s2 siid=2 eiid=1020 is an event instance named "Lock Event". Its "arguments" field is [1,2,3,4,5,6], meaning that piid=1 "Operation Position" property, piid=2 "Operation Method" property, piid=3 "Lock Action" property, piid=4 "Abnormal Condition", piid=5 "Operation ID" property and piid=6 "Current Time" in siid=2 service is the event parameters of this event. Each parameter follows the format, unit, value-range or value-list restrictions defined in the corresponding property instance.
When the device triggers this event, it will report an event_occured message like this:
{"id":123,"method":"event_occured","params":{"did":"2834","siid":2,"eiid":1020,"arguments":[{"piid":1,"value":1},{"piid":2,"value":14},{"piid":3,"value":2},{"piid":4,"value":11},{"piid":5,"value":0},{"piid":6,"value":1764110215}]}}
If xiaomi_home log level is set to debug level in Home Assistant, the following log will be printed:
Lock Event, attributes: {'Operation Position': 1, 'Operation Method': 14, 'Lock Action': 2, 'Abnormal Condition': 11, 'Operation ID': 0, 'Current Time': 1764110215}
Meanwhile in xiaomi_home, the Event entity "Lock Lock Event" whose entity_id is event.xiaomi_cn_2834_s2_lock_event_e_2_1020 is triggered and the Logbook records the time when this event occurred.
Wireless Switch Sensor
For example, xiaomi.remote.mcn002 siid=2 eiid=1013 is an event instance named "Double Click". Its "arguments" field is [2], meaning that piid=2 "Button Type" property in siid=2 service is the only one parameter of this event. This parameter's value should be 0, 1 or 2, for the corresponding property has a value-list to indicate the button type that is "Left Button", "Right Button" or "Left and Right Button".
When the device's left button is double clicked triggering an event, it will report an event_occured message like this:
{"id":456,"method":"event_occured","params":{"did":"blt.3.p0c1","siid":2,"eiid":1013,"arguments":[{"piid":2,"value":1}]}}
If xiaomi_home log level is set to debug level in Home Assistant, the following log will be printed:
Double Press, attributes: {'Button Type': 1}
Meanwhile in xiaomi_home, the Event entity "switch-sensor-for-ble Double press" whose entity_id is event.xiaomi_cn_blt_3_p0c1_mcn002_double_click_e_2_1013 is triggered and the Logbook records the time when this event occurred.
Use event parameters as conditions in automation
For example, we want to trigger an automation when the lock xiaomi.lock.s2 is unlocked indoor, we can set the automation trigger and condition in YAML like this:
alias: lock_event
description: "unlock indoor"
triggers:
- trigger: event
event_type: state_changed
event_data:
entity_id: event.xiaomi_cn_2834_s2_lock_event_e_2_1020
conditions:
- condition: template
value_template: |
{{
trigger.event.data.new_state is not none and
trigger.event.data.new_state.attributes.get('Operation Position') == 1 and
trigger.event.data.new_state.attributes.get('Lock Action') == 2
}}
Display the event parameter in the Sensor entity
For example, xiaomi.waterpuri.s1200g reports siid=8 eiid=1 "Water Production Completed" event when the water production is completed. This event contains piid=2 "Pure Water Output" property as the event parameter. The Event entity does not display the parameters in the entity history. Here is the method to extract the event parameter and display it in a Sensor entity.
- Add the following code in configuration.yaml. input_text is used to store the event parameter which is displayed in template sensor.
input_text:
last_event_data:
name: "Last Event Data"
initial: "No event yet"
template:
- sensor:
- name: "Last Pure Water Output"
state: "{{ states('input_text.last_event_data') }}"
device_class: volume
unit_of_measurement: "mL"
state_class: total_increasing
- Create an automation of which the automation action will set the event parameter as the value of input_text when the event is triggered.
alias: event_attribute_to_input_text
description: "Set event data as input text"
triggers:
- trigger: state
entity_id:
- event.xiaomi_cn_4047_s1200g_water_production_completed_e_8_1
conditions: []
actions:
- action: input_text.set_value
metadata: {}
data:
value: "{{ trigger.to_state.attributes['Pure Water Output'] }}"
target:
entity_id: input_text.last_event_data
mode: single