Struggling with turning lights on automation

This probably seems like something that should be very easy but for some reason I can’t figure it out. I’ve been trying to automatically turn on the lights when coming home but so far nothing I’ve tried worked and hoped someone here could help me.

A Home Assistant docker container runs on my server which is always on and uses swag for a reverse proxy which works as I can control things without wifi. My phone has the app and I’ve enabled location sensors (not high accuracy mode), geolocation is enabled as well but the state is unknown, don’t know why, it’s a OnePlus 7t pro phone, the entity for this is HD1913. Also have an Asus router integration, which shows my phone entity as OnePlus-7TPro. Added both these entities to “track device” under people/me. And I’ve set the zone for home to 250 meters in the configuration.yaml. The map in the Home Assistant app correctly shows where I’m at when not home.

Here are screenshots of the automations I use, tried it multiple ways in the hope one would work: Imgur: The magic of the Internet

They all work when triggering them manually but they’ve never worked automatically. The logbook for these entities does seem to know when I come home… If anyone has any idea of what could be going wrong that would be great and very helpful.

Once I’ve removed the sun part when coming home during the day and then they did work, I was however using my GPS as well. I think perhaps this happens because my phone goes to sleep and then doesn’t update my location in Home Assistant, however then it is strange that the logbook does show me coming home correctly.

For example this weekend I came home from a concert and told Google Home to turn on my living room lights at 4:45 (meaning I was already in my house at this point). The HA logbook entity for my phone, HD1913, shows me coming home at 4:42 and 56 seconds, so it would have a good two minutes to turn on my lights but it didn’t. My OnePlus-7TPro entity does however show me coming home at 4:47 which makes sense as it uses wifi.

Just now looked at debug automation and it says this:


So it seems the sun part is failing for some reason, sunrise was at 5:30 that day according to the logbook.

Any help on getting this to work would be greatly appreciated.

Welcome to the forums Eisoniq!

HA handles this situation slightly different, see the documentation details:

Note that if only before key is used, the condition will be true from midnight until sunrise/sunset. If only after key is used, the condition will be true from sunset/sunrise until midnight . Therefore, to cover time between sunset and sunrise one need to use after: sunset and before: sunrise as 2 separate conditions and combine them using or .


condition:
  condition: or
  conditions:
    - condition: sun
      after: sunset
    - condition: sun
      before: sunrise

https://www.home-assistant.io/docs/scripts/conditions/#sunsetsunrise-condition

Ahh, thank you very much, took me a bit to wrap my head around it properly, at first I thought I needed to add the same thing twice, didn’t occur to me to leave one before/after option blank, but I understand it now. It has to be like this:

I should have pointed that out, then it would have been clearer. You can switch to edit mode in the Automation Editor where you can paste the code snippet.

In accordance with forum customs, please consider no. 11 and no. 19 of the guidelines:

formatting code