Turn on my lights when I receive a message

Hello,

I am trying to find out if it is possible to turn on a light when I receive an SMS, discord message or from another application.
If possible, can you tell me how I can achieve this goal?

Thank you for your help.

Since you talk about receiving a SMS, I guess your talking about your phone ? It would help if you tell what OS your phone runs. If it is an android, tasker can do it. If it is IOS, I don’t know.

Thanks for your quick reply,
I am currently with android phone.

That’s still pretty vague. If you explained a little about your use case we might be able to help more. Are the lights local or cloud based? Are the lights controlled using a smart switch or are they wifi/rf /MQTT controlled? You have actionable notifications with telegram,Node-Red flows, IFTT,Join.

I don’t use SMS to trigger anything, but I receive sms notifications for many of my automations.

It depends on which app. I think @ElSlabo said it fine for a bunch of the apps that have integrations. But maybe I can help with SMS. For the way I’ve done it:

You’ll need

  • Android OS (No clue if it’s possible with iOS, but there’s no Tasker, so you’d need a completely different approach)
  • Tasker
  • RESTask plugin. You don’t need this anymore, now I just use the built in HTTP Requests in Tasker
  • Long lived access token (create in “Profile” section of Home Assistant UI).

Procedure

  • Set up Tasker Profile, choose SMS, and link to the task you’ll create
  • In the Task, add RESTask and fill into the “settings” tab: I’ll leave the below, but as mentioned, I use HTTP Reqests now.

Using RESTask:

Request Type: POST
Host: YOUR_HOST/WHEREEVER_RELEVANT (I use https://MY_PUBLIC_HASS/states/sensor.trash_pickup to create/update a sensor called "trash_pickup" in my HASS).
Custom body: XXXX (I have all my info extracted from the SMS, but leave blank if you don't need that. Or enter "%SMSRB" to send the entire content of the SMS to HASS)
Custom body: {"state":"SOME_STATE"} (You can also add attributes, in case you want to add the text from the SMS).

Leave every other field as is. Then switch to the “Headers” tab and enter into the following sections:

Name: Authorzation
Value: Bearer XXX

Where XX is the long lived access token created in HASS. Now you have a sensor that is updated when an SMS is received.

If using HTTP Requests:

Method: POST
URL: Same as "Host" in the above example
Headers: The "Long Lived Access Token" generated in Home Assistant.
Body: Same as "Custom body" in the above example

You can use that in an automation to turn on lights, as you would any other sensor. Hope this helps.

Oh, and of course you need to have some way to reset the sensor, in case you just have the state as “on” or something like that.

Sorry for the two-year-old necro. Second, thanks for the write-up! I was wondering if you could expand on the text extraction portion if possible.

Here’s where I’m at, the laundry machines in our apartment building sends texts to my phone in the format of “Dryer 03: Your laundry is done!” , “Washer 01: Your laundry is done!”, etc.

Following your instructions, I’ve got tasker a sensor.laundry that I can successfully trigger, and the automations/reset functions to go along with it, but I was hoping you might be able to help parse out if it’s the washer or dryer, and what number the machine is so that I could apply those as attributes.

Thanks again!

Sure, basically you just want to find the correct string to use as “splitter”.

TL;DR:

For your case you would (using %Laundry as custom variable) probably:

  • Set up Variable Set where Name = %Laundry and To = %SMSRB (meaning “text body”).
  • Set up Variable Split with Name = %Laundry and Splitter = :.
  • Set up Variable Split with Name = %Laundry1 and Splitter = “ ” (whitespace).
  • Then use HTTP Request (I think the RESTask is deprecated, or at least not needed anymore) with:
    • Method = POST
    • URL = %HA_ADDR/api/states/sensor.laundry
    • Headers = %HA_TOKEN
    • Body could be something like (according to your preference of what to call states/attributes) {"state": "done", "attributes": {"type": "%Laundry11", "machine": "%Laundry12"} }

where %HA_ADDR is a variable set to your HA address (external, for this to work when not on home network) and %HA_TOKEN is a variable set to the long lived token created in the HA UI.

(Note: binary_sensor.laundry might make more sense than sensor.laundry, since I assume this is, in fact, binary (done or not), not something with e.g. a numerical value?)

Explanation

In the task, you want to start by setting your text message to a variable name (Variable Set, first step in my “task”), then perform the needed Variable Splits.

Then remember that the variable will be a list of each part that can be indexed (accessed) by simply adding a number after the variable name (starting with 1, not 0 as e.g. Python does). E.g. if you set your text to the variable name %Laundry, and your text is:

"Dryer 03: Your laundry is done!"

Then splitting with splitter = n, your %laundry variable would now be:

["Dryer 03: Your lau", "dry is do", "e!"]

Meaning %Laundry1 is Dryer 03: Your lau, %laundry1 is "dry is do, etc. You can then split any of those (e.g. %Laundry1), and index by just adding another number after, e.g. splitting %Laundry1 by r, then %Laundry12 would be 03: You.

Note: Maybe Tasker actually just makes new variables, e.g. %Laundry1, instead of a list, but for all practical purposes it’s the same.

Example that can be imported

This is an example of the text I receive:

Du vil onsdag d. 01-06-2022 fĂĄ afhentet storskrald pĂĄ adressen REDACTED.

SMS-service kan ændres på REDACTED

I set up a Tasker profile with “Event” → “Received Text”, and set the “Sender” to whatever the sender is (or leave blank to try with any text).

I linked that to a task:

Save this as an *.xml file, and import in Tasker if you want to use as template
<TaskerData sr="" dvi="1" tv="5.15.14">
<Task sr="task6">
<cdate>1537800726258</cdate>
<edate>1654844089223</edate>
<id>6</id>
<nme>Trash Pickup</nme>
<pri>100</pri>
<Action sr="act0" ve="7">
<code>547</code>
<Str sr="arg0" ve="3">%trashtype</Str>
<Str sr="arg1" ve="3">%SMSRB</Str>
<Int sr="arg2" val="0"/>
<Int sr="arg3" val="0"/>
<Int sr="arg4" val="0"/>
<Int sr="arg5"/>
<Int sr="arg6" val="0"/>
</Action>
<Action sr="act1" ve="7">
<code>590</code>
<Str sr="arg0" ve="3">%trashtype</Str>
<Str sr="arg1" ve="3"> pĂĄ </Str>
<Int sr="arg2" val="0"/>
<Int sr="arg3" val="0"/>
</Action>
<Action sr="act2" ve="7">
<code>590</code>
<Str sr="arg0" ve="3">%trashtype1</Str>
<Str sr="arg1" ve="3">fĂĄ afhentet </Str>
<Int sr="arg2" val="0"/>
<Int sr="arg3" val="0"/>
</Action>
<Action sr="act3" ve="7">
<code>590</code>
<Str sr="arg0" ve="3">%trashtype1</Str>
<Str sr="arg1" ve="3">u vil</Str>
<Int sr="arg2" val="0"/>
<Int sr="arg3" val="0"/>
</Action>
<Action sr="act4" ve="7">
<code>590</code>
<Str sr="arg0" ve="3">%trashtype12</Str>
<Str sr="arg1" ve="3">fĂĄ afhentet</Str>
<Int sr="arg2" val="0"/>
<Int sr="arg3" val="0"/>
</Action>
<Action sr="act5" ve="7">
<code>590</code>
<Str sr="arg0" ve="3">%trashtype121</Str>
<Str sr="arg1" ve="3"> d. </Str>
<Int sr="arg2" val="0"/>
<Int sr="arg3" val="0"/>
</Action>
<Action sr="act6" ve="7">
<code>547</code>
<Str sr="arg0" ve="3">%Trashtype</Str>
<Str sr="arg1" ve="3">%trashtype122</Str>
<Int sr="arg2" val="0"/>
<Int sr="arg3" val="0"/>
<Int sr="arg4" val="0"/>
<Int sr="arg5"/>
<Int sr="arg6" val="0"/>
</Action>
<Action sr="act7" ve="7">
<code>547</code>
<Str sr="arg0" ve="3">%Trashdate</Str>
<Str sr="arg1" ve="3">%trashtype1212</Str>
<Int sr="arg2" val="0"/>
<Int sr="arg3" val="0"/>
<Int sr="arg4" val="0"/>
<Int sr="arg5"/>
<Int sr="arg6" val="0"/>
</Action>
<Action sr="act8" ve="7">
<code>547</code>
<Str sr="arg0" ve="3">%Trashday</Str>
<Str sr="arg1" ve="3">%trashtype1211</Str>
<Int sr="arg2" val="0"/>
<Int sr="arg3" val="0"/>
<Int sr="arg4" val="0"/>
<Int sr="arg5"/>
<Int sr="arg6" val="0"/>
</Action>
<Action sr="act9" ve="7">
<code>339</code>
<Bundle sr="arg0">
<Vals sr="val">
<net.dinglisch.android.tasker.RELEVANT_VARIABLES>&lt;StringArray sr=""&gt;&lt;_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES0&gt;%http_cookies
Cookies
The cookies the server sent in the response in the Cookie:COOKIE_VALUE format. You can use this directly in the 'Headers' field of the HTTP Request action&lt;/_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES0&gt;&lt;_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES1&gt;%http_data
Data
Data that the server responded from the HTTP request.&lt;/_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES1&gt;&lt;_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES2&gt;%http_file_output
File Output
Will always contain the file's full path even if you specified a directory as the File to save.&lt;/_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES2&gt;&lt;_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES3&gt;%http_response_code
Response Code
The HTTP Code the server responded&lt;/_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES3&gt;&lt;_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES4&gt;%http_headers()
Response Headers
The HTTP Headers the server sent in the response. Each header is in the 'key:value' format&lt;/_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES4&gt;&lt;_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES5&gt;%http_response_length
Response Length
The size of the response in bytes&lt;/_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES5&gt;&lt;/StringArray&gt;</net.dinglisch.android.tasker.RELEVANT_VARIABLES>
<net.dinglisch.android.tasker.RELEVANT_VARIABLES-type>[Ljava.lang.String;</net.dinglisch.android.tasker.RELEVANT_VARIABLES-type>
</Vals>
</Bundle>
<Int sr="arg1" val="1"/>
<Int sr="arg10" val="0"/>
<Int sr="arg11" val="0"/>
<Int sr="arg12" val="1"/>
<Str sr="arg2" ve="3">%HA_ADDR/api/states/binary_sensor.trash_pickup</Str>
<Str sr="arg3" ve="3">%HA_TOKEN</Str>
<Str sr="arg4" ve="3"/>
<Str sr="arg5" ve="3">{"state":"on", "attributes":{"trash_type":"%Trashtype", "trash_day":"%Trashday","trash_date":"%Trashdate"}
}</Str>
<Str sr="arg6" ve="3"/>
<Str sr="arg7" ve="3"/>
<Int sr="arg8" val="30"/>
<Int sr="arg9" val="0"/>
</Action>
</Task>
</TaskerData>

Just remember to change the Variable Splits, set the %HA_ADDR and HA_TOKEN in the VARS tab, as well as “Body” of HTTP Request as needed. And set up the profile, of course.

1 Like

Just wanted to give you a huge thanks for this! You definitely went above and beyond what I was expecting from a response with your examples and explanations, which were super helpful.

The binary sensor worked perfect and passing through the attributes to my TTS automation was no problem :grinning:

service: tts.google_translate_say
data:
  message: >-
    The laundry in {{state_attr('binary_sensor.laundry', 'type')}}
    {{state_attr('binary_sensor.laundry', 'machine')}} has finished!
  entity_id: media_player.living_room

Now my girlfriend and I won’t have to set a timer every time we do laundry :slight_smile:

Thanks again!