[solved] How to trigger apps from script?

Before adopting to Appdaemon I wrote scripts, and as such they would show up in the browser for easy manual triggering by a click on the “ACTIVATE” button.

Now I would like to do the same to easily trigger my apps, can this be done?

The manual[1] talks about firing an event from script so I tried the following:

trigger_hallway_motion:
  alias: "Trigger hallway motion"
  sequence:
    - event: STATE_CHANGED
      event_data:
        entity_id: binary_sensor.aeotec_zw100_multisensor_6_sensor
        old_state: "off"
        new_state: "on"

But this does not trigger my app that uses listen_state() on the same binary_senosor…
(The app works when I physically trigger the sensor by walking in front of it.)

[1] https://home-assistant.io/docs/scripts/#fire-an-event

1 Like

You can write a script that fires events and listen for them inAppDaemon directly with listen_event()

There are examples in the docs - an event is a specific thing in HASS and I don’t think you can force a state change from an automation

Here’s an example:

alias: Night
sequence:
  - event: MODE_CHANGE
    event_data:
      mode: Night

Hmmm… I was inspired by the set_state() in appdaemon. My assumption was that a script would do the same as if I had created a second app that called it like:

set_state("binary_sensor.aeotec_zw100_multisensor_6_sensor", state="on")

Would a second app work? I’m thinking a script that calls the second app with your proposed event MODE_CHANGE… (Everything to not add specific “debug code” in my first app.)

That would probably work, yes, but it’s a lot more complex than just adding a listen_event() to tour existing App :wink:

Agreed, I will probably just add the listen_event for the purpose of triggering from the browser, and try to make it possible to have a dedicated GUI element for each instance of the app.

But I will definitely try the second app approach in order to debug my apps. Thanks!

i use input_boolean to trigger apps like this:

initialize(...):
    listen_state(self.motioncallback,"sensor.motion")
    listen_state(self.motioncallback,"input_boolean.test", state="on")

motioncallback(self,...):
    motion = self.get_state("sensor.motion")
    if self.get_state("input_boolean.test") == "on":
        motion = "on"

    the appcode

    self.turn_off("input_boolean.test")

this way triggering by motion is identical with flipping a boolean on.
set_state could work also, but there are still some problems on the HA side with that.

1 Like

If debugging is your goal, you can use the developer tools in the HASS UI to set the state too.

2 Likes

thx, never noticed that option :wink:

For now I have settled on a combination of input_boolean and template sensors/switches and running my tests and development on a separate machine. This allows me to play around without killing my production server while still using the same App code and configuration. :wink: