Lights automation and device tracker

I am quite happy with the way I have my lights automated at the moment. And now I have a reasonable stable device tracker, so my next step would to switch off all lights when no one is at home. The only thing that stops me from doing that, is when someone comes back home, I want to have the lights switched on as it would have been if no one would have left. And I have no idea how I would implement that.

Probably all the lights automation rules should have some logic in there looking at that device tracker. So that as soon as the rule is triggered it can do so only of someone is at home, or it triggers if someone just got home. This will make all the rules much more complex than they already are. Is there not a more simple way of doing this?

I would like to have some kind of layer in between that enables the lights based on the device tracker. The lighting automation rules will continue to work as usual, and this extra layer will allow the lights to really switch on or off depending on the device tracker. Would that be easy to implement?

- id: all_lights_off_when_gone
  alias: Lights Off when gone
  trigger:
    platform: state
    entity_id: group.mom_and_dads_phones
    from: 'home'
    to: 'not_home'
  action:
  - service: homeassistant.turn_off
    entity_id: group.leaving_lights
  - service: climate.turn_off
    entity_id: climate.radio_thermostat_company_of_america_ct101_thermostat_iris_heating_1
  - service: notify.notifier_name
    data_template:
      message: "Nobody's Home"
  - service: remote.turn_on
    data:
      entity_id: remote.harmony_hub_2
      activity:  -1 # PowerOff
  - service: remote.turn_on
    data:
      entity_id: remote.harmony_hub
      activity:  -1 # PowerOff
- id: lights_on_when_home
  alias: Lights on When Home
  trigger:
    platform: state
    entity_id: group.mom_and_dads_phones
    from: 'not_home'
    to: 'home'
  action:
  - service: homeassistant.turn_on
    entity_id: group.common_area_lights
  - service: notify.notifier_name
    data_template:
      message: "Somebody's Home"
  - service: climate.set_temperature
    data:
      entity_id: climate.radio_thermostat_company_of_america_ct101_thermostat_iris_heating_1
      temperature: 68
  - service: climate.set_hvac_mode
    data:
      entity_id: climate.radio_thermostat_company_of_america_ct101_thermostat_iris_heating_1
      hvac_mode: heat

You have to make a group of device trackers for you and whoever else stays at your home.

The issue is different, let me try to explain with an simplified example. In my house a lamp will switch on at 7PM each day. Now with the device trackers, if nobody was at home at 7PM the light will not be switched on. Now someone gets home a few minutes later. This light was supposed to be on but is off. How do I make sure that the lights that are supposed to be on, will switch on when someone gets home?

Maybe an analogy with heating will help. If you have a programmable thermostat you can switch it to away mode. And all rooms will go to a predefined state. When you cancel the away mode, the thermostat will follow the original schedule again. This is what I am looking for my lighting now too.

I’m not so familiar with it, but maybe schedy can be used for this.

I understand why you mention Schedy, might work yes, however than I have to port all my lighting rules to Schedy which is not really something I am going to do. I think that Schedy could not even do everything I have done in HA automation rules.

i think you are overthinking this?

- alias: turn on lights when i get home
  trigger:
  - entity_id: person.you
    platform: state
    to: home
  condition:
  - condition: state
    entity_id: light.your_light
    state: 'off'
  - condition: or
    conditions:
    - condition: state
      entity_id: light.another_light
      state: 'off'
  action:
  - data:
      entity_id: light.your_light, light.another_light
    service: light.turn_on

Didnt test, but should work.

How many automations are we talking about? Could you please show me some examples? I have the feeling that you would need to rewrite all of your automations and it could get quite complicated, maybe switching to appdaemon (not talking about schedy here) would be the best option.

Here an example where I tried to add the device tracker:

- alias: Woonkamerlamp aan als zon onder gaat
  trigger:
  - platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: 3.5
  - platform: time
    at: '19:01:00'
  condition:
    condition: and
    conditions:
    - condition: time
      after: '19:00:00'
    - condition: numeric_state
      entity_id: sun.sun
      value_template: '{{ state.attributes.elevation }}'
      below: 3.5
    - condition: state
      entity_id: group.noreenpaul
      state: 'home'
  action:
  - service: light.turn_on
    data:
      entity_id: light.woonkamer_lamp
      brightness_pct: 30
      kelvin: 2200
  - service: light.turn_on
    data:
      entity_id: light.ensuite_deur
      brightness_pct: 1
      kelvin: 2200

This automation only triggers when the sun is very low and after 7PM. And I have added the group tracker. Or should I add the group to the triggers instead of the condition section?

I’m on holidays now but will take a deeper look when I’m back at home.

You need the device tracker as a trigger AND as a condition, the same way you did it for the other two triggers.

I do have something similar see if this fits the bill.
(I only do this for speakers not lights, lights generally give the appearance of the house being occupied though I’m yet to create my random (but utterly believable) light switch on / switch off system to keep the nasty men from my door)
I have speakers that have two time slots morning and afternoon (though that’s not hard and fast) they are set by input datetime’s in the interface, I can also switch them on with a timer to go off in (say) 60mins (again configurable) or I can switch them on manually.
I have binary sensors to determine if the speaker has been instructed to turn on.
This stays on all the ‘period set’ but this is not the control for the speaker unless the house is occupied. As if I leave the speaker will turn off. The speaker slot may expire whilst I’m out but if not, when I come back the speaker is switched back on

2 Likes

There are several ways to accomplish this but here’s how I would do it.

  • You need one automation that turns group.all_lights off when no one is home (this is an automatically created group that should already exist). Or include this command in your existing “Elvis Has Left The Building” automation.
- service: light_turn_off
  entity_id: group.all_lights

Example

light:
  - platform: group
    name: Arrive Home Lights
    entities:
      - light.entrance_light
      - light.kitchen_light
  • Then create an automation that turns the group you created above on when someone comes home. In this automation you can put a condition for the sun being down or something similar so they don’t turn on when it’s light outside. You shouldn’t have to change any of you other lighting automations. This one can work independently. I also wouldn’t put this in your “Elvis Has Arrived” automation because you don’t want the daylight condition to stop the rest of the actions from happening.
- service: light_turn_on
  entity_id: light.arrive_home_lights

For your daylight condition can use a simple sun condition or you can get fancier using sun elevations. Fancier yet would be the illuminance custom component.

1 Like

I like Mutt’s idea. Create a binary sensor for each lamp and set binary sensor to true instead of switching on the lamp. And have another automation that triggers based on the binary sensor state and the device tracker to actually switch the light. Probably some vacation mode can be added to it that will override the device tracker in case you want to pretend that someone is at home.

The only thing missing is a way to set brightness. Color temperature will be set by Flux. But brightness might need another variable to store the value in. Are template variables global?

Jason’s solution is a LOT easier to accomplish and may be sufficient (why would you want to store the binary sum of 20 odd lights?

I have light level and colour pattern settings (which all lights adopt when they switch on or light that are on transition to when the segment changes) so I have no need to store these values.
But I just use input_number ‘s to store the patterns and input datetime’ s to store the transition times

And no. Template variables only live during that template evaluation

2 Likes

If you want to get all fancy and whatnot storing brightness and colors and all that I’d consider an arrive home scene instead of a light group. The you can also use it for anything else you want to change when someone comes home. @phsdv didn’t really say anything about any of that so I didn’t take it into consideration.

Needless to say there are several ways to skin this cat!

I’d avoid scenes if at all possible, they are being deprecated as we speak (seemingly anyway, less and less capable with each new release) easier to just to do it in scripts

Curious what makes you say this? At one point I would have agreed with you, scenes were terribly clunky. I didn’t use them at all. But things have improved!

They actually just created new capabilities such as the scene.create service so you can create scenes on the fly. I’m using it in a few places now and it works great! They also just introduced a scene editor for the UI but I haven’t tried that yet. That being said I don’t use them for anything but lighting so far, but they seem to work OK for that.

1 Like

I’ll look for the other thread about moving them to scripts to future proof them, I think it was about no longer being able to use transitions in scenes.
Let me look

They can’t all be scrapped, cos as you point out, they’ve just done an editor

As far as I understand they removed the transition and the ability to call a script from scenes and they should be more seen as a desired state for multiple entities now.

2 Likes