Trigger light when receiving Whatsapp Or Facebook Message?

Hello,

how would you set up a automation that triggers when receiving a message from a person on your phone from facebook or whatsapp?

I have added the companion app and gave it permission to read messages on my phone.
There is a sensor now in HA that is called sensor.sm_a520f_last_notification.

It displays multiple attributes, but i dont know how to set them up to trigger a light.
Has anyone done this before?

android.appInfo: ApplicationInfo{8629146 com.whatsapp}
android.icon: 2131231578
android.infoText: null
android.largeIcon: null
android.people: [Ljava.lang.String;@c5f5f07
android.progress: 0
android.progressIndeterminate: false
android.progressMax: 0
android.remoteInputHistory: null
android.showChronometer: false
android.showWhen: true
android.subText: null
android.text: Hi
android.title: Name of Person
is_clearable: true
is_ongoing: false
package: com.whatsapp
post_time: 1640548014471
icon: mdi:bell-ring
friendly_name: SM-A520F Last Notification

Create an automation,
set a trigger to use the state of a device.
Set the entity to sensor.sm_a520f_last_notification
Set the attribute to: package
Set the To field to com.whatsapp
Set a trigger ID eg: whatsapp

Add another trigger doing the same thing, but this time setting To to the package name for Facebook Messenger. And again give it an ID eg facebook

Then in the actions, you can either set it to turn a light on. Or if you have coloured LEDs, you can use choose and set the first choose condition to trigger, and choose whatsapp. Then you can set the colour of the light to eg green. And the second choose option, use trigger, facebook. Then set the light to turn on with for example blue or purple.

That’s pretty much it.

2 Likes

Thank you, that worked instantly.
Is it possible to have it for a specific person only?

i tried adding a condition using state sensor.sm_a520f_last_notification
attribute Android.title
State to the persons name, but that doesn’t do anything

Do a condition template:

{{ 'persons name' in state_attr('sensor.sm_af520f_last_notification','title') }}

that will let you check if the name of the person appears IN the title, whereas using a state condition, means the title has to completely match what you write.

1 Like

Thank you, but it gives me the following error

Error evaluating condition in ‘Whatsapp Notification’: In ‘condition’: In ‘template’ condition: TypeError: argument of type ‘NoneType’ is not iterable

For the facebook one i needed to add com.facebook.orca to make it work

Apologies - it’s android.title not just title that should fix it.

Thank you once again,
i did change it to android.title it gives the same error code.

to be clear i used condition > template >
{{ ‘Kevin’ in state_attr(‘sensor.sm_af520f_last_notification’,‘android.title’) }}

Just noticed that for some reason we have an f in the sensor name that shouldn’t be there, it should be:
sensor.sm_a520f_last_notification
not as we (I) currently have it:
sensor.sm_af520f_last_notification

1 Like

wow, that was literally it. You got a good eye to catch that one.
Thank you so much for helping me out on this thing.

I do notice though, that if i receive 2 whatsapp messages it will only turn on the light on the first message.
Only if i receive a facebook one and then a new whatsapp message it will work again.
Not sure why it wont tigger every time.

1 Like

Because the package has to change TO either the whatsapp or the facebook one. If the last message was from whatsapp and a new message comes in on whatsapp, the package name has not changed, and thus the trigger condition has not been met.

If you want to make it fire on EVERY message. Then you need to remove the to: in the trigger, and instead add it all as a condition.

{{ 'com.whatsapp' in state_attr('sensor.sm_a520f_last_notification','package') or 'com.facebook.orca' in state_attr('sensor.sm_a520f_last_notification','package') }}

that template will replicate what the trigger was doing it, but now it is a condition template.

I should clarify by the way, that you need to remove the attribute too, you want this to trigger on ALL state changes, if you leave the attribute in there, it will only fire on attribute changes, and the same problem will happen, if you get 2 messages from whatsapp, because the attribute won’t change, the automation won’t trigger.

Thank you i will give it a try.

Sorry to ask again,
but what do i put as a trigger now if i remove the attribute and the to:?

  - trigger: state
    entity_id: sensor.sm_a520f_last_notification

Then

  - condition: >
      {% set pk = state_attr('sensor.sm_a520f_last_notification') %} 
      {{ 'com.whatsapp' in pk or 'com.facebook.orca' in pk }}

Should do it.

1 Like

i pasted

{% set pk = state_attr('sensor.sm_a520f_last_notification') %} 
      {{ 'com.whatsapp' in pk or 'com.facebook.orca' in pk }}

in the conditions > condition type > value template
but the app is not showing new messages received.

Yeah sorry hang on the top line should be:

{% set pk = state_attr('sensor.sm_a520f_last_notification','package') %}
1 Like

scratch that
it is working with the following code

{% set pk = state_attr('sensor.sm_a520f_last_notification','package') %}
{{ 'com.whatsapp' in pk or 'com.facebook.orca' in pk }}

for the final wish :stuck_out_tongue: would it now be possible to make it seperate AND per person?
meaning a automation for a specific person on facebook and whatsapp?
would i just need to add another condition like ?

{% set pk = state_attr('sensor.sm_a520f_last_notification','package') %}
{{ 'com.whatsapp' in pk or 'com.facebook.orca' in pk }}
{{ 'Kevin' in state_attr('sensor.sm_a520f_last_notification','android.title') }}

ah solved it by making a 2e condition and putting the name part in there.

Condition 1:

{% set pk = state_attr('sensor.sm_a520f_last_notification','package') %}
{{ 'com.facebook.orca' in pk }}

Condition 2:

{{ 'Kevin' in state_attr('sensor.sm_a520f_last_notification','android.title') }}

You would typically keep the condition as it was, and then in actions use the CHOOSE option. You can then have a set of actions for each CHOOSE condition.

So:
Choose:
condition: {{ ‘Kevin’ in state_attr(‘sensor.sm_a520f_last_notification’,‘android.title’) }}

and then whatever actions you want to perform.
Then you click on “add option”
and add your next person:

condition: {{ ‘Santa’ in state_attr(‘sensor.sm_a520f_last_notification’,‘android.title’) }}

and just keep going.

1 Like

Great let me try that.