Please help me Trigger on event state_change

hi I would like to setup a automation, track BLE device from close to far! not sure how to write trigger event.
below is Listen event

I try to track device “sensor.holy_iot_room_presence” “attributes distance” Value change make sure device leave or come.
anyone please help me how to write the trigger.

Hi there, can you share the state and attributes of the entity sensor.holy_iot_room_presence. You can get it from the developer console. If you have any trouble getting that, do ask

Also how does the attribute distance change with BLE device going far or staying near?

Not sure why you want to go through events.
Just use a state trigger in your automation

1 Like

thanks for your reply. when i move sensor.holy_iot_room_presence, the value of distance will change.
Near is smaller number, far is big number. Please compare the change in distance. “6.2 & 6.7”
I want to use distance value to trigger automation! For example: when old_state is 6.2, new_state is 6.7, Means the device is leaving, then action.

event_type": “state_changed”,
“data”: {
“entity_id”: “sensor.holy_iot_room_presence”,
“old_state”: {
“entity_id”: “sensor.holy_iot_room_presence”,
“state”: “Garage”,
“attributes”: {
"distance": 6.2,
“last_updated_at”: “2021-11-08T04:45:31.428Z”,
“friendly_name”: “Holy-IOT Room Presence”,
“icon”: “mdi:bluetooth”
},
“last_changed”: “2021-11-08T04:42:44.705959+00:00”,
“last_updated”: “2021-11-08T04:45:31.534302+00:00”,
“context”: {
“id”: “5ad28592235422e32775aea9bd8cab30”,
“parent_id”: null,
“user_id”: null
}
},
“new_state”: {
“entity_id”: “sensor.holy_iot_room_presence”,
“state”: “Garage”,
“attributes”: {
"distance": 6.7,
“last_updated_at”: “2021-11-08T04:45:33.444Z”,
“friendly_name”: “Holy-IOT Room Presence”,
“icon”: “mdi:bluetooth”

You can use the Trend - Home Assistant platform to check if the device is coming or going like below.

binary_sensor:
  - platform: trend
    sensors:
      blr_close:
        entity_id: sensor.holy_iot_room_presence
        attribute: distance
        sample_duration: 60
        device_class: cold
      blr_far:
        entity_id: sensor.holy_iot_room_presence
        attribute: distance
        sample_duration: 60
        device_class: heat

Here I have used the device class cold and heat because distance increasing and decreasing is similar to temperatures. With this particular setup, if the sensor distance value is increasing, the blr_far sensor will be on and if the device is moving closer, the blr_close sensor will be on. Also if there is no change in distance for 1 minutes, both sensors will be off meaning the object is stable.

Now you should be able to use this sensor for your automation triggering. Also you may have to reduce the sample duration.

If you need anymore help with automation do please ask.

thank you for your patience! In fact, the trigger I need is from one value to another! For example, only from 6 to 8 and then trigger! I tried the knowledge on the Internet, and the automation I wrote by myself, but it didn’t work. If possible, please help me to check blow.

alias: new
description: ‘’
trigger:

  • platform: event
    event_type: state_changed
    event_data:
    entity_id: sensor.fsc_bp104_room_presence
    attributes: distance
    old_state:
    state: ‘1’
    new_state:
    state: ‘1.7’
    condition: []
    action:
  • type: turn_off
    device_id: 22bc57c19a420ade5d81e5d698ad6f12
    entity_id: switch.tuya_garage_door
    domain: switch
  • type: turn_off
    device_id: eaf2d2f99cee274ded0fe7f0ff052503
    entity_id: switch.zigbee_da_men_switch_1
    domain: switch
    mode: single

I could be wrong (I don’t use Device triggers very often), but I don’t think this will trigger because you have set it up so that the value has to go from 1 to 1.7 without passing through any other state.

not it isn’t working, not sure how to fix it.

Hi,try changing the trigger type to state. The trigger should be like below.

platform: state
entity_id: sensor.fsc_bp104_room_presence
attribute: distance
from: '6'
to: '8'

If you need any more help please paste the automation code inside ``` so that we can edit it in correct format

Don’t try to use the specific distance value as your trigger. Instead, use the change in distance as the trigger, then use a template to compare the “to” and “from” distances.

Try this:

alias: Presence/Relative Motion controls switched
description: ‘Turn switches off or on if distance increases or decreases’
trigger:
  - platform: state
    entity_id: sensor.fsc_bp104_room_presence
    attribute: distance
condition: []
action:
  - choose:
      - alias: "Turn off switches if leaving (distance increases)"
        conditions:
          - condition: template
            value_template: >-
              {{ trigger.to_state.attributes.distance|float(0) > trigger.from_state.attributes.distance|float(0)}}
          - condition: state
            entity_id: switch.tuya_garage_door
            state: 'on'
            for:
              seconds: 30
          - condition: state
            entity_id: switch.zigbee_da_men_switch_1
            state: 'on'
            for:
              seconds: 30
        sequence:
          - type: turn_off
            device_id: 22bc57c19a420ade5d81e5d698ad6f12
            entity_id: switch.tuya_garage_door
            domain: switch
          - type: turn_off
            device_id: eaf2d2f99cee274ded0fe7f0ff052503
            entity_id: switch.zigbee_da_men_switch_1
            domain: switch
      - alias: "Turn on switches if arriving (distance decreases)"
        conditions:
          - condition: template
            value_template: >-
              {{ trigger.to_state.attributes.distance|float(0) < trigger.from_state.attributes.distance|float(0)}}
          - condition: state
            entity_id: switch.tuya_garage_door
            state: 'off'
            for:
              seconds: 30
          - condition: state
            entity_id: switch.zigbee_da_men_switch_1
            state: 'off'
            for:
              seconds: 30
        sequence:
          - type: turn_on
            device_id: 22bc57c19a420ade5d81e5d698ad6f12
            entity_id: switch.tuya_garage_door
            domain: switch
          - type: turn_on
            device_id: eaf2d2f99cee274ded0fe7f0ff052503
            entity_id: switch.zigbee_da_men_switch_1
            domain: switch
    default: []
  - delay: 15
mode: single

It is probably going to be necessary to modify the automation to get it just right, but see if it works.

I don’t understand exactly what you are trying to say here:

Do you mean you are only interested in changes in distance when they occur between 6 and 8?

@sheminasalam That will not work because you have set it up so that the value has to go from 6 to 8 without passing through any other state.

yes. only 6 to 8!

not 6 to 10 or 8 to 6!

Ok. Since you are only interested if the state change is between 6 and 8 in the “leaving” direction, I have modified the automation from my previous post.

alias: Presence/Relative Motion in a specific range controls switch
description: ‘Turn switches off if distance increases’
trigger:
  - platform: state
    entity_id: sensor.fsc_bp104_room_presence
    attribute: distance
condition:
  - condition: template
    value_template: >-
      {{ 8 >= trigger.to_state.attributes.distance|float(0) >= 6 }}
  - condition: template
    value_template: >-
      {{ trigger.to_state.attributes.distance|float(0) >
      trigger.from_state.attributes.distance|float(0)}}
  - condition: state
    entity_id: switch.tuya_garage_door
    state: 'on'
    for:
      seconds: 15
  - condition: state
    entity_id: switch.zigbee_da_men_switch_1
    state: 'on'
    for:
      seconds: 15
action:
  - type: turn_off
    device_id: 22bc57c19a420ade5d81e5d698ad6f12
    entity_id: switch.tuya_garage_door
    domain: switch
  - type: turn_off
    device_id: eaf2d2f99cee274ded0fe7f0ff052503
    entity_id: switch.zigbee_da_men_switch_1
    domain: switch
mode: single

thanks, for your help. one more question, is decimals available?

Very unfortunate, I tried it for a whole day, but it didn’t work!

If you want anyone to help you further, you will need to provide information. We don’t know what you mean when you say “…it didn’t work”.

Please post the output of the automation trace and the output of the Developer Tools>States tool so that we have something to work from.

Finally, you may need to rethink your project. Single antenna BLE distance accuracy is increasingly unreliable beyond 2 meters