Home Automation - If sun is below horizon

Hello, i neew help with automations. I created automation to automatic turn on light + open curtains when i’m back to home.

- alias: Leaver Cover Close
  trigger:
    platform: state
    entity_id: person.kamil
    from: "not_home"
    to: "home"
  action:
  - service: cover.open_cover
    entity_id: cover.living_room_cover
  - service: cover.open_cover
    entity_id: cover.kitchen_cover
  - service: homeassistant.turn_on
    entity_id: light.decoration_wall_light_left
  - service: homeassistant.turn_on
    entity_id: light.decoration_wall_light_right

I want to make the lights change to ON only when sun is below horizon, can i do that? This:

  - service: homeassistant.turn_on
    entity_id: light.decoration_wall_light_left
  - service: homeassistant.turn_on
    entity_id: light.decoration_wall_light_right

You can use light.turn_on as the service,

  - service: light.turn_on
    entity_id: light.decoration_wall_light_left, light.decoration_wall_light_right

For the Automation on sunset you can use the trigger:

    - event: sunset
      platform: sun

I want to turn on lights only if is night. Trigger is coming to home

Add a condition:

- alias: Leaver Cover Close
  trigger:
    platform: state
    entity_id: person.kamil
    from: "not_home"
    to: "home"
  condition:
    condition: or 
    conditions:
      - condition: sun
        after: sunset
      - condition: sun
        before: sunrise
  action:
  - service: cover.open_cover
    entity_id: cover.living_room_cover
  - service: cover.open_cover
    entity_id: cover.kitchen_cover
  - service: homeassistant.turn_on
    entity_id: light.decoration_wall_light_left
  - service: homeassistant.turn_on
    entity_id: light.decoration_wall_light_right

Alternatively you can add this sensor:

- platform: template
  sensor:
    sun_elevation:
      value_template: '{{ states.sun.sun.attributes.elevation }}'
      friendly_name: "Sun Elevation"

And add the numeric condition below: 0

If you use a condition for sunset, all the actions (incl open covers) will only run after sunset. I’d suggest creating a 2nd automation for the lights only, and only use the sunset condition there.

Or (if that is indeed the wanted action) just move the condition to the actions.

- alias: Leaver Cover Close
  trigger:
    platform: state
    entity_id: person.kamil
    from: "not_home"
    to: "home"
  action:
  - service: cover.open_cover
    entity_id: cover.living_room_cover
  - service: cover.open_cover
    entity_id: cover.kitchen_cover
  - condition: or 
      conditions:
        - condition: sun
          after: sunset
        - condition: sun
          before: sunrise
  - service: light.turn_on
    entity_id: light.decoration_wall_light_left
  - service: light.turn_on
    entity_id: light.decoration_wall_light_right
1 Like