Please Help with Sunset Automation

I am sorry, I don’t mean to come across as lazy, but I am just plain tired from fighting YAML. I have been trying to get this automation working and between hunting down spaces that are not there, yet should be, and errors in the log that don’t point to some syntactical issue I’m just about fried. Sorry HA developers and contributors, I absolutely love your platform, but without question, they choice for configuration language (in my opinion) sucks.

At any rate, I am trying to learn and I keep getting thwarted by the documentation examples that seem to have no basis in reality (I have yet to follow one that has worked). I put together the below automation. I have two conditions:

  1. It must be 30 minutes before sunset
  2. AND the back door must be open

These conditions being met, I want to turn on two light (for when we let the dogs out). Then, when the back door is closed, turn the lights back off. Just don’t turn them on when the door opens unless it is 30 minutes before sunset.

I haven’t gotten to the turn off part simply because I have fought just getting the lights to turn on.

automation:
  - alias: Backyard Auto Lights ON
    trigger:
      platform: sun
      event: sunset
      offset: "-00:30:00"
    condition:
      condition: state
      entity_id: binary_sensor.aeotec_zw112_door_window_sensor_6_sensor_47_0
      state: 'on'
    action:
      service: switch.turn_on
      entity_id:
        - switch.ge_12722_onoff_relay_switch_switch_42_0
        - switch.ge_12722_onoff_relay_switch_switch_43_0

In addition to all that I am also seeing the below error in the logs:

homeassistant.core: Unable to find service switch/turn_off

Does anyone have any ideas on that or should I start a new post and see what we can figure out?

Two things I notice:

First, I feel like your trigger and your conditions are reversed. The door opening should be the trigger. The period before sunset should be the condition for your actions. In addition, you want it to run either anytime AFTER sunset or 30 minutes BEFORE sunset, so you should have both conditions in there, like this:

  condition:
    condition: sun
    after: sunset
    after_offset: "-00:30:00"

Secondly I don’t think you can specify multiple entities like that though I can’t find anything in the docs either way. I would consider doing it like this:

    action:
      - service: switch.turn_on
        entity_id: switch.ge_12722_onoff_relay_switch_switch_42_0
      - service: switch.turn_on
        entity_id: switch.ge_12722_onoff_relay_switch_switch_43_0

@rpitera

Geeee, I am beginning to really, really not like the documentation here because it is hard to know whether or not I can trust it for accuracy or for being current. The documentation does not even mention that with that which covers conditions.

I think I tried reversing them (triggers and conditions) but am not sure. However, I will certainly test it again with your suggestions and see if it works. Thank you for your help.

I just edited and added something so please refresh your browser and reread. :slight_smile:

I saw it, it live updated in fact. I would have NEVER figured out the two after sunset parameters. That’s not even hinted at in the documents. I shall be back shortly with an update once I try your suggestions.

This is what I came up with:

  - alias: Back Yard Auto Lights ON
    trigger:
      platform: state
      entity_id: binary_sensor.aeotec_zw112_door_window_sensor_6_sensor_47_0
      state: 'on'
    conditon:
      platform: sun
      after: sunset
      after_offset: "-00:30:00"
    action:
      - service: switch.turn_on
        entity_id: switch.ge_12722_onoff_relay_switch_switch_42_0
      - service: switch.turn_on
        entity_id: switch.ge_12722_onoff_relay_switch_switch_43_0

And I got this error in the logs after reloading the automation from HA and that pop up card alerting me to an invalid config:

16-12-26 22:02:29 homeassistant.bootstrap: Invalid config for [automation]: [conditon] is an invalid option for [automation]. Check: automation->conditon. (See /home/hass/.homeassistant/automations.yaml:5). Please check the docs at https://home-assistant.io/components/automation/

I caught the spelling error too, didn’t change anything.

Good God!!! I finally got them to turn on. Now, I got to figure out how to turn them off I guess:

  - alias: Back Yard Auto Lights ON
    trigger:
      platform: state
      entity_id: binary_sensor.aeotec_zw112_door_window_sensor_6_sensor_47_0
      state: 'on'
    condition:
      condition: and
      conditions:
        - condition: sun
          after: sunset
          after_offset: "-00:30:00"
    action:
      - service: switch.turn_on
        entity_id: switch.ge_12722_onoff_relay_switch_switch_42_0
      - service: switch.turn_on
        entity_id: switch.ge_12722_onoff_relay_switch_switch_43_0

Try this:

  - alias: Back Yard Auto Lights ON
    trigger:
      platform: state
      entity_id: binary_sensor.aeotec_zw112_door_window_sensor_6_sensor_47_0
    condition:
      condition: sun
      after: sunset
      after_offset: "-00:30:00"
    action:
      service_template: >
        {% if trigger.to_state.state == "on" %}
        homeassistant.turn_on
        {% elif trigger.to_state.state == "off" %}
        homeassistant.turn_off
        {% endif %}

      entity_id:
        - switch.ge_12722_onoff_relay_switch_switch_42_0
        - switch.ge_12722_onoff_relay_switch_switch_43_0
1 Like

When I went to bed last night, the automation I had posted above was working, the lights came on when the door opened and went back off when the door was closed. I get up this morning, now it doesn’t work. I can see that the door sensor is working and that HA is changing states from off to on for that sensor, but the lights are doing nothing. No errors in HA, when I reload the automation file there is no errors, ugggg.

@omeasire thank you for that code, I like that a lot as it is the on and off sequence in one bit of code. Given the issue this morning mentioned above, I will try that out and see if anything changes. I can’t possibly imagine what changed in just a few hours so as the automation to not work.

Man, I just want things to work, LOL

Went and opened the back yard door, manually turned on the lights, and it never reported back to HA that the lights are on.

Check this topic:

Maybe this is the reason why your automation didn’t work in the morning.
In that case you may try to check the state of sun sensor (above/below_horizon) in condition.

Also, are you sure that your switches should report their state?

@omeasire, thank you for that link. And yes, the switches do report their state and have with every automation system I have used. I have never known them not to, at least as far as I have observed.

That appears to have done the trick.

I guess the next question would be can you do offsets from that? It’s not an absolutely huge deal, but I like being able to turn the lights on shortly before sunset and back off slightly after.

1 Like

My idea is to use three automations:

  • ‘lights’, for turning lights on
  • ‘sun’, for turning ‘lights’ on when sunset/sunrise is coming
  • ‘restart’, for turning ‘lights’ on if, for some reason, homeassistant (or machine, running it) restarts in night time.

‘light’:

  - alias: Back Yard Lights Control
    initial_state: False
    trigger:
      platform: state
      entity_id: binary_sensor.aeotec_zw112_door_window_sensor_6_sensor_47_0
    action:
      service_template: >
        {% if is_state("trigger.to_state.state", "on") %}
        homeassistant.turn_on
        {% elif is_state("trigger.to_state.state", "off") %}
        homeassistant.turn_off
        {% endif %}

      entity_id:
        - switch.ge_12722_onoff_relay_switch_switch_42_0
        - switch.ge_12722_onoff_relay_switch_switch_43_0

‘sun’:

  - alias: Back Yard Lights Auto
    trigger:
      - platform: sun
        event: sunset
        offset: "-00:30:00"
      - platform: sun
        event: sunrise
        offset: "00:30:00"
    action:
      service_template: >
        {% if trigger.event == "sunset" %}
        homeassistant.turn_on
        {% elif trigger.event == "sunrise" %}
        homeassistant.turn_off
        {% endif %}
    
      entity_id: automation.back_yard_lights_control

‘restart’:

  - alias: Check sun state on start
    trigger:
      platform: event
      event_type: homeassistant_start
    condition:
      condition: state
      entity_id: sun.sun
      state: "below_horizon"
    action:
      service: homeassistant.turn_on
      entity_id: automation.back_yard_lights_control

But, as always, it needs to be tested.

4 Likes

Wow, I like the way you think. Though I don’t actually need the lights on, or to come on with the door being opened before sunset. All the lights are LED lights including the yard flood lights so it would not be a big spot on the bill if they should turn on every time the door was opened.

However, the idea of checking the automation each time Home Assistant is restarted is just plain brilliant. I did not know that could be done. I had a very practical use for that yesterday and did not know how to go about doing it so I ended up doing so manually.

So this is valid then? I couldn’t find any reference.

Why not?

First example from this page:

automation:
  # Change the light in the kitchen and living room to 150 brightness and color red.
  trigger:
    platform: sun
    event: sunset
  action:
    service: homeassistant.turn_on
    entity_id:
      - light.kitchen
      - light.living_room
    data:
      brightness: 150
      rgb_color: [255, 0, 0]

I never saw multiple entities expressed this way before or that page. That’s why I asked.

It makes sense as it follows YAML conventions but in all the time I’ve been here I’ve never seen it used.