Should I use Condition or Trigger in this Automation?

Hi Guys,

I have an automation taken from the example here on the site, which triggers my lights based on a movement senor, which works great. See below.

I would also like to make sure the light is only triggered when there is movement and it’s dark. So I thought I could use the ‘Sun’ option, but I’m unclear as to whether it should be a trigger or a condition? I would appreciate some help, thanks.

  • alias: Turn on hall lights when there is movement
    trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_3_1
    to: ‘on’
    action:
    service: homeassistant.turn_on
    entity_id: script.timed_lamp

script:
timed_lamp:
alias: “Turn on lamp and set timer”
sequence:
# Cancel ev. old timers
- service: script.turn_off
data:
entity_id: script.timer_off
- service: light.turn_on
data:
entity_id: light.hall
# Set new timer
- service: script.turn_on
data:
entity_id: script.timer_off

timer_off:
alias: “Turn off lamp after 5 minutes”
sequence:
- delay:
minutes: 5
- service: light.turn_off
data:
entity_id: light.hall

Use condition

Also, when posting code, use the preformatted text option so we can see the code properly.

Highlight your code block and press the preformatted text button like so:

Thanks, so where should I put the condition in the script or in the first part of the automation? so this condition will work?

condition: sun
after: sunset

- alias: Turn on hall lights when there is movement
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_3_1
    to: 'on'
  action:
    service: homeassistant.turn_on
    entity_id: script.timed_lamp

script:
  timed_lamp:
    alias: "Turn on lamp and set timer"
    sequence:
      # Cancel ev. old timers
      - service: script.turn_off
        data:
           entity_id: script.timer_off
      - service: light.turn_on
        data:
          entity_id: light.hall
      # Set new timer
      - service: script.turn_on
        data:
          entity_id: script.timer_off

  timer_off:
    alias: "Turn off lamp after 5 minutes"
    sequence:
      - delay:
          minutes: 5
      - service: light.turn_off
        data:
          entity_id: light.hall

You can put in the automation or in the script, both can work the same way.

I think it is better to make a condition for the sun elevation, since it gives a better estimate of darkness.
Here is a similar automation I use: https://github.com/Danielhiversen/home-assistant_config/blob/master/automation/lighting.yaml#L145

Great thanks, sorry for my ignorance, but I assume you mean this part of your automation? but how do you work out the elevation? I already know the elevation of my home, which is 63, so do I just change to below:63 ??

 condition:
    - condition: numeric_state
      entity_id: sun.sun
      value_template: '{{ state.attributes.elevation }}'
      below: 8

It is the sun elevation,
Go to your_ip:8123/dev-state and search for sun.
Then you can see the current elevation of the sun. If you watch the value when it gets dark, you get a good indication of a starting value. Then you can adjust it later.

I see, These are my readings right now, but the sun set around two hours ago, so your saying I will need to check around sunset tomorrow for the elevation then and use that number in my condition?

below_horizon	azimuth: 267.55
elevation: -27.24
next_setting: 2016-12-05T15:52:15+00:00
next_rising: 2016-12-05T07:46:03+00:00
friendly_name: Sun

I think that will give the best result.
But you can start by setting it to 10, and then fine tune it later

You can also ditch the elevation and just use sunset/sunrise :

 - condition: sun
   after: 'sunset'

https://github.com/CCOSTAN/Home-AssistantConfig/blob/master/script/front_house_motion.yaml#L15-L16