Xiaomi door & window contact help

Hi,

I’m having an issue with getting the automation to work and i can’t figure out why at all.

Here is my automation

- alias: If the window is open turn the light on
  trigger:
    platform: state
    entity_id: binary_sensor.door_window_sensor_158d00033aad5b2
    from: 'off'
    to: 'on'
  action:
    - service: homeassistant.turn_on
      entity_id: switch.lounge

as you can see it’s a pretty basic automation so far as i just want to test it. I have used dev tools to change the state but no joy even physically moving the two sensors doesn’t turn it on.

Anyone any idea as i’m stumped even after looking online and following examples, etc.

Thanks!

Here’s one of my examples, sorry for the screenshot, ssh terminal doesn’t want to copy paste all lines.

As you can see, no need to specify the From parameter :slight_smile:

I’m not 100% sure as I’m not working with switches in combination with automations.

But it could be you need to use

service: switch.turn_on
Instead of homeassistant.turn_on.

And else maybe service: script.turn_on.

Hope this works

Hi richatha,
i have just created an automation and it runs without problems. I attach a screenshot. I have not written the automation in the “automation.yaml” itself, but created on the HA UI … The exact formatting is very important here !!!

I would change:

- service: homeassistant.turn_on

to

- service: switch.turn_on

Plus, just in case add:

  initial_state: 'on'

This refers to the status of the automation itself. It was a requirement in all automations at one point, so your code would look like:

- alias: If the window is open turn the light on
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: binary_sensor.door_window_sensor_158d00033aad5b2
    from: 'off'
    to: 'on'
  action:
    - service: switch.turn_on
      entity_id: switch.lounge

Thanks for everyones suggestions but still not having any luck with the light coming on.

I can still see in the entities list that when i pull the contact away from each other the state changes from off to on but then the switch isn’t triggering. Have tried all your combinations so far with no avail.

Anything else wrong?

Here is my current code

- alias: If the door is open turn the light on
  trigger:
    platform: state
    entity_id: binary_sensor.door_window_sensor_158d00033aad5b2
    to: 'on'
  action:
  - data:
    - service: switch.turn_on
      entity_id: switch.lounge

I have also tried a motion sensor too and that one didn’t work either!

You have to remove " - data" under action.

Legend i’ve just got the light to come on at last!

I think it had to do with my indentations!