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)