How to use the passed device_id in an automation service

Here was added the device_id to the pipelines: Allow passing in device_id to pipeline run WS API by balloob · Pull Request #95139 · home-assistant/core · GitHub

I wonder if it is possible to use the passed device_id somehow in an automation service.

An example of what I am trying to do, this is my automation that is supposed to activate an alarm in my phone in X minutes from the time it is triggered by voice:

alias: Alarma
description: |-
  Activa la alarma en X minuts.
trigger:
  - platform: conversation
    command:
      - Activa (la alarma|l'alarma) en {time_amount} minuts
condition: []
action:
  - service: notify.device_id
    data:
      message: command_activity
      data:
        intent_action: android.intent.action.SET_ALARM
        intent_extras: >-
          {% set alarm_time = now() +
          timedelta(minutes=trigger.slots.time_amount|int) %} {{
          'android.intent.extra.alarm.HOUR:' ~ alarm_time.hour|string ~
          ',android.intent.extra.alarm.MINUTES:' ~ alarm_time.minute|string ~
          ',android.intent.extra.alarm.SKIP_UI:true' }}
mode: single

So in service: notify.device_id I wanna replace device_id for the device_id from the device from where it was triggered.

I didnt see that yet, thanks. Will look into it and let you know if I found something.

ok, thank you!

Would be great to be able to do that. Feels like it should be possible, but as I said I’m not sure

Can you try

action:
  - service: notify.notify
    target:
      device_id:
        - "{{ device_id }}"

Based on the example found here.

It does not work even replacing “{{ device_id }}” by the real device_id.

From the documentation I can read this:

Be aware that you might want to change the actual service to whatever service you are actually using since notify.notify is shorthand for the first notify service the system can find and might therefore not be working as intended.

So maybe I need to use notify.platform_id there but not sure which platform_ids exists, maybe notify.android?

I tried many variants and nothing seems to be working.

Can you confirm that the service notify.your_actual_device_id exists? If so, the following should work:

action:
  - service: "{{ 'notify.' ~ device_id }}"

Yes, the device exists because my script works, but just for my device, what I want is for it to work with any device. The thing is that I don’t think device_id as a variable exists, I guess I need to get it from somewhere. Like when I get the sentence variable from trigger.slots.time_amount. I expect something like trigger.device_id. I understand this issue was about being able to pass the device_id to the pipeline so it can be used inside like in my case.

But maybe I’m wrong assuming that, in that case I’ll create a feature request because this would be really usefull for many use cases.

Doesn’t trigger.device_id work? Or maybe trigger.to_state.device_id? On the other hand, that sounds even more wrong than the first one. :laughing: You see, I’m not that far into Assist functions yet, so it’s happy guessing :smiley:

What I can think of as well, isn’t this “variable” inside the response object? That would go to something like response.device_id.

And another guess: :rofl:
According to the code, it should be available at the same level as conversation_id, so maybe the easiest of it all, like device_id?

I’m just guessing here, so don’t take any of this serious. :rofl: But please let us know, how you come along. I’m not there yet, still in the phase where I have to work with custom_sentences and configuring “alias” names for every device… :rofl: But I’ll get there sooner or later, and I’m quite sure I already have use cases where I also need to know the device_id. :slight_smile:

The device_id variable contains something but I’m not sure what it is.

alias: Test sentence
description: ""
trigger:
  - platform: conversation
    command:
      - trigger test sentence
condition: []
action:
  - service: notify.mobile_phone
    data:
      message: |
        {{ 'Device ID: ' ~ device_id }}
mode: single

I get the response:

Device ID: <function device_id at 0x12_char_hex>

where 12_char_hex is replaced by 12 hexadecimal numbers.

Not sure what to do with that.

So what you got there is that basically device_id is a function and if you pass to it an entity_id it will give you its device_id. Not what we need right now.

trigger.device_id Returned an empty string.
trigger.to_state.device_id Returned the following error Error rendering data template: UndefinedError: 'dict object' has no attribute 'to_state'
response.device_id Returned the following error Error rendering data template: UndefinedError: 'response' is undefined

And as I said before, device_id is a function.

:upside_down_face:

Should not this be documented somewhere?

I guess what I am trying to do is not currently possible and that issue was only about passing that function to the pipeline.

But maybe I have been looking at it the wrong way, maybe I need the entity_id and then using the device_id function I will get the device_id.

But how do I get the entity_id? And again, is that even possible right now?

Trigger actually contains {'id': '0', 'idx': '0', 'alias': None, 'platform': 'conversation', 'sentence': ' Identificador de dispositiu', 'details': {}, 'slots': {}}

So the trigger is the conversation entity, not the device. So I think I need to create a feature request for it.

This could also allow for more complex scenarios using scripts where you could for example just say ´turn on the lights´ and since it will know from which mic it comes the command from could turn off the lights of the right room.

I just created the feature request: Be able to get the device_id from where a conversation agent was triggered

1 Like