Looking to create an automation that will turn kitchen pot lights 1 and 2 between 1630 to 2300 and between 0715 to 1100 only while phone1 or phone 2 is home. If during that time both phones are away, the lights will turn off, if either returns, they’ll turn back on.
Here’s what I’ve come up with so far and it doesn’t seem to work:
alias: Kitchen pot lights
description: >-
Turns on two lights between 16:00-24:00 and 07:15-10:30 if either of the
devices is home.
trigger:
- platform: time
at: "16:00:00"
- platform: time
at: "07:15:00"
condition:
- condition: time
after: "07:15:00"
before: "10:30:00"
- condition: time
after: "16:00:00"
before: "23:59:00"
- condition: or
conditions:
- condition: state
entity_id: device_tracker.phone1
state: home
- condition: state
entity_id: device_tracker.phone2
state: home
action:
- service: light.turn_on
data: {}
target:
entity_id:
- light.kitchen_pot_light_1
- light.kitchen_pot_light_2
mode: single
I feel like i have to make a template that includes an If statement that tests the time every time either of the devices’ status changes to ‘home’, and if it falls within the time frame, turn the lights on, else turn them off. But I can’t seem to figure out how to put that together.
If what you have here works (which it seems to do) then I think we can do a pretty simple fix.
What you have in condition:, cut that.
Then you add a choose in your action and paste this you just cut (mind the indentation).
As action you add the light.turn_on as you have it above.
Then the default: is light.turn_off
Thank you both for your replies. I added the conditions as a “choose” like Hellis suggested, and given what Sir_goodenough mentioned, i changed the triggers to when phone1 and phone2 change state. Here’s the result:
alias: Kitchen pot lights on
description: >-
Turns on two lights between 16:00-24:00 and 07:15-10:30 if either of the
devices is home.
trigger:
- platform: state
entity_id:
- device_tracker.phone1
from: null
- platform: state
entity_id:
- device_tracker.phone2
from: null
action:
- choose:
- conditions:
- condition: time
after: "07:15:00"
before: "10:30:00"
sequence:
- service: light.turn_on
metadata: {}
data: {}
target:
entity_id:
- light.kitchen_pot_light_1
- light.kitchen_pot_light_2
- conditions:
- condition: time
after: "16:00:00"
before: "23:59:00"
sequence:
- service: light.turn_on
metadata: {}
data: {}
target:
entity_id:
- light.kitchen_pot_light_1
- light.kitchen_pot_light_2
- conditions:
- condition: or
conditions:
- condition: state
entity_id: device_tracker.phone1
state: home
- condition: state
entity_id: device_tracker.phone2
state: home
sequence:
- service: light.turn_on
metadata: {}
data: {}
target:
entity_id:
- light.kitchen_pot_light_1
- light.kitchen_pot_light_2
default:
- service: light.turn_off
metadata: {}
data: {}
target:
entity_id:
- light.kitchen_pot_light_1
- light.kitchen_pot_light_2
mode: single
I’m not getting any errors on save. I’ve just run out of time, i should be able to test this sometime this week and will report back.
Time triggers are slightly harder to test but device trackers can be set to not_home in developer tools → states.
That is a good way to test automations.
And great job on the automation!
Looks really good.
You can or the two time conditions making one section disappear.
And you could even or it with the device tracker turn on part.
It doesn’t make much difference more than if you one day want to add more lights to it, then it’s nice to not have to repeat the same thing again.
But this should work fine.
I just did a quick test and it didn’t work. The lights turned on even thou we’re outside of the time frame. I think i have to add the “OR” condition of the devices being home within both time conditions and not separately. I’ll do it soon as I’m able and will report.
I just noticed this.
Remove that.
Device trackers can’t have null as state.
Leaving it empty will mean it triggers on everything.
Don’t change the or part you just said.
That should work.
trigger:
- platform: state
entity_id:
- device_tracker.phone1
- device_tracker.phone2
- platform: time
at: "16:00:00"
- platform: time
at: "07:15:00"
I believe i have figured this out. I’ve tested it by changing the times and attributes and so far it’s all gone through well. Here it is for anyone that may be interested in the future. I’ve also cleaned up the yaml to the best of my abilities - bear in my mind I’m not a programmer.
alias: Kitchen pot lights on test
description:
Turns on two lights between 16:00-24:00 and 07:15-10:30 if either of the
devices is home.
trigger:
- platform: state
entity_id:
- device_tracker.phone1
- device_tracker.phone2
- platform: time
at: "16:00:01"
- platform: time
at: "07:15:01"
action:
- choose:
- conditions:
- condition: or
conditions:
- condition: time
after: "07:15:00"
before: "10:30:00"
- condition: time
after: "16:00:00"
before: "23:59:00"
- condition: and
conditions:
- condition: or
conditions:
- condition: state
entity_id: device_tracker.phone1
state: home
- condition: state
entity_id: device_tracker.phone2
state: home
sequence:
- service: light.turn_on
metadata: {}
data: {}
target:
entity_id:
- light.kitchen_pot_light_1
- light.kitchen_pot_light_2
default:
- service: light.turn_off
metadata: {}
data: {}
target:
entity_id:
- light.kitchen_pot_light_1
- light.kitchen_pot_light_2
mode: single
For anyone wondering, the way i was able to get the status of the phones is by using the UniFi network integration with HA - I assume you may be able to do the same even if you don’t have a UniFi system, as long as there’s an integration for your router.
Keep in mind that some phones might disconnect/stop responding to router pinging while in sleep.
You could also install the HA app and it will give you lots of sensors.
But I believe I have heard that unifi integration somehow is better than a standard ping.
Another issue I have heard about before is when you time trigger and have conditions that the condition blocks the trigger if they are both at the same second.
So it could be better to trigger at “07:15:01”.
It’s not everyone that has this issue but I have heard of it.