Automation Templates

Hi. I’m trying to make an automation that turns off my lights and sets my nest thermostat to away mode when I’m not home and reverses everything when I’m home. I’ve been able to do this with multiple separate automations but I’m unsure how to achieve this in 1 automation. Any body have any example scripts? or any other help would be greatly appreciated. Thanks!

There are so many topics on this forum that cover this in pieces. You can use a google search with site:home-assistant.io to find the results in the forum that cover things like this.

Otherwise, it’s best if you post your current automations so people can help you combine them. What you are asking for without any place to start is a tall order and you may not get good help.

If you choose to post your automations, make sure you use markdown so that the code is formatted properly. Click the link in the blue ribbon at the top of the page to learn how to use markdown.

Thanks for the reply. I’ve looked at a bunch of topics and I’m stumped. My code below works with my lights but I can’t figure out how to add the climate service, climate.set_away_mode, so that it’s all in 1 automation.

- alias: Presence Detection
  trigger:
    platform: state
    entity_id: device_tracker.d_phone
  action:
  - service_template: >
        {% if is_state("device_tracker.d_phone", "not_home") %}
          switch.turn_off
        {% else %}
          switch.turn_on
        {% endif %}
    entity_id: group.light_group

So you want to just set it to away mode?

Well, first off, you can add as many services in the action section as you want. Second what do you want to do if the device tracker is home in the automation?

I basically want to set the away mode to “on” when I’m gone and “off” when I’m home. I would also like it to turn the lights off and on accordingly. Not sure if that’s possible with just 1 automation. The normal service for the climate is below but I just don’t know how to put it in the template and do what I need it to do.

  - service: climate.set_away_mode
    data:
      entity_id: climate.hallway
      away_mode: 'off'

All you would need to do is use the same template you are using in the service_template, but change the results. You’d also have to make sure it’s in the correct field.

  - service: climate.set_away_mode
    data_template:  #< change this to data_template to allow templates in fields
      entity_id: climate.hallway
      away_mode: >
        {% if is_state("device_tracker.d_phone", "not_home") %}
          on
        {% else %}
          off
        {% endif %}

Then, you’d need to do that for each light you want to turn on/off as well.

Awesome. Works great. Thanks!!

I’ve got a weird issue now. Its seems to be triggering multiple times so now it’s overiding another automation later on. Is there a way to have this automation only trigger once when the device tracker is not_home?

It will trigger every state change. So you need to figure out how you want it to work. You only want it to trigger when the state changes to not_home?

Yes I think the device tracker must be updating when I’m driving even thought the state hasn’t changed from not_home to home. So it’s sending the away updates multiple times. I basically want to turn lights and my thermostat on and off depending on if I’m home or not… WITH 1 exception. I want my thermostat to turn to heat on an hour before I get off from work. I’m sure I screwed up the latest code

- alias: Presence Detection - Weekday
  trigger:
    platform: state
    entity_id: device_tracker.d_phone
  condition:
    condition: state
    entity_id: 'binary_sensor.workday_sensor'
    state: 'on'
  action:
  - service_template: >
        {% if is_state("device_tracker.d_phone", "home") %}
          switch.turn_on
        {% else %}
          switch.turn_off
        {% endif %}
    entity_id: group.light_group
  - service: climate.set_away_mode
    data_template: 
      entity_id: climate.hallway
      away_mode: >
        {% if is_state("device_tracker.d_phone", "not_home") %}
          on
        {% elif is_state('sensor.time', '15:00') %}
          off
        {% endif %}
  - service: climate.set_operation_mode
    data_template: 
      entity_id: climate.hallway
      operation_mode: >
        {% if is_state("device_tracker.d_phone", "not_home") %}
          eco
        {% elif is_state('sensor.time', '15:00') %}
          heat
        {% endif %}

The state is changing. So ‘state’ changes in home assistant mean ‘state object’ changes. State changes will occur the state or attributes change. So in the case of a device_tracker, the latitude and longitude will change as you move around.

So, you can add a condition that will only update on the full state changes.

- alias: Presence Detection - Weekday
  trigger:
    platform: state
    entity_id: device_tracker.d_phone
  condition:
    - condition: state
      entity_id: 'binary_sensor.workday_sensor'
      state: 'on'
    - condition: template
      value_template: >
        {{ trigger.to_state.state in ['home','not_home'] and trigger.to_state.state != trigger.from_state.state }}
  action:
    ...

This condition will essentially cause the automation to only fire when you exit or enter your home zone, and it will ignore all other state changes.

I’m getting a config error when I add that code. Here’s what it says
Invalid config for [automation]: expected a dictionary for dictionary value @ data[‘action’][0][‘data_template’]. Got None. (See /config/configuration.yaml, line 220). Please check the docs at https://home-assistant.io/components/automation/

- alias: Presence Detection - Weekday
  trigger:
    platform: state
    entity_id: device_tracker.d_phone
  condition:
    - condition: state
      entity_id: 'binary_sensor.workday_sensor'
      state: 'on'
    - condition: template
      value_template: >
        {{ trigger.to_state.state in ['home','not_home'] and trigger.to_state.state != trigger.from_state.state }}
  action:

Nevermind I think it’s working now. Thanks!!!

1 Like