Send notification to the current user of the app // button pusher

I upgraded my dashboard with some of my wife’s ideas. One of them is having a dashboard for our vacuum cleaner with the different rooms to clean (as shown in the pic). She thought it was also lacking in having no instant feedback that it would start cleaning the tapped room. So I added a second action to send a notification.

Now the problem
Is there any chance there is a way to send a notification to the current user? The only solution I found yet is to send it to all devices.

I guess I would have a variable to retrieve the current user entity id and add in the action. But I have no clue how to do it and didn’t find anything online.

  - type: "custom:button-card"
    template: card_script
    variables:
      ulm_card_script_title: "Front"
      ulm_card_script_icon: "mdi:storefront"
    tap_action:
      action:
      multi_calls: |
        [[[
          hass.callService(
            "python_script",
            "vacuum_room",
            { area: "front", entity_id: "vacuum.roborock" }
          );
          hass.callService(
            "notify",
            "all_devices",
            { message: "Cleaning now the front"}
          );
        ]]]

I don’t get exactly, what you’re doing with the second message, sorry, but if you want to get more control over your notifications, use notification groups.

You can find it here (very well hidden under groups, with no link to notifications, but you know, the documentation in HA… :rofl:):

Setup a notification group that you call, and in this notification group you collect the receivers of said notification. :slight_smile: Sounds promising but complicated? Here you go with a short example:

notify:
  - name: notification_telegram_x
    platform: telegram
    chat_id: !secret telegram_chat_id_x
  - name: notification_telegram_y
    platform: telegram
    chat_id: !secret telegram_chat_id_y
  - platform: group
    name: notification_level_4
    services: 
      - service: notification_magentatv
        data:
          data:
            duration: 20
            fontsize: large
            position: center
            color: grey
            transparency: 0%
      - service: notification_telegram_x
      - service: notification_telegram_y 
      - service: mobile_app_mi_mix_2_x
      - service: mobile_app_mi_mix_2s_y

Thanks for your reply. In my case, all_entity is already a group.

To answer the second action is to send a direct notification to confirm the vacuum has been activated. Sometimes we are not at home and knowing it started cleaning is important to us.

What I meant is, is there any way to send a notification to change the user device dynamically?

e.g Chris one of those buttons, the confirmation notification is sent to him and no other devices. Same if Chantal does it, it won’t be sent to Chris’s phone.

Node Red can get the user_id of the user that initiated the action from the mobile app:
msg.data.new_state.context.user_id

You can use that to monitor the entities/buttons and send a notification to only that one person. You can get the user_id for each user from Settings - People - Users.

[{"id":"a0cfced78d640e25","type":"tab","label":"Flow 3","disabled":false,"info":"","env":[]},{"id":"6946980ad325cd06","type":"server-state-changed","z":"a0cfced78d640e25","name":"Lamp1","server":"8be923df.d66f9","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.lamp_1_on_off","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":50,"y":80,"wires":[["1b0390a85ab950dc"]]},{"id":"3722cd2da7e82e43","type":"debug","z":"a0cfced78d640e25","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"data.new_state.context.user_id","targetType":"msg","statusVal":"","statusType":"auto","x":590,"y":100,"wires":[]},{"id":"1b0390a85ab950dc","type":"switch","z":"a0cfced78d640e25","name":"Direct each user_id","property":"data.new_state.context.user_id","propertyType":"msg","rules":[{"t":"eq","v":"28720e25df5f4660869fd3e2ef82ed8d","vt":"str"},{"t":"eq","v":"6f60846b24a5405d879e5300c1af9866","vt":"str"},{"t":"eq","v":"55e7aa394ff1490b92d2fb52d4a22959","vt":"str"},{"t":"eq","v":"f407c838ab394452824279908f84a50e","vt":"str"}],"checkall":"false","repair":false,"outputs":4,"x":270,"y":80,"wires":[["750e4f35ad64de3a","64c994e67ee65812"],["750e4f35ad64de3a","adc91ac46df6ab6f"],["750e4f35ad64de3a","f36f1aa4cf31d28f"],["750e4f35ad64de3a"]]},{"id":"750e4f35ad64de3a","type":"change","z":"a0cfced78d640e25","name":"Convert user_id to friendly name","rules":[{"t":"change","p":"data.new_state.context.user_id","pt":"msg","from":"38620e25df5f4660869fd3e2ab82ed8d","fromt":"str","to":"Sam","tot":"str"},{"t":"change","p":"data.new_state.context.user_id","pt":"msg","from":"6f84949b24a5405d879e5300c1af7400","fromt":"str","to":"Elliot","tot":"str"},{"t":"change","p":"data.new_state.context.user_id","pt":"msg","from":"41e7aa394ff1490b92d2fb52d4a302189","fromt":"str","to":"Robin","tot":"str"},{"t":"change","p":"data.new_state.context.user_id","pt":"msg","from":"f362c838ab394452824292000f84a50e","fromt":"str","to":"Supervisor","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":40,"wires":[["3722cd2da7e82e43"]]},{"id":"a6fb370a5fbf5605","type":"api-call-service","z":"a0cfced78d640e25","name":"Notify Sam","server":"8be923df.d66f9","version":5,"debugenabled":false,"domain":"notify","service":"mobile_app_sams_iphone","areaId":[],"deviceId":[],"entityId":[],"data":"{\"title\":\"Home Assistant Alert!\",\"message\":\"{{payload}}\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":590,"y":180,"wires":[[]]},{"id":"64c994e67ee65812","type":"function","z":"a0cfced78d640e25","name":"Set message text","func":"\nmsg.payload=\"Sam turned on Lamp 1. \";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":180,"wires":[["a6fb370a5fbf5605"]]},{"id":"adc91ac46df6ab6f","type":"function","z":"a0cfced78d640e25","name":"Set message text","func":"\nmsg.payload=\"Elliot turned on Lamp 1. \";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":220,"wires":[["c831140d9df45025"]]},{"id":"c831140d9df45025","type":"api-call-service","z":"a0cfced78d640e25","name":"Notify Elliot","server":"8be923df.d66f9","version":5,"debugenabled":false,"domain":"notify","service":"mobile_app_elliots_iphone","areaId":[],"deviceId":[],"entityId":[],"data":"{\"title\":\"Home Assistant Alert!\",\"message\":\"{{payload}}\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":590,"y":220,"wires":[[]]},{"id":"f36f1aa4cf31d28f","type":"function","z":"a0cfced78d640e25","name":"Set message text","func":"\nmsg.payload=\"Robin turned on Lamp 1. \";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":260,"wires":[["a294bff88d3051c6"]]},{"id":"a294bff88d3051c6","type":"api-call-service","z":"a0cfced78d640e25","name":"Notify Robin","server":"8be923df.d66f9","version":5,"debugenabled":false,"domain":"notify","service":"mobile_app_robins_iphone","areaId":[],"deviceId":[],"entityId":[],"data":"{\"title\":\"Home Assistant Alert!\",\"message\":\"{{payload}}\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":590,"y":260,"wires":[[]]},{"id":"577f5d59033edeb7","type":"comment","z":"a0cfced78d640e25","name":"Monitor Lamp 1","info":"","x":80,"y":40,"wires":[]},{"id":"8be923df.d66f9","type":"server","name":"homeassistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"","connectionDelay":false,"cacheJson":false,"heartbeat":false,"heartbeatInterval":""}]
1 Like

I’m also looking for the answer to this question. It is a similar question to Do we have a "sensor.whoami" to determine the current front end user's person or user_id?.

I’m trying to determine the device tracker of the user that clicks a button and initiates a service (spa_controller.py). This is so that the below message can be sent to the user that initiated the service.

notify.mobile_app_xxx(message=“Spa On: Started”, title=“Spa”)

In other words, if the user is Bob. Then send this code in pyscript:

notify.mobile_app_bobsphone(message=“Spa On: Started”, title=“Spa”)

Researched all over the internet and community. Tried two approaches below, but in both cases I cannot find the correct source variable for username (whoami).

#1

service: pyscript.spa_controller
data:
  user: { username }
  
spa_controller.py

	@service
	def spa_controller(username):

		...

		if username == 'bob':
			notify.mobile_app_bobsphone(message="Spa On: Started", title="Spa")  
		else if username == 'jake'
			notify.mobile_app_jakesphone(message="Spa On: Started", title="Spa")  
			
		...		

#2

service: pyscript.spa_controller

spa_controller.py

	@service
	def spa_controller():

		...
	
		user = hass.states.get('username')
		if username == 'bob':
			notify.mobile_app_bobsphone(message="Spa On: Started", title="Spa")  
		else if username == 'jake'
			notify.mobile_app_jakesphone(message="Spa On: Started", title="Spa")  
			
		...