Can't figure out what's wrong with my automation rule.Please have a look

I decided to start with split configuration files,and the first thing I tried was sun triggered switch.It worked perfect.I tried to combine it with presence detection and it’s not working at all.
Here is my automation rule:

alias: Turn the lights on when home in the evening
trigger:
  platform: sun
  event: sunset
  offset: "-01:00:00"
condition:
  condition: state
  entity_id: group.all_devices
  state: home
action:
  service: homeassistant.turn_on
  entity_id: group.living_room, group.kitchen

No errors at all,but it’s not working.
When I trigger it from the automation frontend is OK

A few things…

First can you post your configuration using the </> button? It allows the forum to maintain your spacing and can show if there is a error in the way you have things indented.

Is anyone not home? I think with the way you have it setup everyone would have to be home. So if anyone from the group.all_devices isn’t home it won’t trigger. Also if that group contains any computers or other devices and they aren’t powered on it won’t trigger. You may want to narrow that down to just a few select entities.

Thanks for the edit,i tried several times to edit the post with no result.When I press </> button it says indent preformatted text by 4 spaces and only the first line looks in preview like it should to be.
I replaced group.all_devices with device_tracker.mitko which is my device.
How to check the rule before next sunset.Maybe I’ve got to set the time several hours back in “states” section?

This was the first thing I spotted too.

@serengeti76: the easy way to post code is to copy from your source and paste into the editor. Next, highlight everything you just posted and THEN press the code formatting button.

alias: Turn the lights on when home in the evening
trigger:
  platform: sun
  event: sunset
  offset: "-01:00:00"
condition:
  condition: state
  entity_id: device_tracker.mitko
  state: home
action:
  service: homeassistant.turn_on
  entity_id: group.living_room, group.kitchen

here is the new file,thanks for the tip.
I created a new rule which contains group.all_devices

 alias: Send email when Family is home.
 trigger:
     platform: state
     entity_id: group.all_devices 
     to: 'home'
 action:
   service: notify.***********_*********
   data:
     message: 'Family is home'

and is working fine,sends email when my pone connects to the home wi-fi network.
Now I see that there is difference between two files,one can’t tell before see them together.I’ll try with the new formatting

  alias: Turn the lights on when home in the evening
  trigger:
       platform: sun
       event: sunset
       offset: "-01:00:00"
  condition:
    condition: state
    entity_id: device_tracker.mitko
    state: home
  action:
    service: homeassistant.turn_on
    entity_id: group.living_room, group.kitchen

Yep now is working :slight_smile: I’m happy with the result.Thanks for your help @silvrr and @rpitera.At least I know how to post my .yaml files :blush:
My question is will that automation work when I arrive home after sunset or in this “offset” time period.

Since your trigger is one hour before sunset with a condition that you are home I think it will only go off at that time.

If you want a automation to turn the lights off when you get home I think you need to have the opposite. Trigger that your state goes from away to home and a condition that the sun is below a certain angle in the sky.

I had this and it was working really well but I’m having problems with my harmony-api that I have to figure out now.

But up until that point, it used to trigger 45 minutes before OR anytime after sunset.

# MQTT/Harmony
- alias: 'Theater Lights'
  trigger:
    - platform: state
      entity_id: switch.watch_tv
      state: 'on'
    - platform: state
      entity_id: switch.firetvplex
      state: 'on'
    - platform: state
      entity_id: switch.chromecast
      state: 'on'
    - platform: state
      entity_id: switch.bluray
      state: 'on'
  condition:
    condition: sun
    after: sunset
    after_offset: "-00:45:00"
  action:
    - service: scene.turn_on
      entity_id: scene.sunset
    - delay: 00:01:30
    - service: scene.turn_on
      entity_id: scene.sunset_dimmed

The effect was that if I triggered any activity on the Harmony Hub, it would bring the lights up to full, wait a minute and a half and then slowly fade down.

Actually that’s not quite right; group.all_devices is special, it will return ‘home’ if any of the included devices is ‘home’. I use it this way extensively.

1 Like

Ahh, good to know. Thanks!

  alias: Turn the lights off when not home
  trigger:
       platform: state
       entity_id: device_tracker.mitko
       state: not_home
  action:
    service: homeassistant.turn_off
    entity_id: group.living_room, group.kitchen

Plus that rule:

  alias: Turn the lights on when home in the evening
  trigger:
       platform: state
       entity_id: device_tracker.mitko
       state: home
  condition:
    condition: sun
    after: sunset
    after_offset: "-00:45:00"
  action:
    service: homeassistant.turn_on
    entity_id: group.living_room, group.kitchen

Now sky is the limit! :slight_smile:
Thanks for your help,I was stuck with this for a week

For some reason,when I enable web managemet of my Kodi Player at port 81 this automation stops working.
I’ll try it few more days just to be sure.

Don’t forget that the Kodi component has a port attribute to set:

- platform: kodi
    host: http://192.168.1.123
    port: 81
    name: Kodi
    username: xbmc
    password: abcd
    turn_off_action: quit

Yep,my media player config is the same.

Just an FYI: group.all_devices will set it’s state home if even one member is home, so it is actually possible to use that as the entity ID that triggers the action.