Automation with multiple conditions and sunset offset

Hi all,

I’m having some difficulty creating automation that will turn the lights out from 45 mins before sunset but only when I’m home (also when we get back after sunset).
The config I currently have is:

  • alias: ‘Lampen aan bij donker.’
    trigger:
    • platform: sun
      event: sunset
      offset: “-00:45:00”
    • platform: state
      entity_id: group.tracking
      from: ‘not_home’
      to: ‘home’
      condition:
      condition: state
      entity_id: group.tracking
      state: home
      action:
      service: scene.turn_on
      entity_id: scene.lampen_aan

This will turn on the lights 45 mins before sunset, but also always when I get home (my own devices are in the group tracking). My problem is that I only want the light to turn on when it’s 45 mins before sunset or later if I arrive home.
Is there any way to make this happen? Thank you for your time.

Kind regards,
Guido Jurriens

Put the sun comparison in as a condition.

You can “and” conditions like this:

condition:
  condition: and
    conditions: 
      - condition: state
        entity_id: group.tracking
        to: 'home'
        from: 'not_home'
      - condition: sun
        after: sunset
        after_offset: "-00:45:00"

I’m doing this freehand, so I might not have it exactly correct - I don’t have any conditions where I use a sun condition. I grabbed the “after_offset” stuff from here.

Then you’re basically saying: trigger whenever I get home or when it’s 45 min to sunset, but stop unless someone’s home or it’s around sunset.

Thank you, that seems very likely.
I’ll know tomorrow if this will work as expected.
I’ll post in here when I find out.

I’ve put in the config (see below), but unfortunately it is not working.
On start I’m getting the following error in the log (and HASS does not start): 16-10-26 16:24:44 homeassistant.util.yaml: mapping values are not allowed here
in “/home/hass/.homeassistant/automation.yaml”, line 13, column 18

  • alias: ‘Lampen aan bij donker.’
    trigger:
    • platform: sun
      event: sunset
      offset: “-00:45:00”
    • platform: state
      entity_id: group.tracking
      from: ‘not_home’
      to: ‘home’
      condition: and
    • condition: state
      entity_id: group.tracking
      state: home
    • condition: sun
      after: sunset
      after_offset: “-00:45:00”
      action:
      service: scene.turn_on
      entity_id: scene.lampen_aan

I also had a conditions: and before al conditions, but that also gave the same error.

When you post, be sure to use “preformatted text” so that we can see indentation.

It looks like you’ve got “condition” inside your trigger - which you can’t do. Conditions for an automation should be on the same level as trigger.

Oke, another try in preformatted text.

   - alias: 'Lampen aan bij donker.'
     trigger:
     - platform: sun
       event: sunset  
       offset: "-00:45:00"
     - platform: state
       entity_id: group.tracking
       from: 'not_home'
       to: 'home'
    condition:
      conditions: and
        - condition: state
          entity_id: group.tracking
          state: home
        - condition: sun
          after: sunset
          after_offset: "-00:45:00"
    action:
      service: scene.turn_on
      entity_id: scene.lampen_aan

And I’ve also tried without conditions (changing the indent).

I find that remembering the formatting for conditions is difficult! I always have to refer to where I’ve used it previously…
Try:

- alias: 'Lampen aan bij donker.'
     trigger:
       - platform: sun
         event: sunset  
         offset: "-00:45:00"
       - platform: state
         entity_id: group.tracking
         from: 'not_home'
         to: 'home'
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: group.tracking
          state: home
        - condition: sun
          after: sunset
          after_offset: "-00:45:00"
    action:
      service: scene.turn_on
      entity_id: scene.lampen_aan
2 Likes

Thank you, I’ll try that and post back.

HASS starts at least.
I’ll post later to see if it gives out the right effect.

Thank you for your time.

As promised…
Just reporting back to confirm that all is working now.

Thank you for your time @ih8gates

Glad you got it working. The first time you do a multiple-condition and/or is difficult. Now you have an example to build other stuff from.