Need Help for Automation

Hello,

I am new to this community and have almost no experience with RasPi. I currently have Home Assistant 0.66.1 running with Hassbian. I was able associating my Z-wave devices (Aeotec LED bulb, a Fibaro motion sensor, and Fibaro door sensor) with my Z stick Gen 5 hosted in a RasPI 3. I am trying to create a relatively simple automation (or scene) to do the following: When it’s dark outside (after sunset) I want the LED bulb to turn on when I open the door. Once the door is closed, I want the light to stay on for 2 minutes.

I have managed to make the light turn on using the home assistant GUI however I can’t find the way to have it turned off after the door closes. I have searched in this community and elsewhere on the web, however I found that the syntax used for most examples is not the same as what was created in my automations.yaml file; the order of the trigger, condition, and services is not the same. Here’s what was created in the yaml file using the HA UI:

- action:
  - alias: ''
    data: {}
    service: light.turn_on
  alias: Turn light on when door opens after dark
  condition:
  - after: sunset
    condition: sun
  id: '1523154489687'
  trigger:
  - entity_id: binary_sensor.sensor_3
    from: 'off'
    platform: state
    to: 'on'

Can anybody help me?

I would use the light on as a trigger, with multiple AND conditions.

Conditions would include the after sunset, and door closed. Then put a delay of 2 minutes before your action

Thanks for the suggestion. However, it is unclear what the difference is between a trigger and a condition. I’ve seen these 2 concepts interchanged in different use cases. In my scenario shouldn’t the trigger be when the door sensor goes from “on” to “off”, and the condition being when the light is on?

I have created another automation to turn off the light. This is what automations.yaml looks like:

- action:
  - service: light.turn_on
  alias: Lumière ON
  condition:
  - after: sunrise
    condition: sun
  id: '1523194569764'
  trigger:
  - entity_id: binary_sensor.sensor_3
    from: 'off'
    platform: state
    to: 'on'
- action:
  - data: {}
    service: light.turn_off
  alias: Lumière OFF
  condition: []
  id: '1523194629124'
  trigger:
  - entity_id: binary_sensor.sensor_3
    from: 'on'
    platform: state
    to: 'off'

Now the light turns on when I open the door and shuts immediately when I close the door. I am not sure about the syntax for delays.

trigger:
  - entity_id: binary_sensor.sensor_3
    from: 'on'
    platform: state
    to: 'off'
    for: '00:02:00'
1 Like

So I know there are many solutions for this, what I implmented only impacts opening/closing the door. It does not change the behavior of the lights because the door is triggering the on/off. Here is my config:

Template sensor that changes my sensor to display the correct data instead of 22/23 (may not be needed for your sensor):

  - platform: template
    sensors:
      main_door_hindge: # MAIN DOOR SENSOR #
        value_template: >
          {% if is_state('sensor.main_door_t_access_control_9_9', '23') %}
            closed
          {% elif is_state('sensor.main_door_t_access_control_9_9', '22') %}
            open
          {% else %}
            closed
          {% endif %}
        friendly_name: Main Door Status

Automation that handles turning the light on:

- alias: Foyer light trigger on main door 
  trigger:
    - platform: state
      entity_id: sensor.main_door_hindge
      to: 'open'
  condition:
    - condition: state
      entity_id: sun.sun
      state: "below_horizon"
  action:
    - service: homeassistant.turn_on
      entity_id: script.foyer_door_is_open

Script to turn on the light and create a 2 minute timer. When the 2 minute timer is up, it will fire my turn off script. If you open the door again, it resets the timer and a new 2 minute timer is created. Thus making it so the door light may be on for more than 2 minutes if you are opening and closing the door (like bringing in groceries):

alias: Foyer Door is Open
sequence:
  # Cancel ev. old timers
  - service: script.turn_off
    data:
      entity_id: script.foyer_light_timer
  - service: switch.turn_on
    data:
      entity_id: switch.foyer_s_switch_7_0
  # Set new timer
  - service: script.turn_on
    data:
      entity_id: script.foyer_light_timer

And lastly, the script that starts a timer and shuts the light off. This script gets canceled and restarted when the door is opened again.

alias: Foyer Light Timer
sequence:
  - delay:
      minutes: 2
  - service: switch.turn_off
    data:
      entity_id: switch.foyer_s_switch_7_0

A trigger is what causes the automation to fire to check for conditions before it performs an action.

You can’t use conditions and triggers interchangeably. A trigger causes the thing to happen, a condition decides if that thing should continue to perform the action.

1 Like

Just read the documentation

1 Like

Thanks. In my case however, what would be the trigger, versus the condition? Again, intuitively I would think the open door is the trigger whereas after sunset is the condition. However couldn’t both the open door and sunset be triggers?

For sure you can! Use like this:

  trigger:
    - platform: state
      entity_id: binary_sensor.sensor_3
      to: 'on'
    - platform: sun
      event: sunset
  condition:  
    condition: and
    conditions:
      - condition: sun
        after: sunset
      - condition: state
        entity_id: binary_sensor.sensor_3
        state: 'on'
  action:
    - service: light.turn_on
      entity_id: **YOU HAVE TO DEFINE AN ENTITY IN YOUR AUTOMATION**
1 Like

I managed to make it work. Not sure whether this is the most optimal way, but it does work.

  - service: light.turn_on
  alias: Lumière ON lorsque porte ouverte (soir)
  condition:
  - after: sunset
    condition: sun
  id: '1523194569764'
  trigger:
  - entity_id: binary_sensor.sensor_3
    from: 'off'
    platform: state
    to: 'on'
- action:
  - delay:
      seconds: 90
  - data: {}
    service: light.turn_off
  alias: Lumière OFF 90s après porte fermée
  condition: []
  id: '1523194629124'
  trigger:
  - entity_id: binary_sensor.sensor_3
    from: 'on'
    platform: state
    to: 'off'

Thanks everyone for your input!!

And I’m surprised that no one has said this but to make things easier and to get you used to seeing how the syntax of your code compares to the examples given in the documentation I would stay away from using the automation editor. That thing is atrocious! It completely jumbles up the code in the result and makes it SO HARD to follow what is actually occurring. Just start writing out the code manually following the docs and you’ll get the hang of it pretty soon.

Honestly with as many complaints as there have been I’m kind of surprised why that hasn’t been addressed.

Every example that I’ve ever seen in the docs use the same layout but then when the creators make an editor they don’t even come close to ensuring it follows the same layout. It just confuses the new people. And even some experienced users too.

It should be fixed or eliminated.

1 Like

It hasn’t been addressed because it may not be possible with all sections of code. When the automations are saved to disk, it just prints out the ordered dictionary. You’ll notice that it’s all alphabetical order. Someone would have to make an order map for all possible combinations. It would be a TON of work, unless they only deal with known keywords and place the rest in alphabetical order.