Complex Automations (Heating)

So I’m working on my setup of home assistant. I’ve got specific automation questions, but I want to ask some basic HA questions up front. So… this is my understanding:

Automations need to have both a trigger and an action. Conditions come after a trigger to set whether or not an action takes place. Scripts are a way to have multiple actions happen in a sequence? I don’t think I understand the context of templates at all. Am I in the right ballpark?

I’ve got a very basic setup. I have 3 zones in my apartment (bedroom, living room, and hallway) that I monitor with three Aeotec MultiSensor 6 sensors. In each zone I have a Dimplex electric wall radiator plugged into a TZ88 Z-Wave Smart Energy Plug. It’s all tied into my RP3 via a Aeon Labs Aeotec Z-Stick (Gen 5). Right now I have 3 instances of this very simple automations in each zone running in Home Assistant:

automation 2: - alias: _Temp Bedroom High
trigger: - platform: numeric_state entity_id: sensor.aeotec_zw100_multisensor_6_temperature above: 73
action: service: homeassistant.turn_off entity_id: - switch.wenzhou_tkb_control_system_tz88e_smart_energy_plugin_switch_switch
alias: _Temp Bedroom Low
trigger:
platform: numeric_state entity_id: sensor.aeotec_zw100_multisensor_6_temperature below: 69
action: service: homeassistant.turn_on entity_id: - switch.wenzhou_tkb_control_system_tz88e_smart_energy_plugin_switch_switch

This works well and has been stable over the last month or so. But I’m wanting to add some flexibility to my setup. I’m wanting to have my HA setup see if I am home. If I’m home and awake, the above default is brilliant. When I go to work, I’m editing the temps manually (to lower defaults to save energy) until I figure this all out. I’d like HA to know if I’m not home and at work, or not home and not working. I’m thinking of using a mixture of Owntracks, Google calendar, and/or NMAP?

Basically I want HA to:

  • know that I’m leaving home for work and turn down the default temps - know that I’m leaving home, but I’m not scheduled to work

  • know that I’m leaving work, and turn my heat back up to the normal defaults.

  • know that I’m coming home, but I’m not at work and to turn my heat back to the normal defaults.

  • know that I’m at home, sleeping, and to turn down the default temps.

  • know that I have to work in the morning, and turn the heat up to the defaults an hour before I wake up.

In terms of leaving for work, I think this could be done using Owntracks, but I found that by itself it’s not consistently reliable (will show me as not being home, when I am home) so I think maybe using another component (like Google Calendar, for example) might be helpful.

In terms of leaving home when I’m not working, I’m thinking HA could use the Google Calendar component to flag me as not working.

In terms of leaving work, my clockout time at work is pretty consistent. I also run Tasker on my phone at work (to stay on the work wifi and keep my phone on silent). Thinking maybe I could send HA an http post (via exit task maybe?).

Say I’m not working, but out at a bar. HA has turned down the defaults, I’m thinking of a way to signal to HA I’m on my way home and to return to normal defaults. I don’t think there’s a reliable way to do this automatically, but maybe a tasker shortcut to send a http post to HA saying I’m on my way home?

I think I can accomplish the sleeping piece in terms of falling asleep I think I accomplish by using the the Fitbit component. The waking up bit I think maybe setting a default time based on if I’m working or not (again, google calendar component.

My issue is understanding how to piece each one of these things together. I think I’m confused exactly on how conditions, scripts, and templates fit together to make a lot of these happen. Are they separate automations? I’m seeking clarity.

Any suggestions on resources that are easy to understand? I’ve watched (and get a lot of help) the videos put together by Bruh Automation. To my unmathematical brain the explanations on the HA website seem like they’re written in a language I currently don’t completely understand. Any suggestions? Up to this point I’ve just kind of cobbled together other people’s posted code.

I think your goals are good, but you will need to get there in small steps.

Try, to start with, just detecting when you are not at home, and turning the temperature down. I just use nmap as a presence detector for that.

Then go for detecting you are in bed. I use the fact that my phone is on charge and connected to the bluetooth speaker by my bed, to determine I am in bed. This causes the Llama app in my phone to call a shortcut created by the ‘HTTP Shortcuts’ app to update an input boolean in HA.

Once you have this done, moving on to predicting where you will be with owntracks or gpslogger will hopefully seem less daunting.

Yes, otherwise there would be no point

Strictly speaking, they don’t have to be after in the configuration (if that is what you meant). They are just evaluated after one of the trigger(s) happen. So, if a trigger happens, the conditions are evaluated. The logic has to evaluate to true for the actions to be performed.

Sure, but you don’t need scripts. You can do multiple actions in an automation. The benefit of scripts is that you can write it once and use it in multiple spots.

Use them any time you want to have variable programming. For example, I wrote a single script that sends notifications if a light is left on. So that the message gets formatted properly I use a template to extract the name of the light that was left on.


After that, you have a fairly complex problem. Break it down into chunks and do one at a time and each can be their own automation. For example, first add the home/not-home part. Then add in sleeping. Then add in left home for work/not work, and so on.

You may have to revisit automations to fix poor assumptions along the way, but it happens. Many of these you won't need templates for, some you may. Try to write your logic (conditions) in a neat and efficient way. The less conditions to achieve what you want the easier it'll be to maintain.

As a side note, I use Automate to update location (geofencing) combined with DD-WRT. This should give similar results as OwnTracks + NMAP. If you find you are leaving zones try increasing the radius. You don't need a tight radius around your house unless there is another zone near by or you travel near by and might accidentally trigger it. My zone radius for home is probably 200m, which means I don't really leave unless I go pretty far.
1 Like

Try, to start with, just detecting when you are not at home, and turning the temperature down. I just use nmap as a presence detector for that.

I’ve actually got Owntracks running to show if I’m home or not, I just don’t know right now how to integrate it into my heating automation. That’s where my question is. How do I add in these in? Is it a single automation using conditions? Or scripts? Part of this is that I’m just knowledgeable enough to be dangerous :smiley:

One thing I’ve realized is that this is a project that will never be “finished”, so I need to understand fully what I’m doing.

Hey Marti - welcome! You’ve gotten great advice up to this point. Let me answer the above question by showing you the way I do it now. I’m sure someone will come along and may show you a better way and then I’ll incorporate that into my automations. That’s usually the way things work here! :smile:

I have a Mail notifications that I run on the Google Home while I am at the house but I like them to go to pushbullet when I’m away so that I get the notification on my phone. I do it with two automations. (Actually three, but if you read the comment lines you’ll see why.)

# You've got mail!  (Conditional alerts for home/away)

- alias: 'Mail Alert Home'
  trigger:
    platform: state
    entity_id: binary_sensor.mailbox
    from: 'off'
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: input_boolean.mailbox_switch
        state: 'off'
      - condition: state
        entity_id: device_tracker.rpitera_rpitera
        state: 'home'
  action:
    - service: tts.google_say
      entity_id: media_player.living_room_home
      data:
        message: 'Pardon the interruption, but the mail has been delivered Robert.'
    - service: input_boolean.turn_on
      entity_id: input_boolean.mailbox_switch

- alias: 'Mail Alert Away'
  trigger:
    platform: state
    entity_id: binary_sensor.mailbox
    from: 'off'
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: input_boolean.mailbox_switch
        state: 'off'
      - condition: state
        entity_id: device_tracker.rpitera_rpitera
        state: 'not_home'
  action:
    - service: notify.pushbullet
      data:
        message: 'Mail has been delivered, Robert.'
    - service: input_boolean.turn_on
      entity_id: input_boolean.mailbox_switch

# But don't notify me when I open the box to retrieve it!

- alias: 'Mail Retrieval'
  trigger:
    platform: state
    entity_id: binary_sensor.mailbox
    from: 'off'
  condition:
    condition: state
    entity_id: input_boolean.mailbox_switch
    state: 'on'
  action:
    service: input_boolean.turn_off
    entity_id: input_boolean.mailbox_switch

After that, you have a fairly complex problem. Break it down into chunks and do one at a time and each can be their own automation. For example, first add the home/not-home part.

So should I change the automation 2 to an automation that’s specific for when I’m home, like:

automation 2: - alias: _Temp Bedroom High home
trigger: - platform: numeric_state entity_id: sensor.aeotec_zw100_multisensor_6_temperature above: 73
condition:
   - condition: state
      entity_id: 'device_tracker.phone_phone'
      state: 'home'
action: service: homeassistant.turn_off entity_id: - switch.wenzhou_tkb_control_system_tz88e_smart_energy_plugin_switch_switch

alias: _Temp Bedroom Low home
trigger:
platform: numeric_state entity_id: sensor.aeotec_zw100_multisensor_6_temperature below: 69
condition:
   - condition: state
      entity_id: 'device_tracker.phone_phone'
      state: 'home'
action: service: homeassistant.turn_on entity_id: - switch.wenzhou_tkb_control_system_tz88e_smart_energy_plugin_switch_switch

and then then copy the same automation, but change the high and low temps and the state (not home)? Like this?

automation 3: - alias: _Temp Bedroom High away
    trigger: - platform: numeric_state entity_id: sensor.aeotec_zw100_multisensor_6_temperature above: 69
    condition:
       - condition: state
          entity_id: 'device_tracker.phone_phone'
          state: 'away'
    action: service: homeassistant.turn_off entity_id: - switch.wenzhou_tkb_control_system_tz88e_smart_energy_plugin_switch_switch

    alias: _Temp Bedroom Low away
    trigger:
    platform: numeric_state entity_id: sensor.aeotec_zw100_multisensor_6_temperature below: 55
    condition:
       - condition: state
          entity_id: 'device_tracker.phone_phone'
          state: 'away'
    action: service: homeassistant.turn_on entity_id: - switch.wenzhou_tkb_control_system_tz88e_smart_energy_plugin_switch_switch

It will be easier to see your code if you select the code in the forum editor and press the preformatted text button as shown in the image below.