Issue with Door Sensor

Hello Community i am really new to HA and search here for help.

My Name is Daniel and i am Happy to be here.

Iam using an RPi3+ with an Raspeeshield and a manually installed Homeassistant(not HASSIO)

i played around and everything works really fine and easy but i have a problem and couldnt solve it.
I have an Aquara door sensor and he works, now i want when my “stehlampe” is on and somebody opens the door than the stehlampe switch off. And this works but i also want that when the door gets closed the light should switch back to on and this didnt work.

This is my automation.yaml

  • id: ‘1534705610002’
    alias: Insektenschutz
    trigger:
    • entity_id: binary_sensor.openclose_5
      platform: state
      condition:
      condition: or
      conditions:
      - condition: sun
      after: sunset
      - condition: sun
      before: sunrise
      action:
    • data:
      entity_id: light.stehlampe
      service: light.toggle

Hope that somebody could help me

Could you please apply proper formatting to your code snippet so that it is displayed as it is in your yaml-file - for instructions see the blue box at the top of the page.

As suggested you really need to properly format your YAML code, otherwise it makes if very difficult to read, and also indentation errors are impossible to see.

You didn’t say, but I’ll assume that binary_sensor.openclose_5 being ‘on’ indicates the door is open, and being ‘off’ indicates the door is closed. (That’s the typical meaning. See Binary Sensor Device Class for more detail.)

I would recommend using the light.turn_on and light.turn_off services for this instead of light.toggle. If the light got turned on or off by some other means, then this automation would get “out of sync” using toggle.

Also you didn’t mention in your description of how you want this automation to work anything about time of day, but it looks like that matters based on your current automation.

So, putting everything together, I would suggest this:

- id: '1534705610002'
  alias: Insektenschutz
  trigger:
    platform: state
    entity_id: binary_sensor.openclose_5
  condition:
    condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
    service_template: >
      # If door opened, turn light off.
      {% if trigger.to_state.state == 'on' %}
        light.turn_off
      # Else door closed, so turn light on.
      {% else %}
        light.turn_on
      {% endif %}
    entity_id: light.stehlampe

This automation will trigger whenever binary_sensor.openclose_5 changes, and the action will run at that time if it’s after sunset or before sunrise (i.e., the sun is down.) The service_template determines which service to run. It will turn the light off if binary_sensor.openclose_5 just changed to ‘on’ (i.e., the door opened.) Otherwise it will turn the light on (i.e., the door closed.)

Wow thx for help. O will try this tomorrow.
Yes i tryed to use the code component for the topic bit i do not really understand how this works. Also the held didnt help me hahaha i am so sorry about that

hmmm okay i teste it but it only switches off the light but not on again.

- id: '1535744124978'
  alias: Insektenschutz
  trigger:
  - entity_id: binary_sensor.openclose_5
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
  - service_template: >
      {% if trigger.to_state.state == 'on' %}
        light.turn_off
      {% else %}
        light.turn_on
      {% endif %}
    entity_id: light.stehlampe

Try taking out the from: ‘off’ and to: ‘on’, then automation will trigger whenever state changes, not just from off to on.

Exactly. That’s why my suggested automation didn’t have those. :slight_smile:

Hi yes but as i used your code the konfiguration made an error. But i will try it again today. But really thx so far

If you’re not getting an error now, then removing those two lines should not cause an error. If you do get an error, though, post it and it might give a clue what’s wrong. Good luck!

Hey so now i had time to test it but its not perfect.
The light turn on and off if sun is down.
But its always turns on and off also if the light was not turned on before

- id: '1535744124978'
  alias: Insektenschutz
  trigger:
    platform: state
    entity_id: binary_sensor.openclose_5
  condition:
    condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
    service_template: >
      {% if trigger.to_state.state == 'on' %}
        light.turn_off
      {% else %}
        light.turn_on
      {% endif %}
    entity_id: light.stehlampe

Ok, then try this instead:

- id: '1535744124978'
  alias: Insektenschutz
  trigger:
    platform: state
    entity_id: binary_sensor.openclose_5
    to: 'on'
  condition:
    - condition: state
      entity_id: light.stehlampe
      state: 'on'
    - condition: state
      entity_id: sun.sun
      state: below_horizon
  action:
    - service: light.turn_off
      entity_id: light.stehlampe
    - wait_template: "{{ is_state('binary_sensor.openclose_5', 'off') }}"
    - service: light.turn_on
      entity_id: light.stehlampe

This automation will only run when the door opens while the light is on and it’s dark. It will turn the light off, wait for the door to close, then turn the light on.