Need help with motion sensor automation please

Hi all.
I bought this motion sensor: IHSENO Tuya ZigBee PIR Motion Sensor
image

I’m trying a simple “switch on light when sensing motion, and switch off light if not sensing motion for 10 seconds”

That is just an example to test if it even works.
Questions:

  1. What I assume is that the polling rate of the sensor is very low (probably to conserve battery) - is there a way to change that in HA?
  2. The automation I created using UI only works for switching on the Yeelight, but not switching off if there’s a trigger for no motion for X amount of seconds. Can someone please assist with that?

This is what I created so far:

alias: Gil's office light automation
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 39c3e992e4e87f871b7a50a371023522
    entity_id: d310b2e5d926c696637b0d7f2ce5cf50
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 1
condition: []
action:
  - if:
      - type: is_motion
        condition: device
        device_id: 39c3e992e4e87f871b7a50a371023522
        entity_id: d310b2e5d926c696637b0d7f2ce5cf50
        domain: binary_sensor
    then:
      - type: turn_on
        device_id: f87d15b2b40ca31515829daab15c980d
        entity_id: 1e891c8cba0250124c87a1b99ad7cd43
        domain: light
    else:
      - if:
          - type: is_no_motion
            condition: device
            device_id: 39c3e992e4e87f871b7a50a371023522
            entity_id: d310b2e5d926c696637b0d7f2ce5cf50
            domain: binary_sensor
            for:
              hours: 0
              minutes: 0
              seconds: 10
        then:
          - type: turn_off
            device_id: f87d15b2b40ca31515829daab15c980d
            entity_id: 1e891c8cba0250124c87a1b99ad7cd43
            domain: light
mode: single

It’s not normally recommended to use device triggers, conditions or actions (for reasons beyond the scope of this topic) but I think you can do what you want by adding a second trigger for no motion for 10 seconds.

Remove the “for” in the else action as it won’t be needed because the timing is done in the trigger.

1 Like

Hi,

So this one is not recommended 2? Can you explane please.

alias: PIR Garage AAN
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 21be8f42c0800f42967f7077669e95b1
    entity_id: binary_sensor.pir_garage
    domain: binary_sensor
condition:
  - condition: state
    entity_id: input_boolean.rookalarm
    state: "off"
  - type: is_off
    condition: device
    device_id: b577924bba8603e49e66f05aa9126a3b
    entity_id: binary_sensor.lamp_garage
    domain: binary_sensor
action:
  - type: turn_on
    device_id: b577924bba8603e49e66f05aa9126a3b
    entity_id: switch.lamp_garage
    domain: switch
mode: single

The downside to device “things” is that if (and when) a device dies and needs replaced then there is no way to replace the device and end up with the same device_id.

So that means that you need to manually edit all of the places where you use that device_id in your system to update everything to the new device_id.

if you use services (which use entity_ids instead of device_ids) then if the device needs replaced you simply edit the device to make the entity_ids the same as the old device and everything else all just works.

Also there are some other perks with using services instead of devices like the ability to use templates in many areas. Device_ids don’t allow templates in any of the various selections.

2 Likes

ok thanks for the advice.
I came up with this based on your recommendations:

alias: Gil's office light automation
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 39c3e992e4e87f871b7a50a371023522
    entity_id: d310b2e5d926c696637b0d7f2ce5cf50
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 0
  - type: no_motion
    platform: device
    device_id: 39c3e992e4e87f871b7a50a371023522
    entity_id: d310b2e5d926c696637b0d7f2ce5cf50
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 5
condition: []
action:
  - if:
      - type: is_motion
        condition: device
        device_id: 39c3e992e4e87f871b7a50a371023522
        entity_id: d310b2e5d926c696637b0d7f2ce5cf50
        domain: binary_sensor
    then:
      - type: turn_on
        device_id: f87d15b2b40ca31515829daab15c980d
        entity_id: 1e891c8cba0250124c87a1b99ad7cd43
        domain: light
      - type: turn_on
        device_id: e602110525aad5cfee5939e748e56bb9
        entity_id: 84e831a757c090336d838004d341674f
        domain: light
    else:
      - if:
          - type: is_no_motion
            condition: device
            device_id: 39c3e992e4e87f871b7a50a371023522
            entity_id: d310b2e5d926c696637b0d7f2ce5cf50
            domain: binary_sensor
        then:
          - type: turn_off
            device_id: f87d15b2b40ca31515829daab15c980d
            entity_id: 1e891c8cba0250124c87a1b99ad7cd43
            domain: light
          - type: turn_off
            device_id: e602110525aad5cfee5939e748e56bb9
            entity_id: 84e831a757c090336d838004d341674f
            domain: light
mode: single
  1. How can I find and tweak the polling interval of the motion sensor?
  2. How do I change from devices to services, based on your suggestion?
  3. Based on the current YAML automation, is it written in an optimized way?

Thanks again for the help!

Here is what I use to turn on my hallway light when motion is detected:

alias: Hallway Light - Motion On
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.motionsensor1_occupancy
    from: "off"
    to: "on"
condition: []
action:
  - service: light.turn_on
    data:
      brightness_pct: 80
    target:
      entity_id:
        - light.hallway_light

Hi, Below is my code:

- id: '1672480509565'
  alias: General - Light
  description: ''
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.occupancy
    to: 'on'
    id: occupancy_on
  - platform: state
    entity_id:
    - binary_sensor.occupancy
    to: 'off'
    id: occupancy_off
    for:
      hours: 0
      minutes: 5
      seconds: 0
condition: []
  action:
  - choose:
    - conditions:
      - condition: trigger
        id: occupancy_on
      sequence:
      - if:
        - condition: numeric_state
          entity_id: sensor.ruangtamu_lux_illuminance_lux
          below: 2222
        then:
        - service: light.turn_on
          data: {}
          target:
            entity_id: light.dapur
    - conditions:
      - condition: trigger
        id: occupancy_off
      sequence:
      - service: light.turn_off
        data: {}
        target:
          entity_id: light.dapur

1st. Automation
If motion is detected (on), condition lux value is below ‘2222’, light will turn on

2nd. Automation
If motion is cleared (off) => 5 minutes, light will turn off

which motion sensor with lux are you using?

well what are you using? What is the model, as shown by HA in Devices.

I’m using this: TuYa IH012-RT01 control via MQTT | Zigbee2MQTT

It actually an individual sensor:

Motion - SONOFF SNZB-03
Lux - Aqara Light Sensor T1