Lights automation mixed between sun elevation and time

Hi poeople,

I want to create a single automation that takes care of the exterior lights and Xmas lights (exteriorspecial).

The exterior will go ON from sunset to sunrise.
The exterior special will go ON from sunset to midnight

I mixed a couple of solutions I found over here, but it’s not working.

EDIT

Thanks to @DefenestrateIT and @123 I have used this automation, but there are several flows in it.
If I manually turn off the lights, whatever is the next trigger, it will turn all on again. Super weird because the conditions should not been met.

I am also trying to add another trigger at 9:00 PM but I can’t understand the condition "{{ 0 <= now().hour < 16 }}" to match the exact time.

I appreciate your help.

- alias: Scheduled Exterior
  trigger:
    - platform: numeric_state
      entity_id: sun.sun
      attribute: elevation
      below: 1.8
    - platform: numeric_state
      entity_id: sun.sun
      attribute: elevation
      above: -3.1
    - platform: time
      at: 00:00:00
    - platform: time
      at: "21:00:00"
    - platform: homeassistant
      event: start
  action:
    - variables:
        e: "{{ state_attr('sun.sun', 'elevation') | float }}"
    - choose:
        - conditions:
            - "{{ e <= 1.8 }}"
            - "{{ is_state('light.exterior', 'off') or is_state('light.exteriorspecial',
              'off') or is_state('light.on_off_plug_1_5', 'off')}}"
          sequence:
            - service: light.turn_on
              target:
                entity_id:
                  - light.exterior
                  - light.exteriorspecial
                  - light.on_off_plug_1_5
                  - light.light_plug_1_switch
        - conditions:
            - "{{ e >= -3.1 }}"
            - "{{ is_state('light.exterior', 'on') }}"
          sequence:
            - service: light.turn_off
              target:
                entity_id: light.exterior
        - conditions:
            - "{{ 0 <= now().hour < 16 }}"
            - "{{ is_state('light.exteriorspecial', 'on') }}"
            - "{{ is_state('light.light_plug_1_switch', 'on') }}"
          sequence:
            - service: light.turn_off
              target:
                entity_id:
                  - light.exteriorspecial
                  - light.light_plug_1_switch
        - conditions:
            - "{{ 0 <= now().hour < 16 }}" # Need to match the 21:00 trigger
            - "{{ is_state('light.on_off_plug_1_5', 'on') }}"
          sequence:
            - service: light.turn_off
              target:
                entity_id: light.on_off_plug_1_5
      default: []
  mode: single

You have a few error in this automation.

Did you make this using the GUI? because it should have formatted this correctly, but it isn’t.

If you are using a text editor for yaml, you are better off using Studio Code Server Add-On, it helps heaps!

I am not sure what that last “condition” is, I assume it is a default (but i think the trigger.id is going to fail).

But try this:

- alias: 'Scheduled Exterior'
  trigger:
  - platform: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: 1.8
  - platform: numeric_state
    entity_id: sun.sun
    attribute: elevation
    above: -3.1
  - platform: time
    id: 'off'
    at: 00:00
  - platform: homeassistant
    event: start
  action:
  - variables:
      e: "{{ state_attr('sun.sun', 'elevation') | float }}"
  - choose:
    - conditions:
      - "{{ e <= 1.8 }}"
      - "{{ is_state('light.exterior', 'off') }}"
      - "{{ is_state('light.exteriorspecial, 'off) }}"
      sequence:
      - service: light.turn_on
        target:
          entity_id: 
            light.exterior
            light.exteriorspecial
    - conditions:
      - "{{ e >= -3.1 }}"
      - "{{ is_state('light.exterior', 'on') }}"
      sequence:
      - service: light.turn_off
        target:
          entity_id: light.exterior
    default:
      - service: {{ 'light.turn_' + trigger.id }}
        target:
          entity_id: light.exteriorspecial
  mode: single

It’s still giving the same error. It won’t save it. I know it’s a format error, but I can’t figure it out. I want to diversify the light.turn_off trigger. One group is at a certain time, another at sunrise.

Yeah you have heaps more error’s than i thought. use the GUI in the future to create (format) automations, & use yaml to edit small parts.

Give me 20min’s & i’ll have another look.

alias: Scheduled Exterior
description: ""
trigger:
  - platform: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: 1.8
  - platform: numeric_state
    entity_id: sun.sun
    attribute: elevation
    above: -3.1
  - platform: time
    id: "off"
    at: "00:00"
  - platform: homeassistant
    event: start
condition: []
action:
  - variables:
      e: "{{ state_attr('sun.sun', 'elevation') | float }}"
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ e <= 1.8 }}"
          - condition: template
            value_template: "{{ is_state('light.exterior', 'off') }}"
          - condition: template
            value_template: "{{ is_state('light.exteriorspecial', 'off') }}"
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id:
                - light.exterior 
                - light.exteriorspecial
      - conditions:
          - condition: template
            value_template: "{{ e >= -3.1 }}"
          - condition: template
            value_template: "{{ is_state('light.exterior', 'on') }}"
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.exterior
            data: {}
      - conditions:
          - condition: trigger
            id: "off"
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.exteriorspecial
mode: single

damn it @123 you beat me to it

1 Like

I would have thought that post should have/be under “community guides”, but nice post. it might be an idea to redo it so it is compatible with the GUI tho

          - condition: template
            value_template:

so other newbies find it easier to implement.

@hatarez has also left out some close quotes E.g.

'light.exteriorspecial

Thank you all for the help. let’s try it out!

@123 Is giving me the same error. I fixed the close quotes but there is still something wrong.

Try the second one i posted

1 Like

Solid! Thanks!

@hatarez I will leave this with @123 as he/she is a lot more experienced than I.

I got this when trying to save the automation. How did you save it and check it out?

Message malformed: extra keys not allowed @ data['0']

I marked the solution since it got accepted from the automation stack. So far, the lights have gone on. I am open to correcting it if necessary.

Is there anything I should do differently other than copy a paste of your solution? It is not saving it.

They will be using the GUI “Automations” (edit in yaml).
It doesn’t like your syntax

This automation can not be edited from the UI, because it is not stored in the automations.yaml file, or doesn’t have an ID.

1 Like

That’s correct. I am not dropping the automation in the config.

I am not expert, but it sounds simple to me. Turn on all together, turn off at different events (two). But I don’t know how to do it in a way that home assistant accept and let me try.

How should I test your automation?

Developer Tools > “States” &or “Service”
Change the state of an entity & test it.

I would probably just change my last condition to default, But @123 knows this automation A lot better than I.

There is a “automations.yaml” file (in the root folder) that “Automations” go into. The GUI just edit’s that file, set-up Studio Code Server & use that to help you format yaml better, in the future.

So I created a test automation to generate an id, pasted @123 code, check the config, restart. Let’s test.

You can use “Developer Tools > Services > Automation: Trigger” to trigger an automation (to simulate a restart).
But depending how your lights are configured, I would be thinking about a power outage (lights may turn on), with the restart issue. As that is the most likely reason HAss will restart if everything else is set up correctly (no error/bugs).

And “Developer Tools > Yaml > Automations” to reload your automation config’s.

1 Like