How invoke a app in appdaemon

My purpose is to write a small code that I can call for signaling an alarm. Depending on the message I want to give I want the alarm to beep 3 or 5 or an other amount of times. So I made a small python app:

import appdaemon.plugins.hass.hassapi as hass
from time import sleep
 
class verklikker(hass.Hass):
 
  def initialize(self): 
    self.listen_state(self.alarm_aan,"input_boolean.test", new="on")


  def alarm_aan (self, entity, attribute, old, new, kwargs):
    aantal = self.args["aantal"]
    self.turn_on("switch.alarm")
    for index in range(0,aantal):
      sleep(0.5)
      self.turn_on("switch.alarm")
      sleep(0.5)
      self.turn_off("switch.alarm")

The yaml file looks like this

verklikker3:            #any name you like.
  module: verklikker    #that was the name we used for our py file
  class: verklikker     #and this was the name from our class
  aantal: 3
  
verklikker5:            #any name you like.
  module: verklikker    #that was the name we used for our py file
  class: verklikker     #and this was the name from our class
  aantal: 5

How can I call ‘verklikker5’ instead of verklikker3?

Thanks

First, you should avoid “sleep” in your AppDaemon apps, expect if you are using AppDaemon 4 and async apps.

I don’t understand your use case exactly. Can you explain your end goal?

1 Like

depending on a situation I want to sound an alarm as a notification (1 time) or as a warning (3 times) or as an alert (10 times)

I think the best solution for this would be to change your alarm_aan method to accept a variable, which contains the number of times you want to sound the alarm.
Then in your other apps, that will use the alarm you use the appdaemon method “get_app” to the alarm app, to access the method alarm_aan.

Thanks for your reply.

I will do that.

In your previous message you advised not to use .sleep.
Is there another way to realize an pause of half a second?

Use run_in. From the AppDaemon documentation

Given the above, NEVER use Python’s time.sleep() if you want to perform an operation some time in the future, as this will tie up a thread for the period of the sleep. Instead use the scheduler’s run_in() function which will allow you to delay without blocking any threads.

ok, but I have read that the smallest time interval is 1 second. Is this right?

Yes, this is right. If you really know what your are doing you can use sleep, otherwise it’s strongly suggested to not use it.

You can now call run_in() with as small an interval as you’d like (positive numbers only, of course), including “0” which means, run_in()… right now!

1 Like

Niiiice, didn’t know about this. Must have missed that change. Is this already available in 3.0.5 or only in 4?

@Burningstone,

Only in 4.0 can you use floats.

Regards

1 Like