Automation - Sunset with Device Tracker

Having some trouble getting my living room lamp to turn on 30 minutes from sunset when my wife and I are home. I am using trigger to test from the front end. Every time the light turns on but the system sees that one of us is away (not_home).

Automation.yaml:

- alias: Living Room Lamp - Sunset
   trigger:
      platform: sun
      event: sunset
      offset: "-00:30:00"
    condition:
       - condition: state
         entity_id: 'device_tracker.XXXXXX'
         state: 'home'
       - condition: state
         entity_id: 'device_tracker.XXXXXX'
         state: 'home'
    action:
      service: homeassistant.turn_on
      entity_id: switch.switch

Entity:

Finally!!
I can try and give something back to the community.
I am definitely no expert by any means but this works for me:

- alias: Dining room light on at dusk if someone is home
  trigger:
    - platform: template
      value_template: '{{states.group.family_presence.state == "home"}}'
    - platform: numeric_state
      entity_id: sun.sun
      value_template: '{{ state.attributes.elevation }}'
      below: 5.0
  condition: 
    condition: and
    conditions:
    - condition: state
      entity_id: group.family_presence
      state: "home"
    - condition: numeric_state
      entity_id: sun.sun
      value_template: '{{ state.attributes.elevation }}'
      below: 5.0
    - condition: time
      before: "23:00:00"
  action:
    service: homeassistant.turn_on
    entity_id: switch.dining_room_light

I use sun elevation rather than sunset as I read that it works better with the varying seasons. I have yet to establish that as I have only had this up and running for a few days.

Iā€™m happy to answer any questions but there are a LOT more knowledgeable people out there (Iā€™m expecting to be told there is a better way to do what I am doing!)

1 Like

Some pointers in addition to klogg example. Or maybe more explanation how it works :slight_smile:

As in klogg post, you could change the condition to work with a group. Make a group with all the devices e.g.

family_devices:
  name: Family
  entities:
  - device_tracker.XXX
  - device_tracker.YYY

If one of the entities in the group evaluates ā€œhomeā€ the group itself evaluates ā€œhomeā€.

Or you could use ā€œorā€ condition instead of ā€œandā€ which is the default if you dont specify. it would look like below code. With a group you need to change the entity ID to ā€œgroup.family_devicesā€. And have only one condition of course.

  condition:
    - condition: or
      conditions:
      - condition: state
        entity_id: device_tracker.XXXXXX
        state: 'home'
      - condition: state
        entity_id: device_tracker.XXXXXX
        state: 'home'

Moving to trigger:
Currently your lamp turns on 30 min before sunset if you are home. But it will not turn on after that point if you were not at home. Like in klogg example, you could add another trigger for this.

Mine is a bit different compared to above post but both work just fine.

  - platform: state
    entity_id: group.family_devices
    to: 'home'

It would look something like below code.

- alias: Living Room Lamp - Sunset
  trigger:
  - platform: sun
    event: sunset
    offset: '-00:30:00'
  - platform: state
    entity_id: group.family_devices
    to: 'home'

If you add all persons separately as triggers then I would add one more condition to check if the lamp if already on. The automation would trigger separately when all persons are coming to home and the lamp would go on multiple times. Not necessary any kind of problem but personally I donā€™t want to do it so :slight_smile:

@klogg Canā€™t think of anything to do better. You have already taken into consideration all my points. Can only think of different ways but not really better ways. But then again, Iā€™m more of hardware guy myself :wink:

1 Like

When you trigger from the front end the condition is ignored and the action is executed.

Remove the quotes from around the entity_ids in your conditions.

And, Iā€™m just checking that you only want this switch on if youā€™re both home? Thatā€™s what youā€™ve coded. Youā€™ll need either an OR condition or a group if you want it to come on if either of you are home.

Other than that the code is fine. And will trigger at sunset minus 30 minutes every day, and if youā€™re both home turn the switch on :+1:

1 Like

Thanks all! I did not realize trigger will ignore the condition. Is there a way to test this completely?

For now, I was just testing my first automation so I wanted to see if I was getting the conditions right before setting up as ā€œeither one of us homeā€.

Final Code which says ā€œIf either one of us are home (or come home), turn on lamp 30 minutes before sunsetā€:

- alias: Living Room Lamp - Sunset
  trigger:
  - platform: sun
    event: sunset
    offset: "-00:30:00"
  - platform: state
    entity_id: group.family_devices
    to: 'home'
  action:
    service: homeassistant.turn_on
    entity_id: switch.switch

This community is awesome!

That doesnā€™t say ā€˜is home or comes homeā€™. It just says ā€˜comes homeā€™.

If you want that, youā€™ll need to condition on arrival home too.

Edit - in fact, that automation doesnā€™t do anything like what you want.

ā€œIf either one of us are home (or come home), turn on lamp 30 minutes before sunsetā€:

- alias: Living Room Lamp - Sunset
  trigger:
    - platform: sun
      event: sunset
      offset: "-00:30:00"
    - platform: state
      entity_id: group.family_devices
      to: 'home'
  condition:
    - condition: state
      entity_id: group.family_devices
      state: 'home'
    - condition: sun 
      after: sunset 
      after_offset: "-00:30:00"
  action:
    service: homeassistant.turn_on
    entity_id: switch.switch

Is a lot closer, if not exactly what you want.

1 Like

Ahh, I was missing conditions. Maybe @taikapanu didnā€™t include conditions in his trigger code.

Basically I only want the light to turn on 30 minutes before sunset when either one of us are home -OR- if we come home after 30 minutes before sunset.

Thanks!

1 Like

I should have maybe pieced my post in better wording and not like it is :grin:

Hereā€™s my code in full format. I have additional of one minute delay so my wife canā€™t accuse me of having the light on without someone being home :smile:

- alias: Triangle lamp (on)
  trigger:
  - platform: sun
    event: sunset
  - platform: state
    entity_id: group.family_devices
    to: 'home'
    for:
      minutes: 1
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: group.family_devices
      state: home
    - condition: time
      after: '17:00:00'
      before: '23:00:00'
    - condition: state
      entity_id: switch.sonoff1
      state: 'off'
  action:
    service: homeassistant.turn_on
    entity_id: switch.sonoff1

Excellent! Just so I understand correctly, this says turn Triangle lamp on at sunset if someone is home for 1 minute (or comes home) between the hours of 5pm and 11pm?

Not quite but almost. Automation is always triggered if any of the triggers evaluate true. If conditions evaluate true then the action is executed.

So in this case:
IF sunset OR someone has been home for one minute or longer
=> check if conditions evaluate TRUE

Because of the AND all mentioned conditions need to be true

  1. someone needs to be home (family_devices)
  2. time needs to be between 5pm and 11pm
  3. the lamp needs to be off (switch.sonoff1)

=> if all TRUE execute the action

I have then another automation to turn off devices when people leave the house, itā€™s getting bright inside and some other.

#--------------------------------------------------------------
#
# Turn off all devices when everyone has left the house.
# Dog is not counted.
#  
#--------------------------------------------------------------

- alias: Leaving home
  trigger:
  - platform: state
    entity_id: group.family_devices
    to: not_home
  - platform: state
    entity_id: group.relative_devices
    to: not_home
  condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: group.family_devices
        state: 'not_home'
      - condition: state
        entity_id: group.relative_devices
        state: 'not_home'
  action:
  - service: script.turn_on
    entity_id: script.turn_off_all_devices

And below the script that is calling. I have it like this because I reuse that in few other places.

turn_off_all_devices:
  alias: Turn off all devices
  sequence:
  - alias: turn off (kitchen)
    service: homeassistant.turn_off
    entity_id: group.kitchen_lights
  - alias: turn off (living room)
    service: homeassistant.turn_off
    entity_id: group.living_room_lights
  - alias: turn off (decorative lights)
    service: homeassistant.turn_off
    entity_id: group.common_decorative_light
  - alias: turn off (all bedroom lights)
    service: homeassistant.turn_off
    entity_id: group.all_bedroom_lights
  - alias: pause (spotify)
    service: media_player.media_pause
    entity_id: media_player.spotify
  - alias: turn off (hallway)
    service: homeassistant.turn_off
    entity_id: switch.hallway
  - alias: turn off (bedroom)
    service: homeassistant.turn_off
    entity_id: switch.bedroom

Just as a point, that last script would be much neater and easier like thisā€¦

turn_off_all_devices:
  alias: Turn off all devices
  sequence:
    - service: homeassistant.turn_off
      entity_id:
        - group.kitchen_lights
        - group.living_room_lights
        - group.common_decorative_light
        - group.all_bedroom_lights
        - switch.hallway
        - switch.bedroom
    - alias: pause (spotify)
      service: media_player.media_pause
      entity_id: media_player.spotify
1 Like

Thanks for the tip! Iā€™ll put that in!

1 Like

No worries :+1:

I canā€™t seem to get my head around this :confused:

I want to turn the light on 30 minutes before sunset if one/both of us is home. If one/both of us comes home (both being away) after sunset, turn on the light.

Current code:

- alias: Living Room Lamp - Sunset
  trigger:
    - platform: sun
      event: sunset
      offset: "-00:30:00"
    - platform: state
      entity_id: group.family_devices
      to: 'home'
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: group.family_devices
      state: 'home'
    - condition: sun 
      after: sunset
  action:
    service: homeassistant.turn_on
    entity_id: switch.switch

Is your light actually called switch.switch?

Other than that, presuming your time zone is correct, the code I posted above will do what you want.

Yes it is. I havenā€™t renamed them yet in the entity_registry.yaml.

Thank you kindly. I wish I could test this that took the entire automation into account.

You just have to be inventive with your tests :wink:

Presuming it is past half-an-hour to sunset where you are, set your device trackers to away using the device tracker ā€˜seeā€™ service. Then so far as your system is concerned, youā€™re out.

Then push an update from your device trackers, your system thinks youā€™ve come home and runs the automation. :+1:

Also if you trigger 30 min before sunset and have condition at exact time of sunset it will always evaluate false.

See here for fix as in mf_social example

I have used 1 minute safety margins in conditions. E.g. setting the offset to -31 minutes to be sure it evaluates true when triggering at -30min.

So this?:

- alias: Living Room Lamp - Sunset
  trigger:
    - platform: sun
      event: sunset
      offset: "-00:30:00"
    - platform: state
      entity_id: group.family_devices
      to: 'home'
  condition:
    - condition: state
      entity_id: group.family_devices
      state: 'home'
    - condition: sun 
      after: sunset 
      after_offset: "-00:31:00"
  action:
    service: homeassistant.turn_on
    entity_id: switch.plugin_1