Help needed on automation using event call service

Hi I would like to create an automation that is executed every time a device is “seen” . For this I have created following automation:

- alias: Network device status change
  trigger:
    platform: event
    event_type: call_service
    event_data: 
        domain: device_tracker
        service: see
 # condition: 
 #   - condition: template
 #     value_template: "{{ trigger.event.as_dict()['data']['domain'] == 'devicetracker'}}"        
 # above condition does not work at all
  action:
    - service: notify.notify_nancy_telegram
      data_template:
        message: >-
        {{ trigger.event.as_dict()['data']}}
        hi {{ trigger.event.as_dict()['data']['service_data']['dev_id'] }}

When manually fireing the event device_tracker.see with some payload

{"dev_id":"device", "location_name":"home"}

the automation works fine. And generates following output:

{'domain': 'device *tracker', 'service': 'see', 'service* data': {'dev *id': 'device', 'location* name': 'home'}} hi device

However, the automation never fires automatically. what am I missing?

This works for me:

- alias: 'light.turn_on Call_Service'
  trigger:
    platform: event
    event_type: call_service
    event_data: 
      domain: light
      service: turn_on
  action:
    - service: system_log.write
      data_template:
        level: info
        message: >-
          {% set x = trigger.event.as_dict()['data'] %}
          DATA: {{ x }}, ENTITY_ID: {{ x.service_data.entity_id }}

It produced this in the system log:

2019-05-09 09:59:19 INFO (MainThread) [homeassistant.components.system_log.external] DATA: {'domain': 'light', 'service': 'turn_on', 'service_data': {'entity_id': 'light.family'}}, ENTITY_ID: light.family

I looked at a device_tracker’s services.yaml file to confirm the service name (see) and its attributes. It looks like your automation should work.


Initially I thought it might be because of the extra indenting you have for domain and service:

    event_data: 
        domain: device_tracker
        service: see

However, when I added extra indenting in my automation, it continued to work properly.


I think I spotted the error:

        message: >-
        {{ trigger.event.as_dict()['data']}}
        hi {{ trigger.event.as_dict()['data']['service_data']['dev_id'] }}

Change the indenting to this:

        message: >-
          {{ trigger.event.as_dict()['data']}}
          hi {{ trigger.event.as_dict()['data']['service_data']['dev_id'] }}

Thank you very much for taking the time to look at my code. However the issue does not seem to lie within the indentation. Your light example automation works well too by the way.
Perhaps I am missing something else?
I am using fritz.device tracker to scan my network, defined here https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/fritz/device_tracker.py . This method works well for presence detection.
I would assume that the fritz component also somehow triggers the device_tracker.see service?

Thanks in advance.

Unfortunately, I don’t use any device_trackers so I’m not very familiar with the details of their operation. It makes sense that the tracker would use call_service. If it is not triggering your automation, maybe there is a remote possibility that the fritz device_tracker is not using this service? I realize this doesn’t sound plausible but you’ve already confirmed it works when you monitor call_service but for the light domain.

If you shutdown Home Assistant, you can use DB Browser to inspect Home Assistant’s database. The events table will contain a record of every call_service and you can confirm if any were logged for the fritz device_tracker (in the event_data field).

Thank you for that hint. Indeed searching the db file gave some insights. Also I dug into the device tracker logic which does imply that the see job is indeed fired.

I am wondering if this may be a bug?

The service is registered multiple times in the database:

"service_registered" "{"domain": "device_tracker", "service": "see"}" "LOCAL" "2019-05-09 18:16:00.339842" "2019-05-09 18:16:22.611863" "ec767f98f67245959f9894990332c0fe"

however it is never called, such as many other services.

Another device tracker platform which I use does also not fire the event which also explicitly calls the see method.

I am not quite sure where to go from here.

Could this possibly be a bug?

Perhaps if your automation will be triggered if it uses event_type: service_registered

  trigger:
    platform: event
    event_type: service_registered
    event_data: 
        domain: device_tracker
        service: see

However, I don’t know if resulting event object will contain the information you need.

No, as you figured: According to the database the object does not contain any relevant information.

“98503” “service_registered” “{”“domain”": ““device_tracker””, ““service””: ““see””}" “LOCAL” “2019-05-03 05:32:43.082224” “2019-05-03 05:33:02.843096” “fcde4b5ce5e1462383bdbbd56d1809ca”