Help with automation - sun and state change from not_home to home

I am trying to create an automation that will switch on my lights when I come home, but only when it is dark outside.

This is what I have now. But It does not work. Any ideas?

      - id: auto444
  alias: Lys på når jeg kommer hjem og mørkt
  initial_state: True
  hide_entity: False
  trigger:
    platform: state
    entity_id: device_tracker.olsen_iphone
    from: 'not_home'
    to: 'home'
  condition: or
      conditions:
       - condition: sun
         before: sunset
         before_offset: "1:00:00"
       - condition: sun
         after: sunset
  action:
    - service: scene.turn_on
      entity_id: scene.stuekjokkengang_minimum_lys
    - service: notify.pushover
      data:
        title: "tittel"
        message: "Welcome Home! Lights ON!"

If that’s an exact paste you’re missing the actual declaration of your condition block, and running straight in to your OR conditions.

Try

- id: auto444
  alias: Lys på når jeg kommer hjem og mørkt
  initial_state: True
  hide_entity: False
  trigger:
    platform: state
    entity_id: device_tracker.olsen_iphone
    from: ‘not_home’
    to: ‘home’
  condition:
    condition: or
    conditions:
      - condition: sun
        before: sunset
        before_offset: “1:00:00”
      - condition: sun
        after: sunset
  action:
    - service: scene.turn_on
      entity_id: scene.stuekjokkengang_minimum_lys
    - service: notify.pushover
      data:
        title: “tittel”
        message: “Welcome Home! Lights ON!”
1 Like

thank you so much. I tried you code, but got a validation error:

Invalid config for [automation]: extra keys not allowed @ data['condition'][0]['conditions'][0]['before']. Got None
extra keys not allowed @ data['condition'][0]['conditions'][0]['before_offset']. Got None
not a valid value for dictionary value @ data['condition'][0]['conditions'][0]['condition']. Got None

This is the code I used (not changed):

- id: auto444
  alias: Lys på når jeg kommer hjem og mørkt
  initial_state: True
  hide_entity: False
  trigger:
    platform: state
    entity_id: device_tracker.olsen_iphone
    from: ‘not_home’
    to: ‘home’
  condition:
    condition: or
    conditions:
      - condition: sun
        before: sunset
        before_offset: “1:00:00”
      - condition: sun
        after: sunset
  action:
    - service: scene.turn_on
      entity_id: scene.stuekjokkengang_minimum_lys
    - service: notify.pushover
      data:
        title: “tittel”
        message: “Welcome Home! Lights ON!”

What editor are you using?

I copied the unformatted text from your original post, which I’ve just realised has the wrong type of quote marks in it, they need changing to normal single and double quotes instead of the angled ones.

1 Like

You should change your quotes for standard quotes like these " " and these ’ '.

1 Like

I am using Visual Studio Code.

Thank you @anon43302295 and @keithh666 :smile:

Just did a test with this script, turned on lights like it should when I came home. Now I just have to do the same test when it is daylight.

Only one problem, I did not get a push notification. Did I forget something in the code to make it send the message?

Show us your notification configuration (redacted), and is there an error in the log from the time the automation activated?

I guess I do not have the notification configuration set. I only have the “ios:” code in configuration.yaml.
What do I need?

# Example configuration.yaml entry
notify:
  - name: NOTIFIER_NAME
    platform: pushover
    api_key: ABCDEFGHJKLMNOPQRSTUVXYZ
    user_key: ABCDEFGHJKLMNOPQRSTUVXYZ

From https://www.home-assistant.io/components/notify.pushover/

2 Likes

Thank you so much!

I created an account at pushover.net and created my app to get the api_key.

Added code in configuration.yaml:

notify:
  - name: Push_meldinger
    platform: pushover
    api_key: !secret pushover_api_key
    user_key: !secret pushover_user_key

But I get a error message in my log:
## Log Details (WARNING)

Fri Oct 19 2018 11:40:51 GMT+0200 (sentraleuropeisk sommertid)

Unable to find service notify/pushover

Checked both api keys and they are right. Did I miss something?

I guess this will solve it:
- service: notify.notify

Just did another test with the “coming home script”. It did turn on the light, even if the sun is up. Or since I live near the polar circle the sun is not very high at this time of year. Will I need to use another method to make this work? I only want the lights to turn on when it is dark outside. Or getting dark.

It would be notify.push_meldinger based on your configuration entry.

Re the sun, have you ensured that your longitude and latitude are set correctly? Also, check your dev panel for next sunrise and sunset times to see whether this will be reliable for you. If not you may need to use a device with a lux meter in it and use the actual light levels to inform the system.

1 Like

Long and lat is correct, it works perfect with my driveway lights.

Did another test, still turning on lights even if it is daylight.
Could there be another thing we are missing in the code?

- id: auto444
  alias: Lys på når jeg kommer hjem og mørkt
  initial_state: True
  hide_entity: False
  trigger:
    platform: state
    entity_id: device_tracker.olsen_iphone
    from: 'not_home'
    to: 'home'
  condition:
    condition: or
    conditions:
      - condition: sun
        before: sunset
        before_offset: "-00:20:00"
      - condition: sun
        after: sunset
  action:
    - service: scene.turn_on
      entity_id: scene.stuekjokkengang_minimum_lys
    - service: notify.push_meldinger
      data:
        title: "Casa de Olsen:"
        message: "Velkommen hjem! Lys slått på!"

Yeah, shouldn’t it be before sunrise for the first one. Your conditions are basically before and after sunset, which is all day (with an offset).

1 Like

I will try that, tnx :slight_smile:

I can not get this automation to start before the sun is below horizon. I have tried to add time before, but nothing happens.

- id: auto444
  alias: Lys på når jeg kommer hjem og mørkt
  initial_state: True
  hide_entity: False
  trigger:
    platform: state
#    entity_id: device_tracker.olsen_iphone
    entity_id: group.tracker_group_borge
    from: 'not_home'
    to: 'home'
  condition:
    condition: or
    conditions:
      - condition: sun
        before: sunrise
        before_offset: "-01:00:00"  
      - condition: sun
        after: sunset
  action:
    - service: scene.turn_on
      entity_id: scene.stuekjokkengang_medium
    - service: notify.ios_olsen
      data:
        title: "Casa de Olsen:"
        message: "Velkommen hjem! Lys slått på!"

I think this sets it to 1 hour after

Try

before_offset: "01:00:00"

And then for sunset you would want the negative

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

To make it one hour before sunset.

1 Like

thank you! that worked :smile:

1 Like