Very simple time based automation

Hi all

switching from iobroker to HA (and really missing blockly …)
I tried to do a simple automation of my lights at the house.

turn on at 06:00 in the morning, turn off when sun rises (when sun rises before 06:00 basically nothign happens)
turn on when sun sets, turn off again at 23:00 latest

I did the following based on documentation I found:


alias: Aussenbeleuchtung Zeit
description: ""
trigger:
  - platform: sun
    event: sunrise
    offset: 0
    id: Licht_aus
  - platform: sun
    event: sunset
    offset: 0
    id: Licht_an
  - platform: time
    at: "06:00:00"
    id: Licht_an
  - platform: time
    at: "23:00:00"
    id: Licht_aus
condition: []
action:
  - condition: trigger
    id:
      - Licht_an
  - type: turn_on
    device_id: 48760ba4af6555ca4910123809db20e9
    entity_id: a8949354d96fe69293413b94401089cf
    domain: switch
  - condition: trigger
    id:
      - Licht_aus
  - type: turn_off
    device_id: 48760ba4af6555ca4910123809db20e9
    entity_id: a8949354d96fe69293413b94401089cf
    domain: switch
mode: single

I’d expect the action to be more “indented” underneath the trigger

But it isn’t. Why would you expect that? Or to say it another way, what is the goal of your question?

Indenting it would make it a property of the conditions. Actions belong to the automation, not the conditions.

You are using the wrong type here.
The condition you are using is a full stop of the code if it fails.
Meaning the automation will work with turning on, but not off since it’s below the turn on condition.

What you should be using is choose.
And the condition in the choose is the condition you have here.
Add the action, then add a new option and set up the second condition.

If you now open the Yaml mode you will see that the code is more indented just as you expected.

I’m impressed with your first attempt of an automation.
It’s not easy to know the difference between condition and choose.

The only thing we are missing when the above is done is the condition of sun up before 6.
You can add that as a state condition, sun.sun below horizon.

2 Likes

I see, thanks a lot for your help here.

so, this should work:

alias: Aussenbeleuchtung Zeit
description: ""
trigger:
  - platform: sun
    event: sunrise
    offset: 0
    id: Licht_aus
  - platform: sun
    event: sunset
    offset: 0
    id: Licht_an
  - platform: time
    at: "06:00:00"
    id: Licht_an
  - platform: time
    at: "23:00:00"
    id: Licht_aus
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Licht_an
          - condition: state
            entity_id: sun.sun
            state: below_horizon
        sequence:
          - type: turn_on
            device_id: 48760ba4af6555ca4910123809db20e9
            entity_id: a8949354d96fe69293413b94401089cf
            domain: switch
      - conditions:
          - condition: trigger
            id:
              - Licht_aus
        sequence:
          - type: turn_off
            device_id: 48760ba4af6555ca4910123809db20e9
            entity_id: a8949354d96fe69293413b94401089cf
            domain: switch
mode: single

Or even this, using your switch’s entity ID rather than device:

alias: Aussenbeleuchtung Zeit
description: ""
trigger:
  - platform: state
    entity_id: sun.sun
    to: 'above_horizon'
    id: off
  - platform: state
    entity_id: sun.sun
    to: 'below_horizon'
    id: on
  - platform: time
    at: "06:00:00"
    id: on
  - platform: time
    at: "23:00:00"
    id: off
condition:
  - or:
    - "{{ trigger.id == 'on' and now().hour >=6 and is_state('sun.sun', 'below_horizon') }}"
    - "{{ trigger.id == 'off' }}"
action:
  - service: switch.turn_{{ trigger.id }}
    target:
      entity_id: switch.YOUR_SWITCH_ID
1 Like

Yes I believe so.

Shouldn’t the lights turn on at 6?
Keep in mind the condition that the lights should not turn on of the sun is up before 6.

1 Like

Quite right, fixed both. Thanks.

1 Like