Automation - house thermostat based on phone location - not working

Hi!

Been trying to get this automation to work where if my phone and my wife’s phone leaves the house to turn down the heat to 16 and when either of us comes home, to turn the heat back up to 21.5.

The tracking is provided by the device_tracker groups below, using the Home Assistant companion app. on each phone, and shown below:

# Somebody Home
somebody_home:
  name: Somebody is home
  entities:
    - device_tracker.sony_xperia_zx1
    - device_tracker.iphone
  all: false 

# Everybody Home
everybody_home:
  name: Everybody is home
  entities:
    - device_tracker.sony_xperia_zx1
    - device_tracker.iphone
  all: true  

the above works pretty well, when we both leave the house (greater than 300m), the tracking correctly report as everybody home “Away”, when only one person is home, its says somebody home “Home” and everbody home “Away”.

So the above works well. Now, when trying to use this presence informatoin to control my thermostat. It doesn;t seem to work. What seems to be the issue here? Please see my automation:

alias: Presence - Everybody Leaves Home - Adjust Furance Temperature Heat
description: ''
trigger:
  - platform: state
    entity_id: group.somebody_home
    from: Home
    to: Away
    id: Turn Down Heat
  - platform: state
    entity_id: group.somebody_home
    from: Away
    to: Home
    id: Turn Up Heat
condition:
  - condition: device
    device_id: 7bace92912fa0cf344ae84233548c1ec
    domain: climate
    entity_id: climate.house_thermostat_nativezone
    type: is_hvac_mode
    hvac_mode: heat
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Turn Down Heat
        sequence:
          - service: climate.set_temperature
            data:
              temperature: 16
            target:
              entity_id: climate.house_thermostat_nativezone
          - choose:
              - conditions:
                  - condition: trigger
                    id: Turn Up Heat
                sequence:
                  - service: climate.set_temperature
                    data:
                      temperature: 21.5
                    target:
                      entity_id: climate.house_thermostat_nativezone
            default: []
    default: []
mode: single

Your to and from states are incorrect. Person entities, device trackers, and their groups have states of “home” or “not_home”.

alias: Presence - Everybody Leaves Home - Adjust Furance Temperature Heat
description: ''
trigger:
  - platform: state
    entity_id: group.somebody_home
    from: home
    to: not_home
    id: Turn Down Heat
  - platform: state
    entity_id: group.somebody_home
    from: not_home
    to: home
    id: Turn Up Heat
condition:
  - condition: device
    device_id: 7bace92912fa0cf344ae84233548c1ec
    domain: climate
    entity_id: climate.house_thermostat_nativezone
    type: is_hvac_mode
    hvac_mode: heat
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Turn Down Heat
        sequence:
          - service: climate.set_temperature
            data:
              temperature: 16
            target:
              entity_id: climate.house_thermostat_nativezone
      - conditions:
          - condition: trigger
            id: Turn Up Heat
        sequence:
          - service: climate.set_temperature
            data:
              temperature: 21.5
            target:
              entity_id: climate.house_thermostat_nativezone
    default: []
mode: single

Group Behavior
Person Entity
Device Tracker States

1 Like

@Didgeridrew thank you very much! It should work now, but I wil report back when we leave the house next.
Cheers

We also have air-conditioning (cooling) for when its hot outside. Any way I can use the same automation to create the cooling rules? for example, when we leave, change the thermostat temp to a higher temperature (+26oC) and then when we come home, change it to 21.5oC. At the moment the condition has to be that the HVAC has to be in heat mode to work, if I simply add a cooling condition, would that work? it has to be “or” based condition and not an “and” based condition…

The more I think about it, I don’t think I can make heating and cooling in the same automation, I’ll have to make a similar and second automation as above, but with cooling…

Yes, there are a few ways you can do that. Here are two examples:

Using Variables and Templates
alias: Presence - Everybody Leaves Home - Adjust Furance Temperature Heat
description: ''
variables:
  cool_home: 21.5
  cool_away: 26
  heat_home: 21.5
  heat_away: 16
  temp_home: >
    {{ heat_home if state_attr('climate.house_thermostat_nativezone', 'hvac_mode') = 'heat' else cool_home }}
  temp_away: >
    {{ heat_away if state_attr('climate.house_thermostat_nativezone', 'hvac_mode') = 'heat' else cool_away }}
trigger:
  - platform: state
    entity_id: group.somebody_home
    from: home
    to: not_home
    id: Away Temp
  - platform: state
    entity_id: group.somebody_home
    from: not_home
    to: home
    id: At Home Temp
condition:
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Away Temp
        sequence:
          - service: climate.set_temperature
            data:
              temperature: '{{ temp_away }}'
            target:
              entity_id: climate.house_thermostat_nativezone
      - conditions:
          - condition: trigger
            id: At Home Temp
        sequence:
          - service: climate.set_temperature
            data:
              temperature: '{{ temp_home }}'
            target:
              entity_id: climate.house_thermostat_nativezone
mode: single

Using Nested Choose Actions
alias: Presence - Everybody Leaves Home - Adjust Heat/AC Temp
description: ''
trigger:
  - platform: state
    entity_id: group.somebody_home
    from: home
    to: not_home
    id: Away
  - platform: state
    entity_id: group.somebody_home
    from: not_home
    to: home
    id: Home
condition:
action:
- choose:
    - conditions:
        - condition: state
          entity_id: climate.house_thermostat_nativezone
          attribute: hvac_mode
          state: cool
      sequence:              
        - choose:
          - conditions:
            - condition: trigger
              id: Away
            sequence:
            - service: climate.set_temperature
              data:
                temperature: 26
              target:
                entity_id: climate.house_thermostat_nativezone
          - conditions:
            - condition: trigger
              id: Home
            sequence:
            - service: climate.set_temperature
              data:
                temperature: 21.5
              target:
                entity_id: climate.house_thermostat_nativezone
    - conditions:
        - condition: state
          entity_id: climate.house_thermostat_nativezone
          attribute: hvac_mode
          state: heat
      sequence:              
        - choose:
          - conditions:
            - condition: trigger
              id: Away
            sequence:
            - service: climate.set_temperature
              data:
                temperature: 16
              target:
                entity_id: climate.house_thermostat_nativezone
          - conditions:
            - condition: trigger
              id: Home
            sequence:
            - service: climate.set_temperature
              data:
                temperature: 21.5
              target:
                entity_id: climate.house_thermostat_nativezone
mode: single

Also, note that I edited my previous post because I noticed that you had used 2 Choose actions where only 1 was needed and nested them in a way that the heat would never be turned up when you got home.

1 Like

this is brilliant. Thank you!

The nested choose actions, is brilliant and I believe this is what I was looking for:

  1. In the winter (or when “hvac_mode: heat”) change the thermostat temp to 16oC when not home, and 21.5 oC when coming home.

  2. In the summer (or when “hvac_mode: cool”) change the thermostat temp to 26oC when not home, and 21.5 oC when coming home.

As for heat vs cool, this is something that we decide, whenever the season is, but the script doesn’t care it just goes by what the current HVAC setting is hvac_mode: heat or hvac_mode: cool

For some reason the Variable and Templates one doesn’t make too much sense to me as it doesn’t seem to check what the current state of the temostat is set to (i.e. cool or heat), and seems to be good for just in heating mode…i think that is what you were trying to show anyways…awesome thanks!

The HVAC mode is checked in the variables:

{{ 16 if state_attr('climate.house_thermostat_nativezone', 'hvac_mode') = 'heat' else 26.0 }}

In plain English this template means: “If the HVAC mode is ‘heat’, set the variable “temp_away” equal to 16; otherwise set the variable to 26”

@Didgeridrew ahhh got it! that makes sesnse now.

I’ll try them out now.

awesome and thank you!

I wanted to follow up on this because its not often that we leave home, I was only able to test it in the real world a few times since its implementation last week. I made some modifications (just additions to notify my phone), so here’s the latest:

alias: Presence - Everybody Leaves Home - Adjust Heat/AC Temp
description: ''
trigger:
  - platform: state
    entity_id: group.somebody_home
    from: home
    to: not_home
    id: Away
    for:
      hours: 0
      minutes: 3
      seconds: 0
  - platform: state
    entity_id: group.somebody_home
    from: not_home
    to: home
    id: Home
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: climate.house_thermostat_nativezone
            attribute: hvac_mode
            state: cool
        sequence:
          - choose:
              - conditions:
                  - condition: trigger
                    id: Away
                sequence:
                  - service: climate.set_temperature
                    data:
                      temperature: 26
                    target:
                      entity_id: climate.house_thermostat_nativezone
                  - service: notify.mobile_app_sony_xperia_zx1
                    data:
                      message: The house temperature is set to 26 oC.
                      title: You left the house
              - conditions:
                  - condition: trigger
                    id: Home
                sequence:
                  - service: climate.set_temperature
                    data:
                      temperature: 21.5
                    target:
                      entity_id: climate.house_thermostat_nativezone
                  - service: notify.mobile_app_sony_xperia_zx1
                    data:
                      message: The house temperature is set to 21.5 oC.
                      title: You are approaching home
      - conditions:
          - condition: state
            entity_id: climate.house_thermostat_nativezone
            attribute: hvac_mode
            state: heat
        sequence:
          - choose:
              - conditions:
                  - condition: trigger
                    id: Away
                sequence:
                  - service: climate.set_temperature
                    data:
                      temperature: 16
                    target:
                      entity_id: climate.house_thermostat_nativezone
                  - service: notify.mobile_app_sony_xperia_zx1
                    data:
                      message: The house temperature is set to 16 oC.
                      title: You left the house
              - conditions:
                  - condition: trigger
                    id: Home
                sequence:
                  - service: climate.set_temperature
                    data:
                      temperature: 21.5
                    target:
                      entity_id: climate.house_thermostat_nativezone
                  - service: notify.mobile_app_sony_xperia_zx1
                    data:
                      title: You are approching the house
                      message: The house temperature is set to 21.5 oC.
mode: single

Here’ what happens. when we leave the house after a few minutes, the automation says it runs (after 3 mins), then when we come back, the automation says it again, however, it didn’t actualy change the thermostat temperature nor did a notification to the phone be sent, and no error messages in the logs. So this leads me to believe that the automation conditions are not being met.

At first I thought it was because of the attributes:

attribute: hvac_mode

and if it should be

attribute: hvac_modes

Because heres how it reports state:

BUT, changing the attribute to hvac_modes, doesn’t solve the problem either.

So, im not sure, without any errors, why this automation is not seeing through. Could it have something to do withthe conditions structure?

You’re 95% there… if you wanted the modes you would use attribute: hvac_modes. But, the actual active mode value is being store as the base state; hvac_modes is just a list of available modes.

As you observed, your automation was triggered, but none of the Choose conditions evaluated to True because it was looking for a value that does not exist. Even if you had the correct variable name it would still be looking for a string heat and finding an array i.e. ['off', 'auto', 'heat_cool', 'heat', 'cool'] so it would evaluate as False. For testing purposes you could add a default to your first Choose action to send a notification that the automation was triggered, but no hvac action was taken.

To fix the issue do not assign a value to the attribute variable in the choose conditions.

Modified Automation
alias: Presence - Everybody Leaves Home - Adjust Heat/AC Temp
description: ''
trigger:
  - platform: state
    entity_id: group.somebody_home
    from: home
    to: not_home
    id: Away
    for:
      hours: 0
      minutes: 3
      seconds: 0
  - platform: state
    entity_id: group.somebody_home
    from: not_home
    to: home
    id: Home
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: climate.house_thermostat_nativezone
            state: cool
        sequence:
          - choose:
              - conditions:
                  - condition: trigger
                    id: Away
                sequence:
                  - service: climate.set_temperature
                    data:
                      temperature: 26
                    target:
                      entity_id: climate.house_thermostat_nativezone
                  - service: notify.mobile_app_sony_xperia_zx1
                    data:
                      message: The house temperature is set to 26 oC.
                      title: You left the house
              - conditions:
                  - condition: trigger
                    id: Home
                sequence:
                  - service: climate.set_temperature
                    data:
                      temperature: 21.5
                    target:
                      entity_id: climate.house_thermostat_nativezone
                  - service: notify.mobile_app_sony_xperia_zx1
                    data:
                      message: The house temperature is set to 21.5 oC.
                      title: You are approaching home
      - conditions:
          - condition: state
            entity_id: climate.house_thermostat_nativezone
            state: heat
        sequence:
          - choose:
              - conditions:
                  - condition: trigger
                    id: Away
                sequence:
                  - service: climate.set_temperature
                    data:
                      temperature: 16
                    target:
                      entity_id: climate.house_thermostat_nativezone
                  - service: notify.mobile_app_sony_xperia_zx1
                    data:
                      message: The house temperature is set to 16 oC.
                      title: You left the house
              - conditions:
                  - condition: trigger
                    id: Home
                sequence:
                  - service: climate.set_temperature
                    data:
                      temperature: 21.5
                    target:
                      entity_id: climate.house_thermostat_nativezone
                  - service: notify.mobile_app_sony_xperia_zx1
                    data:
                      title: You are approching the house
                      message: The house temperature is set to 21.5 oC.
    default:
      - service: notify.mobile_app_sony_xperia_zx1
        data:
          message: "Your HVAC Automation triggered, but no action was taken."
          title: Automation Error
mode: single
1 Like

Thanks for explaining that! I’ll give this new version a go. Cheers

that last code did it. Worked exactly as discussed!

Cheers and thanks again

@Didgeridrew for Home Assistant Best Coder Helper award.

actually, the award recomendation goes beyond that. Thanks to your automations, my furnace shutdown this morning because my wife had the window open when it was +8oC. Inside temperatures dropped to +19oC, thus preventing the furnace from turning on (which was set for 21.5oC so it would have turned on otherwise)…so you saved the earth a little GHG emissions!

1 Like