Automation: Service Template?

Hi Community,

So if you’ve seen my posts in other threads, I’m using Zanzito and was running into issues where GPS was causing multiple zone transitions. After some testing, I’ve determined my fix by switching off high-precision was a working solution (for the most part–I have other plans if not.)

One thing I just ran into is my wife left the house with me still home and my automation ended up firing multiple MQTT changes to my device causing 8 zone notifications :slight_smile: After staring at it for a bit I realized that it was sending the MQTT service call regardless of who entered/left:

- alias: Tracker Leave Zone
  trigger:
    - platform: zone
      event: leave
      zone: zone.home
      entity_id: device_tracker.homeassistant_asusz012d, device_tracker.val
    - platform: zone
      event: leave
      zone: zone.work_chris
      entity_id: device_tracker.homeassistant_asusz012d, device_tracker.val
    - platform: zone
      event: leave
      zone: zone.work_val
      entity_id: device_tracker.homeassistant_asusz012d, device_tracker.val
  action:
    - service: notify.fbmsg
      data_template:
        message: "{{ trigger.to_state.attributes.friendly_name }} left {{ trigger.zone.attributes.friendly_name }} at {{ now().strftime('%Y-%m-%d %H:%M') }}"
        target:
          - !secret fbmsg_chris
          - !secret fbmsg_val
    - service: mqtt.publish
      data_template:
        topic: "zanzito/homeassistant/set_prefs"
        payload: >-
          {
            "location_high_precision": true
          }

So it seems to me that I need to use a service_template here in a scenario like so:

{% if trigger.entity_id == 'device_tracker.homeassistant_asusz012d' %}

And I’ve looked at a handful of examples but can’t get working solution. Basically I want the MQTT to only target when my device triggers the automation, not any device and I’ve hit a wall on it.

Anyone have any ideas to help me out here?

Put a template condition between the two services, it will then only execute the second service if the template resolves true.

2 Likes

Can you give me an example? Not asking you to do the work just a framework example please :slight_smile:

Sure, this is the action of an automation for an alarm clock in the boys’ bedroom…

  action:
    - service: script.boys_wake_up
    - condition: state
      entity_id: input_boolean.boys_alarm_clock_repeat
      state: 'off'
    - service: homeassistant.turn_off
      entity_id: input_boolean.boys_alarm_clock_status

It runs the ‘wake up’ script every time the alarm time is reached, but only switches the boolean off if I haven’t selected the alarm to repeat the next day.

1 Like

Thanks. Your example makes perfect sense, now just trying to adapt it to my use-case. I’m thinking something like this:

action:
  - service: notify.fbmsg
      data_template:
        message: ...
        target: ...
  - condition: state
      entity_id: device_tracker.homeassistant_asusz012d
      state: 'not_home'
  - service: mqtt.publish
      data_template:
        message: ...
        payload: ...

That look right?

Editted to match advice.

Yeah, but you want the mqtt to be conditional, so you’d want the notification above the condition block and the mqtt publish below.

Editted my original example—passes config test; testing now. Thank you!

Entity_id and state need to come left 2 spaces :+1:

(as do all the other keys that haven’t got a hyphen)

action:
  - service: notify.fbmsg
    data_template:
      message: ...
      target: ...
  - condition: state
    entity_id: device_tracker.homeassistant_asusz012d
    state: 'not_home'
  - service: mqtt.publish
    data_template:
      message: ...
      payload: ...

My YAML looks exactly like yours :slight_smile: Just need someone to leave the house now to test. Fairly certain this is what I want so I’ll mark it as solution. Always appreciate the help, in the past and today!

1 Like

No worries :+1:

Worked! When I switched to not_home it fired. When I switched to home however, it didn’t so I’m still missing something but not related to this. many thanks my friend!

EDIT: Figured it out, shouldn’t be in zone automation, should be in my home/not_home automation.

1 Like