Just turn_off light with a xiaomi binary_sensor

Hi everyone :slight_smile:

I have a big problem and I cannot find the solution in topics it’s crary :frowning:
My code is correct but don’t work correctly :slight_smile:

- alias: 'light on'
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: binary_sensor.door_window_sensor_158d0002431944
    from: 'off'
    to: 'on'
  action:
      - service: light.turn_on
        entity_id: light.yeelight_strip1_7811dcfdc835
      - delay: 00:04:00
      - service: light.turn_off
        entity_id: light.yeelight_strip1_7811dcfdc835
        
- alias: 'light off'
  trigger:
    platform: state
    entity_id: binary_sensor.door_window_sensor_158d0002431944
    to: 'closed'
  action:
      - service: light.turn_off
        entity_id: light.yeelight_strip1_7811dcfdc835        

When I open the door the light comes on: perfect
When I close the door, unfortunately it stays on when it should go out…
AND when I open the door again the light comes off!

Anyone can understand what is happening?

Thank you!

Should be ‘off’ instead of ‘closed’. Make sure the automation is turned on from the states page as well.

Hi walrus, many thanks, It’s already better:

  • I open the door, the light comes on >> ok
  • I close the door, the light comes off >> ok
  • if I open aigain the door, nothing… I need to close aigain and only after that the light comes on when I open the door.
    it works once in two

You have an idea? (sorry for my english)

On many battery operated sensors there is a delay before they can detect again. Sometimes the delay is adjustable.

Hi bosborne,

I would like to believe you but for a few seconds, not after a delay of more than 3 minutes :frowning:
Does anyone know how to force an initial return at the end of the code?

Sure, just fix the automations. The “problem” is that the “on” automation runs for four minutes, and you can’t re-trigger an automation that’s already running.

# Turn light on when the door opens
- alias: 'light on'
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: binary_sensor.door_window_sensor_158d0002431944
      from: 'off'
      to: 'on'
  action:
    - service: light.turn_on
      entity_id: light.yeelight_strip1_7811dcfdc835

# Turn the light off if the door is left open for four minutes
- alias: 'light on after four'
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: binary_sensor.door_window_sensor_158d0002431944
      to: 'on'
      for: '00:04:00'
  action:
    - service: light.turn_off
      entity_id: light.yeelight_strip1_7811dcfdc835

# Turn the light off when the door closes
- alias: 'light off'
  trigger:
    - platform: state
      entity_id: binary_sensor.door_window_sensor_158d0002431944
      to: 'closed'
  condition:
    - condition: state
      entity_id: light.yeelight_strip1_7811dcfdc835
      state: 'on'
  action:
    - service: light.turn_off
      entity_id: light.yeelight_strip1_7811dcfdc835

I’ve also fixed the random indenting problem you had there :wink:

@Tinkerer thank you for your time! but with your code, the light turn on when I open the door but after impossible to turn off the light, even door though open and close the door several times, the light stay on :frowning:

Have you tried to put “initial_state: ‘on’” before the trigger of the second automation?

you can use the wait_template instead of delay

https://www.home-assistant.io/docs/scripts/#wait

- alias: 'light on'
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: binary_sensor.0x00158d0002450e74_contact
    from: 'off'
    to: 'on'
  action:
    - service: light.turn_on
      entity_id: light.furdo_xiaomi_philips_smart_led_ball
    - wait_template: "{{ is_state('binary_sensor.0x00158d0002450e74_contact', 'off') }}"
      timeout: '00:00:30'
      continue_on_timeout: 'true'      
    - service: light.turn_off
      entity_id: light.furdo_xiaomi_philips_smart_led_ball
3 Likes

Thank you all for your contributions ^^
@echopage yes I do but it does not correct my problem
@jungervin thank you, interesting, I test it when I get home !

Check the door sensor is reporting the state change, and that the automations are turned on

Yes, the state change is ok and all the automations are turned on

@jungervin Yes! excellent !! I’m so happy :star_struck:

All is okay now ^^

How to close this subject?

As explained in the sticky post mark the solution :wink:

1 Like

Thanks to all Hassio contributors, your work is remarkable

That’s one of the installers for Home Assistant :wink: Don’t confuse the two :stuck_out_tongue:

1 Like

oops… sorry I’m noob :sweat_smile::joy: