Lights off when I leave home

Dear forum members,

First, what a great system is home assistant.

Now my question, I want the lights to go out when I leave the house, now I have googled and tried different options, unfortunately it does not work. I don’t get an error message and the automation is neat on the homepage when I go to / states.

I will put the code below:

automation 19:
  alias: Turn All Lights off When We Leave
  trigger:
    entity_id: device_tracker.gerritjan
    from: home
    platform: state
    to: away
  action:
     entity_id: group.switches
     service: switch.turn_off

You say “we” - if you have multiple persons in the house I suggest you create a template binary_sensor for the house so that it will have a separate “occupancy” point indicating if anyone is in the house or not.
It will also allow you to trigger lights on when first person arrive and off when the last person leaves.

But for only one person, try this:

  alias: Turn All Lights off When We Leave
  trigger:
  - entity_id: device_tracker.gerritjan
    platform: state
    from: home
    to: not_home
  condition: []
  action:
  - entity_id: group.switches
    service: switch.turn_off

Change away to not_home.

2 Likes

Of course! :slight_smile:

Gentlemen,

thanks for your reply!

Forschi, unfortunately your possible solution does not work, I have turned off the wifi but the lamp stays on when hass says that I am ‘not home’.

Did device_tracker.gerritjan go to not_home and nothing happened? Do you see anything in the logs?

Yes, that has happened, but unfortunately nothing is happening, by the way, it says ‘absent’ and ‘home’ in Dutch.

If you go to the sidebar and then to Developer Tools -> States, it says “home” and “not_home”, no matter what language your system is. “Absent” and “home” will only be shown in the frontend and will be based on the language you configured for the user.
Do you see any errors under Developer Tools -> Logs?

The following was the last one, this was before I asked you for help and before I added the above automation. There are no new errors or the like.

2019-11-17 13:33:06 ERROR (MainThread) [homeassistant.components.hassio] Invalid config for [automation]: expected dict for dictionary value @ data[‘action’][0][‘data’]. Got None
extra keys not allowed @ data[‘entity_id’]. Got None
extra keys not allowed @ data[‘service’]. Got None. (See /config/configuration.yaml, line 87). Please check the docs at https://home-assistant.io/integrations/automation/
2019-11-17 13:34:06 ERROR (MainThread) [homeassistant.components.hassio] Invalid config for [automation]: [data] is an invalid option for [automation]. Check: automation->action->0->data. (See /config/configuration.yaml, line 87). Please check the docs at https://home-assistant.io/integrations/automation/

If you go to “States” and find your automation, did it trigger?
It has a “last_triggered” attribute

No

Maybe this?

  alias: Turn All Lights off When We Leave
  trigger:
  - entity_id: device_tracker.gerritjan
    platform: state
    from: home
    to: not_home
  condition: []
  action:
  - data:
      entity_id: group.switches
    service: switch.turn_off

Still, weird that it didn’t seem to trigger

Can you please post your code again, looks to me like a indentation error or something like this. Your automation can not trigger because it is not correct.

  action:
  - entity_id: group.switches
    service: homeassistant.turn_off

The switch.turn_off service is for switches, not groups.

The clue was in the Error:


Check: automation->action->0-

This is pointing to your first action (counting starts at 0).

1 Like

Sorry, this one doesn’t work either haha

This was before you helped me

automation 19:
  alias: Turn All Lights off When We Leave
  trigger:
  - entity_id: device_tracker.gerritjan
    platform: state
    from: home
    to: not_home
  condition: []
  action:
  - data:
    entity_id: group.switches
    service: switch.turn_off

The “data” in action is wrong.
Try like this:

automation 19:
  alias: Turn All Lights off When We Leave
  trigger:
  - entity_id: device_tracker.gerritjan
    platform: state
    from: home
    to: not_home
  condition: []
  action:
  - service: homeassistant.turn_off
    entity_id: group.switches

Grrrrrrr, the lamp stays on.

With this exact code I posted, do you get an error in the logs? Do you light entities start with “switch.” or with “light.” ?