A notify function that does more then just 1 notify

if you are using appdaemon you maybe also want to send some notifies sometimes.
and for that we normally have 2 options:

  1. self.notify(“blabla”,name=“a_notifier”)
  2. self.call_service(“notify/a_notifier”,message=“blabla”)

they are both easy to use, and you can even create a general function in a general app to do more then that.
but then you need to import that general function or use self.get_app(“general_functions”)

so you can probably call that the 3th option.

i thought of a 4th option to use.
i created a sensor for notifies. and an app to go with that.

import appdaemon.appapi as appapi


class notify(appapi.AppDaemon):

  def initialize(self):
    self.set_state("sensor.notify_message",state=" ")
    self.listen_state(self.send_notify,"sensor.notify_message")

  def send_notify(self, entity, attribute, old, new, kwargs):
    self.call_service("notify/pushetta",message=new)
    self.log(new)
    ... do any action you like when a meassage is send, send messages to several notifiers
    ... use tts to hear the message, translate the message, anything you can think off

you probably think: “hmm, why use that?” or maybe “how would i use that?”

very simple in every app you can now use:

self.set_state("sensor.notify_message", state="blabla")

just 1 line and if you are gonna use another notifier? just edit the notify app and not all the app you created.
extra bonus: in your HA frontend you can always see your last notify.
want in in a dashboard? just put the sensor in the dashboard and you always directly can see what the last notify was.

is 1 type not enough?
create as many notify functions as you like by just changing the sensor name.
do you find it confusing that its a sensor? call it my_notify.message

i hope this gets your brain going.

4 Likes

Very good approach but in my case on the frontend, it didn’t retain value.

I assume that’s a template sensor.

Ex:

- platform: template
  sensors:
    apd_notify_message:
      value_template: '{{value}}'
      friendly_name: "AppDaemon Notify Message"

I used the following snippet. I did get the notification via IFTTT but from HA/States I didn’t find the value of this sensor there.

notify.py

import appdaemon.appapi as appapi

class notify(appapi.AppDaemon):

  def initialize(self):
    self.set_state("sensor.apd_notify_message",state=" ")
    self.listen_state(self.send_notify,"sensor.apd_notify_message")

  def send_notify(self, entity, attribute, old, new, kwargs):
    self.log("Updated Sensor reading - Value : " + new)
    self.call_service('ifttt/trigger', event='SR_Home', value1=new)

From my hello app:

self.set_state("sensor.apd_notify_message", state="testing1234567")

Log file:

2017-11-21 16:54:38.071366 INFO notifymessage: Updated Sensor reading - Value : testing1234567
2017-11-21 16:54:38.091301 INFO notifymessage: Updated Sensor reading - Value :

the problem is that you created a sensor in HA.
the app creates the sensor itselve if it doesnt exist.
a template sensor depends on a template. setting that value is a problem.

so remove the template sensor from HA and you are good to go.

1 Like

Wow. I didn’t know that AppDaemon creates the sensor for you. That is so cool.

I changed the code accordingly and it worked flawlessly. Thank you for sharing this snippet. :clap:

1 Like

you can do even more with it if you like.
you can edit and add attributes.

so you can change your code so, that in the sensor attributes you could always see the last 10 messages.
and you could add a time attribute to see the time from the last message
and that you can use to add times to the last 10 message attributes.

how to:

thistime= timestring
self.set_state("sensor.notify_message", state="blabla", attributes={"last_time":thistime})
last_message = self.get_state{"sensor.notify_message")
last_message_time = self.get_state{"sensor.notify_message","last_time")
self.set_state("sensor.notify_message", state="blablabla", attributes={"last_time":thistime, "history1":last_message_time + " " + lastmessage})
1 Like

Thank you for that tip. I modified the function and utilized multiple attributes.

  def send_notify(self, entity, attribute, old, new, kwargs):

    self.log("Updated Sensor value : " + new)
    # always send IFTTT notification
    self.call_service('ifttt/trigger', event='SR_Home', value1=new)

    tts_trigger = self.get_state("sensor.appd_notify_message","announce")
    self.log("Announce value : {}".format(tts_trigger))
    if tts_trigger:
        self.call_service('tts/google_say', entity_id='media_player.living_room_home', message=new)

    ha_trigger = self.get_state("sensor.appd_notify_message","frontend")
    self.log("Frontend value : {}".format(ha_trigger))

    if ha_trigger:
        self.call_service('persistent_notification/create',
            title="Attention", message=new+ " - " + datetime.now().strftime('@ %-I:%M %p') )

From other app:

self.set_state("sensor.appd_notify_message", state="Hello Teddy Good afternoon ", attributes={"announce":False, "frontend":True})

2 Likes

use atrributes as args!
cool!

Would you mind sharing or uploading a complete notify app for appdaemon? And how to integrate this in has to add automations. My idea us to show messages in the dashboard sent by homeassistsnt

i dont think you need an app for that.
i think that what you want is just a template sensor.

So it would be possible to show notifications in dashboard? How would I achieve this Master?

My little project: LCD Display with MQTT and Home-Assistant notifies

lol.
what message would you like to see in the dashboard?

something like this?

sensor:
  - platform: template
      temp_message:
        friendly_name: temp
        value_template: '{%- if states.sensor.temp>25 -%}{{ "wow, it i quite hot here" }}{%- else -%}{{"brrrr, maybe i should put on the heating, because its not really warm now."}}{%- endif -%}'
        entity_id:
          - sensor.temp

Wow… Thanks that helps a lot

that MQTT topic isnt that actually a sensor? (sorry, master has never used MQTT :wink: )

I have an automation which publishes all sensors in has to a topic … I can then listen to thus topic with my LCD display

cant you listen with HA to that topic?

The display is mounted in an ESP… This one is running easyesp with mqtt listener .

i understand that.
but you can listen to MQTT with homeassistant, cant you?
so instead of:

sensor > MQTT > homeassistant you have
homeassistant > MQTT > ESP

but then you can also

homeassistant > MQTT > homeassistant > Dashboard

u r right :slight_smile: will see how I will do that :slight_smile:

Hey @ReneTode , are you still doing this method? Did it work out long-term? I ask because I’m thinking I might do this.