Going beyond basic SMS with HA and Twilio

I got the basic SMS integration up and running with Twilio --> Home Assistant. What I have today works, but I would like to get much more advanced than this simple automation below.

We give out the Twilio SMS number to guests, and they can send a text to the Twilio number, which then triggers an automation that opens the gate to drive up to our house. The second part of the automation also texts us that someone used the service, so we know in advance if someone is coming.

This works great, but it will open the gate regardless of who sends the text, or what the message is.

Here is that current automation:

- id: '1582915285777'
  alias: 5502 - Open Gate
  description: Open Gate Automation
  trigger:
  - platform: webhook
    webhook_id: gate_open_trigger
  condition: []
  action:
  - data: {}
    entity_id: switch.gate
    service: switch.turn_on
  - data:
      message: Someone opened the gate with a text message.
      target:
      - '+11235551212'
      - '+11235551234'
    service: notify.twilio_sms

What I would like to do:

  • Capture the body of the message, and parse it with a Home Assistant Template.
  • Capture the number that sent the message, and use it to look at a list of known numbers
  • Use the known numbers list to send a message of who just opened the gate
  • Create a whitelist of allowed numbers to use the gate
  • parse the message body, and look for open gate and reply hello $user!

What I have been able to do so far:

- id: ‘8172926285707'
  alias: 5501 - Twilio Testing
  description: Testing Twilio Contents
  trigger:
  - platform: webhook
    webhook_id: gate_open_trigger
  condition: []
  action:
  - data_template: 
      message:  '{{ trigger.data }}'
    service: persistent_notification.create

This works, when I send an SMS to the number, with the text open gate I get the following as a notification in Home Assistant:

<MultiDict('ToCountry': 'US', 'ToState': ‘CA', 'SmsMessageSid': 'SM8472411245123412, 'NumMedia': '0', 'ToCity': ‘LOS ANGLES’, 'FromZip': '90001’, 'SmsSid': 'SM6777828241aea122’, 'FromState': ‘CA’, 'SmsStatus': 'received', 'FromCity': ‘LOS ANGLES, 'Body': ‘open gate, 'FromCountry': 'US', 'To': '+11235551212’, 'ToZip': '90005’, 'NumSegments': '1', 'MessageSid': 'SSM6777828241aea122, 'AccountSid': 'AC42013259609123526’, 'From': '+11235551234, 'ApiVersion': '2010-04-01')>

So all the data I want is there, I need the ‘Body’: ‘open gate and the ‘From’: ‘+11235551234’ from this message, but it isn’t JSON, and I have not yet been able to figure out how to parse it and use it.

It would be really cool if I could parse and leverage this data.

So far I am guessing it is going to be something along the following lines:

        value_template: >-
          {% if state_attr(‘message.text‘, ‘Body’)|regex_search(‘Open Gate’, ignorecase=TRUE) %}
            open gate
          {% else %}
            closed

Am I headed in the right direction? Any pointers about how I can best approach this problem? (I know I am going to have to break each line item into pieces)

Oh this is great but I’m having the same problem the message from any number triggers the automation

So far it looks like either Home Assistant cannot do this, or no one has documented how to do this, but Node Red it appears, can do this:

https://flows.nodered.org/flow/d65e0c5e4f5fef767be2#:~:text=The%20way%20it%20works%20is,that%20is%20the%20TWiML%20payload.&text=My%20Node-RED%20flow%20listens%20for%20a%20%2Ftwiliosms%20url.

I use nodered with telegram to do this. That way you can control access by cell phone #.

Any chance anyone has figured out how to do this in HA directly?

UPDATE:
I haven’t been able to figure out how to parse form data, but json was simple, it’s just: trigger.json.PROPERTY_NAME

Example:

choose:
  - conditions:
      - condition: template
        value_template: '{{ trigger.json.newstate == "ON" }}'
    sequence:
      - service: switch.turn_on
        entity_id: switch.on_off_light_15
  - conditions:
      - condition: template
        value_template: '{{ trigger.json.newstate == "OFF" }}'
    sequence:
      - service: switch.turn_off
        entity_id: switch.on_off_light_15

To trigger this webhook with curl for example:

curl --location --request POST 'https://hooks.nabu.casa/BLAH' --header 'Content-Type: application/json' --data-raw '{"newstate": "ON"}'

Hello
Have you been able to figure out how to extract the body and from phone number from the text message?
I am at exactly the same point as you are but instead of we hook, I’m using twilio_data_received and the trigger data is Payload
Thanks!