First automation to turn on lights when motion is detected

Hello everyone. I am learning and hoping to get some help with an automation. I have installed a shelly dimmer switch and a shelly motion 2 sensor. I have them in HA and when I use the visual editor to build an automation, it isn’t working. The automation is to turn on the light when there is motion.

alias: Turn on kitchen light with motion
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 48a9cf1c8428cbf5e01f674b879e967d
    entity_id: binary_sensor.shellymotion2_8cf681be15ae_motion
    domain: binary_sensor
condition:
  - type: is_illuminance
    condition: device
    device_id: 48a9cf1c8428cbf5e01f674b879e967d
    entity_id: sensor.shellymotion2_8cf681be15ae_luminosity
    domain: sensor
    below: 1000
    enabled: true
action:
  - service: light.turn_on
    data:
      brightness_pct: 50
    target:
      device_id: be917bcc64ede08c87e4c3456d8e32d8
mode: single

Getting the shelly 2 motion sensor into HA was a little tricky and I followed a video here

It looks like HA can tell when motion happen as my card show motion. The light will turn on if I run the automation, but then motion happens, the lights do not come on.

Any help is apperciated.

Steve

Try using the state of the motion detector entity as the trigger. Something like…

platform: state
entity_id:
  - binary_sensor.shellymotion2_8cf681be15ae_motion
to: "on"

Similarly for the condition…

condition: numeric_state
entity_id: sensor.shellymotion2_8cf681be15ae_luminosity
below: 1000

I tried several approaches but have settled on the code below. I use it for five Shelly Motion 2 and two Everything Presence One detectors. It seems to work fine. For the Shelly’s I have set the Motion Blind Time to 2 Minutes. At 5 Minutes there were times when it would not report motion.

alias: Foyer Lights Motion Control
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.foyer_sensor_a_motion
    id: Foyer_A_Occupancy_Detected
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.foyer_sensor_a_motion
    to: "off"
    for:
      hours: 0
      minutes: 5
      seconds: 0
condition: []
action:
  - if:
      - condition: trigger
        id: Foyer_A_Occupancy_Detected
    then:
      - service: light.turn_on
        data: {}
        target:
          entity_id:
            - light.small_lamp
            - light.foyer_lamp
    else:
      - service: light.turn_off
        data: {}
        target:
          entity_id:
            - light.small_lamp
            - light.foyer_lamp
mode: single

Thank you for the replies. I really appreciate this help. I wanted to add a little to the post as this morning when I came into the room, the lights turned on as if the system worked as I expected, however when I manually turned off the light at the wall dimmer, when I got a led light from the shelly motion sensor indicating that it had detected motion, the lights did not turn on. On a bright note (no pun intended) when the lights turned on, the dimmer was set to the 50% where the automation had be set to (reported as 128 value out of 255). I will look through the recommendations you have listed and work on this. Thank you again

Is there a way to use the graphical interface to set an automation to use the state entity rather than the device? I think I got it. Does this look correct?

alias: Turn on kitchen light with motion
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.shellymotion2_8cf681be15ae_motion
    from: "on"
condition:
  - type: is_illuminance
    condition: device
    device_id: 48a9cf1c8428cbf5e01f674b879e967d
    entity_id: sensor.shellymotion2_8cf681be15ae_luminosity
    domain: sensor
    below: 1000
    enabled: true
action:
  - service: light.turn_on
    data:
      brightness_pct: 50
    target:
      device_id: be917bcc64ede08c87e4c3456d8e32d8
mode: single

I think that this part might not work

trigger:
  - platform: state
    entity_id:
      - binary_sensor.shellymotion2_8cf681be15ae_motion
    from: "on"

The from “on” I think may cause this to now work as expected.

Thoughts? TIA

Hi, have you checked the traces from the automation? (right hand top when you’re in the automation editor)

Thank you for asking. I found this, but since I am new, have not dived into it. I think with time I can figure out what it is saying. Here is what I found

It looks like it triggered twice (which is weird given that the led on the motion sensor was going off many more times than the time points here so I would expect more triggers, but obviously I don’t quite know how it is working)

this:
  entity_id: automation.turn_on_kitchen_light_with_motion
  state: 'on'
  attributes:
    last_triggered: '2023-06-24T05:50:39.970421+00:00'
    mode: single
    current: 0
    id: '1687577018762'
    friendly_name: Turn on kitchen light with motion
  last_changed: '2023-06-24T03:33:03.847410+00:00'
  last_updated: '2023-06-24T05:50:40.006617+00:00'
  context:
    id: 01H3NZ8PV1YKG1QFGH0KR206N1
    parent_id: 01H3NZ8PTYE570D3K0HJW95E92
    user_id: null
trigger:
  id: '0'
  idx: '0'
  alias: null
  platform: device
  entity_id: binary_sensor.shellymotion2_8cf681be15ae_motion
  from_state:
    entity_id: binary_sensor.shellymotion2_8cf681be15ae_motion
    state: 'off'
    attributes:
      device_class: motion
      friendly_name: shellymotion2-8CF681BE15AE motion
    last_changed: '2023-06-24T05:52:43.429872+00:00'
    last_updated: '2023-06-24T05:52:43.429872+00:00'
    context:
      id: 01H3NZCFD5JN2ZDNA70PRWJAKQ
      parent_id: null
      user_id: null
  to_state:
    entity_id: binary_sensor.shellymotion2_8cf681be15ae_motion
    state: 'on'
    attributes:
      device_class: motion
      friendly_name: shellymotion2-8CF681BE15AE motion
    last_changed: '2023-06-24T11:31:49.327074+00:00'
    last_updated: '2023-06-24T11:31:49.327074+00:00'
    context:
      id: 01H3PJSCEFBTRPW3QGC878BT0Y
      parent_id: null
      user_id: null
  for: null
  attribute: null
  description: state of binary_sensor.shellymotion2_8cf681be15ae_motion

This is from when it turned on the light this morning. I will be able to dive more into this later this evening.

Take the time to get familiar with it because this will come in handy as you go with HA.

No, it’s not because of being triggered twice, this is status info about the automation itself.
The Step Details and Step Config will help you the most.