arnonym / ha-plugins

Home-Assistant SIP Gateway
Apache License 2.0
157 stars 19 forks source link

Generating dynamic Menus and messages on HA Entity State #86

Closed LukasGrebe closed 5 months ago

LukasGrebe commented 5 months ago

I couldn't find this in the doc - is there any way to use the template engine for messages or in other ways act upon entity states?

eg creating a light on-off menu:

press 1 to hear a list lights that are turned on

arnonym commented 5 months ago

You can use the normal home assistant templating features if you're getting called from home assistant and define your menu in the action or if you act on the web-hook for incoming calls and accept the call from there. But you can only get the state from moment you accepted the call. If this was changed in the call or from outside, there's no way to get the current state.

LukasGrebe commented 5 months ago

Could you provide an example where I call into ha-sip (ha-sip accepts the incoming call) and will then tell me a status of an entity? I'm not sure how a webhook could respond to ha-sip.

arnonym commented 5 months ago

First you need to set answer_mode: listen on your SIP account in the ha-sip config and set a webhook name.

Then you can create an automation acting on the webhook:

alias: Accept incoming call
description: ""
trigger:
  - platform: webhook
    allowed_methods:
      - POST
    local_only: true
    webhook_id: sip_call_webhook_id
condition:
  - condition: template
    value_template: "{{ trigger.json.parsed_caller==\"0234123456\" }}"
action:
  - service: hassio.addon_stdin
    data:
      addon: c7744bff_ha-sip
      input:
        command: answer
        number: "0234123456"
        menu:
          message: |
            {% if is_state('light.livingroom', 'on') %}
              The light is on.
            {% else %}
              The light is off.
            {% endif %}
          post_action: hangup
mode: single

You need to change the webhook_id to match your ha-sip config. Also you need to replace the phone number to the phone you're calling from. If you're unsure what the parsed number is, you can call and take a look in the log of ha-sip.

LukasGrebe commented 5 months ago

That's awesome! Thanks!