AndyHeinz
(Andreas Hemmerich)
November 23, 2021, 8:42am
1
Hi,
I’m not sure how to get around of this:
I want to display who is on the phone, so I created a couple of entities (the phones) as switches. If the phone is picked, the switch should turn ‘on’, if the person’s is hanging up, the switch should turn ‘off’.
The Trigger would be a webhook, which is initiated by our phone service provider (placetel). So I wanted to setup automations to trigger the switches. We got multiple phones (peer), but only one landline (to).
This is what the payloads look like:
Phones are ringing:
{"call_id":"asdh32847asdhq8zdhaidhj3ueaskdhajdh298hdadhaj","direction":"in","event":"IncomingCall","from":"000111111","to":"222233333"}
Somebody (the peer) is picking up:
{"call_id":"asdh32847asdhq8zdhaidhj3ueaskdhajdh298hdadhaj","direction":"in","event":"IncomingCall","from":"000111111","to":"222233333"}
The Peer is Hanging up:
{"call_id":"asdh32847asdhq8zdhaidhj3ueaskdhajdh298hdadhaj","direction":"in","duration":601,"event":"HungUp","from":"000111111","to":"222233333","type":"accepted"}
I somehow had the idea to use a temporary entity:
service: persistent_notification.create
data:
message: '{{ trigger.data.peer }}'
notification_id: '{{ trigger.data.call_id }}'
how do I implement this? This fails because of the data_template. How get I to create messages with templating?
alias: placetel_notify_api
description: ''
trigger:
- platform: webhook
webhook_id: placetel_notify_api
id: placetel_notify_api
condition: []
action:
service_template: >
{% if (trigger.data.event == 'CallAccepted')) -%}
persistent_notification.create
{%- else -%}
switch.turn_off
{%- endif %}
data_template:
{% if (trigger.data.event == 'CallAccepted')) -%}
message: "{{ trigger.data.peer }}"
notification_id: "{{ trigger.data.call_id }}"
{%- else -%}
entity: switch."{{ trigger.data.peer }}"
{% endif %}
mode: single
Thanks!
Try the following. ( The use of the “x_template” format has mostly been deprecated, and you were missing a “>”)
alias: placetel_notify_api
description: ''
trigger:
- platform: webhook
webhook_id: placetel_notify_api
id: placetel_notify_api
condition: []
action:
service: >
{% if (trigger.data.event == 'CallAccepted')) -%}
persistent_notification.create
{%- else -%}
switch.turn_off
{%- endif %}
data: >
{% if (trigger.data.event == 'CallAccepted')) -%}
message: "{{ trigger.data.peer }}"
notification_id: "{{ trigger.data.call_id }}"
{%- else -%}
entity: switch."{{ trigger.data.peer }}"
{% endif %}
mode: single
finity
November 23, 2021, 3:52pm
3
you can’t add more than one key:value pair inside of a template like that:
action:
service: >
{% if (trigger.data.event == 'CallAccepted')) -%}
persistent_notification.create
{%- else -%}
switch.turn_off
{%- endif %}
data: >
{% if (trigger.data.event == 'CallAccepted')) -%}
message: "{{ trigger.data.peer }}" ### this won't work
notification_id: "{{ trigger.data.call_id }}" ### with this
{%- else -%}
entity: switch."{{ trigger.data.peer }}"
{% endif %}
you should use a “choose:” option to set the persistent notification with the correct data if the call is accepted or else choose the switch actions:
action:
choose:
- conditions:
condition: " {{ (trigger.data.event == 'CallAccepted')) -}}"
sequence:
service: persistent_notification.create
data:
message: "{{ trigger.data.peer }}"
notification_id: "{{ trigger.data.call_id }}"
default:
service: switch.turn_off
data:
entity_id: switch."{{ trigger.data.peer }}"
it’s untested so I’m not 100% sure of the syntax.
1 Like
petro
(Petro)
November 23, 2021, 4:37pm
4
The only way you can template fields is if you use a dictionary. Also, data_template is deprecated, just use data.
data: >
{% if (trigger.data.event == 'CallAccepted')) -%}
{{ {'message': trigger.data.peer, 'notification_id': trigger.data.call_id} }}
{% else %}
{{ {'entity': 'switch.' ~ trigger.data.peer } }}
{% endif %}
1 Like
AndyHeinz
(Andreas Hemmerich)
November 23, 2021, 6:45pm
5
Hi,
thanks so much!
@finity gave me the final input. I didn’t think about conditions in the actions. Works frickin great now!
So for whoever also has placetel as an carrier here’s the automation to change your status.
The only thing you have to setup is the switches. The placetel userids are [someint]@fpbx.de .
My switches are uid[someint].
alias: placetel Notify API
description: 'Incoming Post request from placetel: https://web.placetel.de/integrations/notify_api. You have to subscribe to that API'
trigger:
- platform: webhook
webhook_id: placetel_notify_api
id: placetel_notify_api
condition: []
action:
- choose:
- conditions:
- condition: template
value_template: '{{ trigger.data.event == "CallAccepted" }}'
sequence:
- service: persistent_notification.create
data:
message: uid{{ trigger.data.peer.split("@")[0] }}
notification_id: '{{ trigger.data.call_id }}'
- service: switch.turn_on
target:
entity_id: switch.uid{{ trigger.data.peer.split("@")[0] }}
- choose:
- conditions:
- condition: template
value_template: '{{ trigger.data.event == "HungUp" }}'
- condition: template
value_template: >-
{{ state_attr('persistent_notification.'+trigger.data.call_id,'message') != '' }}
sequence:
- service: switch.turn_off
target:
entity_id: >-
switch.{{state_attr('persistent_notification.'+trigger.data.call_id,'message')
}}
- service: persistent_notification.dismiss
data:
notification_id: '{{ trigger.data.call_id }}'
default: null
mode: single
Still got some styling to do. But ot works decent, and people are happy
finity
November 24, 2021, 3:36am
6
you don’t need to use two choose statements.
just put both under the same one. And you don’t really need a default either:
- choose:
- conditions:
- condition: template
value_template: '{{ trigger.data.event == "CallAccepted" }}'
sequence:
- service: persistent_notification.create
data:
message: uid{{ trigger.data.peer.split("@")[0] }}
notification_id: '{{ trigger.data.call_id }}'
- service: switch.turn_on
target:
entity_id: switch.uid{{ trigger.data.peer.split("@")[0] }}
- conditions:
- condition: template
value_template: '{{ trigger.data.event == "HungUp" }}'
- condition: template
value_template: >-
{{ state_attr('persistent_notification.'+trigger.data.call_id,'message') != '' }}
sequence:
- service: switch.turn_off
target:
entity_id: >-
switch.{{state_attr('persistent_notification.'+trigger.data.call_id,'message')
}}
- service: persistent_notification.dismiss
data:
notification_id: '{{ trigger.data.call_id }}'