Would love to see a TTS implementation

This is what I have, in AppDaemon (but it could easily be done in an automation.yaml)!

When my phone starts my morning alarm, Tasker shoots out an event on the HomeAssistant REST API called external_wakeup. The following is a snippet of the app I use to govern external events.

import appdaemon.appapi as appapi
from datetime import datetime

class ExternalEventsListener(appapi.AppDaemon):

    def initialize(self):
        self.listen_event(self.wakeup, event='external_wakeup')
        ...

    def wakeup(self, event_name, data, kwargs):
        time = datetime.now().strftime('%A, at %-I:%M')
        conditions = self.get_state('sensor.pws_weather')
        precip_chance = data['rain_chance']
        temperature = round(int(float(self.get_state('sensor.pws_feelslike_f'))))

        greeting = ("Good morning! It's currently {}. The weather out is {} with a {} percent "
                    "chance of rain. It is {} degrees outside. It's time to get up!"
                    .format(time, conditions, precip_chance, temperature))

        self.call_service('tts/google_say', entity_id='media_player.bedroom', message=greeting)

So every morning I get a custom greeting that tells me what the current time is and what the weather’s like outside. Things I instinctively check each morning before getting ready for the day.

4 Likes

Sorry to dig up an old thread, but could you please share the exact command you sent via tasker?

I used your code above with the HTTP POST command: http://MYHAADDRESS:8123/api/events/external_wakeup/?api_password=hunter2

But it doesn’t seem to be responding for me.

It looks like you’ve got the correct HASS endpoint. Are you doing a HTTP POST in Tasker?

@aimc - might there be a way to achieve this now with the API endpoints in AppDaemon instead of pushing the event through HASS?

Absolutely - as long as tasker is capable of hitting an arbitary URL path with a post request, and either setting a specific header, or appending URL parameters you can go straight to an app.

@poebae You should be able to get it working with an HTTP POST in Tasker.


I would recommend you check out the below links. It used to be that we had to use Home Assistant as the method of communication between Tasker and AppDaemon, but that is no longer the case and Andrew offers a direct mode of communication, with a slight bit of setup.

http://appdaemon.readthedocs.io/en/latest/APPGUIDE.html#restful-api-support
http://appdaemon.readthedocs.io/en/latest/APIREFERENCE.html#api

1 Like

Yep, I’m using HTTP POST in Tasker.

Thanks for the links. I’ve read through both a bunch of times and still trying to get my head around it. Does the new functionality significantly change the code you pasted above?

I’m trying to use:

self.register_endpoint(self.wakeup, "external_event")

with a Tasker command of HTTP Post:

http://MYHAADDRESS:5050/api/appdaemon/external_event

Does it still require my api_password at the end, or any other information?

Note: I’ve followed these instructions:

The RESTFul API is disabled by default, but is enabled by adding an ad_port directive to the AppDaemon 
section of the configuration file. The API can run http or https if desired, separately from the dashboard.

Am I supposed to reference the port I specified somewhere? I’ve also tried subbing out ‘ad_port’ for ‘api_port’ following someone’s instructions.


EDIT: it seems that the Appdaemon API requires the api_key in the header if one is set up and Tasker requires root/a plugin to do that, so now I’m trying RUN SHELL:

curl -i -X POST -H "x-ad-access: !secret api_password" -H "Content-Type: application/json" 
http://MYHAADDRESS:5050/api/appdaemon/external_event

Still no dice though.

And while I’m here, thanks for all your hard work @aimc and @SupahNoob. I’m new to HA and Python and your examples have really helped kickstart my understanding!

That’s a cool idea. I have Tasker app used before but couldn’t get my head around it. Could you share steps that you created in Tasker app to achieve this?

WIth the API you can use ?api_password= as an alternative to the header.

The URL must specify the port that you configured in the api_port (not ad_port) directive.

e.g., if you have:

appdaemon
...
  api_port: 5000

You need a URL something like:

http://<some address>:5000/appdaemon/api/<endpoint>?api_password=<some password>

+ @sbmlat … so unfortunately, I don’t use either Tasker or AppDaemon anymore - longer story, for another time. :slight_smile:


@poebae what do you get if you flash the %HTTPD variable in Tasker? This will help with debugging so we can tell if you’re getting an HTTP 200 or not. If you are, then something might be wrong in your AppDaemon app coding. If you are getting a 4xx error, then you are not authenticating correctly and should fix that first. Tasker can pass the data, but I do not remember exactly which field it should be passed in.

Let’s figure out the response code first, though! If need be, I’ll finally redownload Tasker to help.

@aimc hmm I tried that and still not getting a response. Is it supposed to be /api/appdaemon/endpoint? I’ve tried both.

I know the app works because I can trigger it by firing the event from the HA backend. It looks like @SupahNoob can help me out though!

@SupahNoob I hadn’t heard of any of those things before so I looked up a guide, but not sure what to do from here.

    def initialize(self):
    self.register_endpoint(self.wakeup, "external_wakeup")

Tasker:     http://192.168.1.18:5555/api/appdaemon/external_wakeup?api_password=PASSWORD

/appdaemon/api

When in doubt, trust the docs not me :wink:

http://appdaemon.readthedocs.io/en/latest/APPGUIDE.html?highlight=appdaemon%2Fapi#restful-api-support

What response data does %HTTPD / %locjson send back? I am looking to confirm that you are getting the proper response. Your code snippets all look fine to me.

With port 5555 I’m getting Socket error: failed to connect.

If I try port 5050 I get 404 not found.

@aimc does the issue in this thread still hold true? Could that be the problem?

It has to be the post you specify with the api_port directive - 5000 in my example above.

Are you using hass.io? If so, the requirements may be different.

Yep, I used the port specified in api_port.

I am using hass.io. Maybe that’s the problem :frowning:

Older versions of hass.io did not support the API - which version are you using of the AppDaemon plugin? And what version of AppDaemon is showing in the AppDaemon logfile?

I’m using:

Home Assistant v0.65.5
hass.io AppDaemon2 add-on v1.1.0
AppDaemon 2.1.12

Is the API support something that was introduced in ADv3? I’ve been following the beta threads and feeling a bit scared to upgrade at the moment.

@frenck Can correct me, but although the API is in 2.1.12, I don’t believe the hass.io supports it until the appdaemon3 addon.

3.0 is now out of beta, so it’s a good time to upgrade :slight_smile:

The latest version of the AppDaemon2 add-on for Hass.io contains AppDaemon 2.1.12 and supports the API of AppDaemon.

IT’S WORKING.

After shelving this project for a while out of frustration, I revisited it today and finally figured it out. What I needed to fix:

  1. Changing the api_port to 5050. It seems to be a hass.io limitation that only one open port is supported, as outlined here by @Mark_Boss. I don’t currently use the Dashboard anyway so I disabled it and hijacked port 5050.

  2. Adding {} to the Data/File section of my Tasker task (or if using curl, -d ‘{}’

  3. Switching /appdaemon/api for /api/appdaemon

So the final command that got it working was:

curl -i -X POST -H "Content-Type: application/json" http://myaddress:5050/api/appdaemon/endpointname?api_password=mypassword -d '{}'
1 Like