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?
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.
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.
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!