[RestAPI] How to use actions

Hi,

I am trying to access and execute available actions through the RestAPI.
Unfortunately there seems to be no endpoint which exposes the registered actions.
Am I missing something?

Also under the VOICE endpoint there is no way to trigger a SAY command.
Was this forgotten or is this intentional?

Linkback

I suspect the two are related as the SAY command is an Action. Until very recently, Actions were still OH 1.x and maintained in the openhab1-addons repo. A lot of work needs to be made, including creating REST endpoints, for them and other parts of OH (e.g. Persistence configuration).

There is also a new way to do Actions for 2.x bindings where the Actions are now a part of the bindings (see MQTT 2) and to use them in Rules they have to be imported. See https://www.openhab.org/addons/bindings/mqtt.generic/#rule-actions. I’d not be surprised if there is no REST end point for these either.

While I know why you want these end points, I doubt that Actions as a concept have ever been considered to be used from anywhere but the Rules engines built into OH.

Exactly - or at least that was my impression.

Thank you for your quick reply - I have created two issues to track this since I believe this should be part of the Rest API provided by the core.

Were these issues ever solved?
Also looking for ways to call actions via rest api.

Here is the issue in case anyone is looking for it. https://github.com/openhab/openhab-core/issues/926

No - I use items mapped to channels instead of actions or use corresponding python libraries instead of bindings.

@Spaceman_Spiff, has there been any movement on getting actions incorporated into the REST API? I am waiting on converting about 1/3 of my Jython rules to HABapp because they are pretty heavy on action calls. (Mostly notification and Voice) I moved over to HABapp because of the threat of JSR233 dying on the vine (Jython anyways), but the HABapp docs say to make proxy items and JSR233 rules to make the action calls, which seems pretty circular, so for now I think I’m just going to keep my Jython rules until there’s an action-able (see what I did there?) solution. Looking forward to being 100% HABapp eventually though! Thank you for all your efforts.

In the mean time, one could implement the action by sending the arguments as json through MQTT, and create a rule on openhab’s side using rulesdsl, or something else. It would be a generic one that can invoke any built in actions or actions from any binding. It would work well for actions that do not return values.

For Transformation, Ephemeris, etc, using REST would probably be better since you’ll need the action’s return value.

I was thinking you could probably create a “HabAppBridge” add-on that facilitates this, amongst others.

Yes - it’s a bad workaround but that’s the only thing that’s supported out of the box atm.

What you always can do ist make the REST call directly. E.g. in the API explorer look at the actions and then create a rule that does make these calls.
You can use the self.async_http.get which provides an aiohttp wrapper.

It’s working on the openHAB side (or at least the endpoints are there).
However I still have to come up with a good concept how I can type hint the actions so that you get auto-complete and checks in your IDE.
Unfortunately my life is currently very busy, so this is something that will not come before summer, probably even later.

That would work or just use a proxy item and send a json there. No need to go over MQTT.

Or just use an existing python library for your notification service and skip the action altogether :wink:

Thanks for the very complete response. I didn’t realize that actions were available via the API. I’ll take a look at the API tool and figure out if making http calls to API would simplify my code at all. And, I’m looking forward to it being available in HAPapp, whenever that might be. If you need any help testing it, prior to all they type-hints being complete, let me know.

1 Like

@Spaceman_Spiff I’ve run into another roadblock with my attempt to use actions from HABApp. I am trying to figure out the correct payload or body for an Action call via the REST API Explorer. I can’t find any examples in the forums, maybe because it’s a seldom used feature. What I’m trying to do is access the Astro binding actions. In jython this is how I would do it:

sunActions  = actions.get("astro","astro:sun:local")
currentElev  = sunActions.getElevation(zdt_now()).floatValue()

This works fine and returns the current Elevation of the Sun. However I can’t figure out the appropriate way to call this in the API explorer. What I have tried is:

In the API explorer I first do GET /actions/{thingUID} where thingUID = astro:sun:local
This returns a big JSON block with all of the available actions. The one I want is:

  {
    "actionUid": "astro.getElevation",
    "label": "get the elevation",
    "description": "Get the elevation for a given time.",
    "inputs": [
      {
        "name": "date",
        "type": "java.time.ZonedDateTime",
        "label": "Date",
        "description": "Considered date",
        "required": false,
        "tags": [],
        "reference": "",
        "defaultValue": ""
      }
    ],
    "outputs": []
  }

Then I try a POST /actions/{thingUID}/{actionUID} where thingUID = astro:sun:local and actionUID = astro.getElevation. In the body I’ve tried everything I can think of, but I can’t figure out how to properly pass it the java.time.ZonedDateTime that it is apparently looking for. I have tried:

{
  "additionalProp1": "2019-09-03T15:22:33.650-05:00",
  "additionalProp2": {},
  "additionalProp3": {}
}

This Returns 200 OK and {} in the response body

  {
    "actionUid": "astro.getElevation",
    "label": "get the elevation",
    "description": "Get the elevation for a given time.",
    "inputs": [
      {
        "name": "date",
        "type": "java.time.ZonedDateTime",
        "label": "Date",
        "description": "Considered date",
        "required": false,
        "tags": [],
        "reference": "",
        "defaultValue": "2019-09-03T15:22:33.650-05:00"
      }
    ],
    "outputs": []
  }

This Returns 200 OK and {} in the response body

{
  "date": "2019-09-03T15:22:33.650-05:00"
}

This Returns 200 OK and {} in the response body

Basically I’m just guessing and I don’t know how to input a time properly so that it returns the Elevation of the Sun at that given time. Any help is greatly appreciated!