Trouble with light automation

I am trying to have the lights turn on, when I get home and it is dark, but it doesn’t work when I add both conditions (sunset, sunrise). It does work with a single condition, atm this is “before sunrise”.

trigger:
  - entity_id: device_tracker.huawei_mate_20_pro
    from: not_home
    platform: state
    to: home
  condition:
  - after: sunset
    after_offset: -01:00:00
    before: sunrise
    before_offset: +01:00:00
    condition: sun
  action:
  - service: light.turn_on

I tried reversing conditions, sunrise before sunset but it doesn’t work either.
What am I doing wrong?

It cannot be both after sunset and before sunrise in the same day, you need two conditions, one for after sunset and one for before sunrise, nested under an OR condition.

1 Like

I tried , but I get an error “Unsupported condition: or”

condition:
    condition: or  # 'when dark' condition: either after sunset or before sunrise - equivalent to a state condition on `sun.sun` of `below_horizon`
    conditions:
      - condition: sun
        after: sunset
        after_offset: "-01:00:00"
      - condition: sun
        before: sunrise
        before_offset: "+01:00:00"

I have not seen after_offset or before_offset anywhere… should be just offset I believe? Also it won’t span across midnight as mf_social said…

I used this for a reference:

Never used that… only used offset… the sign sorts out if it’s before or after…

Sounds logical and apparently works too, but doesn’t help with the error I get - “Unsupported condition: or”

You’re trying to use the automation editor, aren’t you?

The OR condition works fine, it is just not supported by the automation editor because it was introduced as a ‘work in progress’ a long long time ago and then forgotten about.

You should see the automation editor as a way to try out some basic automations, and then move to coding your own as soon as you can.

3 Likes

I thought I needed to check what I code with the editor.
I am a beginner, obviously, so I start with the editor to create an automation, then edit/code it further.
I’ll see if it works in the evening, thanks for the help.

1 Like

Alternatively you could use the elevation attibute. You find some examples here.

1 Like

Still doesn’t work… Does HA have a troubleshooting and testing for automation rules? Logs or something so I could see what’s wrong.

Config check found an error, but I don’t understand what it means. Invalid config for automation:

Got None 
extra keys not allowed @ data['condition'][0]['conditions'][0]['offset']. 
Got None     
not a valid value for dictionary value @ data['condition'][0]['conditions'][0]['condition']. 
Got None    
required key not provided @ data['condition'][0]['conditions'][0]['entity_id']. 
Got None. (See /config/configuration.yaml, line 104). 
Please check the docs at https://home-assistant.io/components/automation/ ```

Please post the entire automation with formatting preserved.

- id: '1547866606884'
  alias: Ugasni luči ko odidem ali vzide sonce.
  trigger:
  - entity_id: device_tracker.huawei_mate_20_pro
    from: home
    platform: state
    to: not_home
  - event: sunrise
    platform: sun
  condition: []
  action:
  - data: {}
    service: light.turn_off

- id: '1547865918090'
  alias: Prižgi luči, kot pridem domov.
  trigger:
  - entity_id: device_tracker.huawei_mate_20_pro
    from: not_home
    platform: state
    to: home
  condition:
    condition: or  # 'when dark' condition: either after sunset or before sunrise - equivalent to a state condition on `sun.sun` of `below_horizon`
    conditions:
    - condition: sun
      after: sunset
      offset: "-02:00:00"
    - condition: sun
      before: sunrise
      offset: "+01:00:00"
  action:
  - service: light.turn_on
- id: '1547865918090'
  alias: Prižgi luči, kot pridem domov.
  trigger:
  - entity_id: device_tracker.huawei_mate_20_pro
    from: not_home
    platform: state
    to: home
  condition:
    condition: or
    conditions:
    - condition: sun
      after: sunset
      after_offset: "-02:00:00"
    - condition: sun
      before: sunrise
      before_offset: "+01:00:00"
  action:
  - service: light.turn_on
    entity_id: ID OF LIGHT

Don’t forget to add the entity id to the bottom, and also to the one above it.

Both have an id, the top one wasn’t showing due to my copy&paste error; should be showing correctly now.

No, the entity id of the light(s) you want to turn on.

Ah, sorry, I didn’t enter any entities, because for the moment I just want all the lights to turn on and off.

That facility has been removed because it causes too many problems, if you want all your lights to come on you need

entity_id: group.all_lights
1 Like

Ok, added entity in both automation rules. Didn’t change anything regarding functionality or error. Light do turn off as per rule, but they don’t turn on with the 2 conditions.