zachowj / node-red-contrib-home-assistant-websocket

Node-RED integration with Home Assistant
https://zachowj.github.io/node-red-contrib-home-assistant-websocket/
MIT License
501 stars 93 forks source link

calendar.list_events: Service call requires responses but caller did not ask for responses #972

Closed schwickster closed 10 months ago

schwickster commented 1 year ago

Describe the bug

Hey,

Currently when calling calendar.list_events service it's suppose to return data. However the error that's returned is

"Call-service error. Service call requires responses but caller did not ask for responses"

This feature is new since HA 2023.7.0. Looks like Nodered HA node does not have this feature yet. Would love this to be added.

To Reproduce

  1. Add call service
  2. Use domain calendar
  3. use service list_events
  4. Add start_date_time and end_date_time to data
  5. Call service inside nodered

Expected behavior

Array of event from calendar

Screenshots

No response

Example Flow

[{"id":"8a29a312e1130358","type":"api-call-service","z":"b8e8781a.debe28","name":"","server":"fdf82e47.5cab1","version":5,"debugenabled":false,"domain":"calendar","service":"list_events","areaId":[],"deviceId":[],"entityId":["calendar.stefanie_mark"],"data":"{\t   \"start_date_time\":\"2022-07-01 20:00:00\",\t   \"end_date_time\":\"2022-07-22 22:00:00\"\t}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[{"property":"data","propertyType":"msg","value":"","valueType":"msg"}],"queue":"none","x":450,"y":1360,"wires":[["39a0dc31f2379cfa"]]},{"id":"39a0dc31f2379cfa","type":"debug","z":"b8e8781a.debe28","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":640,"y":1360,"wires":[]},{"id":"5dc7ff9f2fb23d95","type":"inject","z":"b8e8781a.debe28","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":1360,"wires":[["8a29a312e1130358"]]},{"id":"fdf82e47.5cab1","type":"server","name":"Home Assistant","version":5,"addon":false,"rejectUnauthorizedCerts":false,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

This package's version (not the Home Assistant add-on version)

0.51.0

Is Node-RED running in Docker?

Yes

Node-RED version

3.0.2

Node.js version

16.16.0

Additional context

No response

zachowj commented 1 year ago

handle_call_service doesn't support response data, which is the websocket endpoint that NR uses. This would need to be added to Home Assistant or NR changed to use the handle_execute_script endpoint.

In the meantime, you can use the API node to make a call to the 'excute_script' endpoint.

{
   "type": "execute_script",
   "sequence": [
       {
           "service": "calendar.list_events",
           "data": {
               "start_date_time": "2023-07-09 00:00:00",
               "end_date_time": "2023-07-15 00:00:00"
            },
           "target": {
               "entity_id": [
                   "calendar.events"
                ]
            },
           "response_variable": "service_result"
        },
       {
           "stop": "done",
           "response_variable": "service_result"
        }
    ]
}
bfmorgan commented 1 year ago

This helped and its working. However, when I try to use {Template function and try:

This is the payload: {{payload.context.id}} ! The locations are {{payload.response.events[1].location}}

The payload.context.id returns a value, but payload.response.events[1].location does not resolve to a value. It should be:

{ "context":{ "id":"01H4RQK3F1QZM9740W99SQY1MD", "parent_id":null, "user_id":"4409938ad8da43f392aec7d5d1c50d6f" }, "response":{ "events":[ { "start":"2023-07-08T09:00:00-07:00", "end":"2023-07-08T10:00:00-07:00", "summary":"OA", "description":" \r\n", "location":"Band Room K1" },

zachowj commented 1 year ago

payload.response.events[1].location

Arrays are zero base. payload.response.events[0].location

bfmorgan commented 1 year ago

This is the output with

This is the payload: {{payload.context.id}} ! The locations are {{payload.response.events[0].location}}

in the Template

This is the payload: 01H4S1YQSSMWYGMS35RV1XVKN4 ! The locations are

bfmorgan commented 1 year ago

This is my flow.

[{"id":"977c23b2e096b041","type":"tab","label":"Test Sand Box","disabled":false,"info":"","env":[]},{"id":"704b39077b155cb1","type":"inject","z":"977c23b2e096b041","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":70,"y":580,"wires":[["ee13db2c6ee8b4e5"]]},{"id":"ec6aa564089e342d","type":"debug","z":"977c23b2e096b041","name":"debug 4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":580,"y":580,"wires":[]},{"id":"ee13db2c6ee8b4e5","type":"ha-api","z":"977c23b2e096b041","name":"","server":"863c96d1.fe1118","version":1,"debugenabled":false,"protocol":"websocket","method":"get","path":"","data":"{\"type\":\"execute_script\",\"sequence\":[{\"service\":\"calendar.list_events\",\"data\":{\"start_date_time\":\"2023-07-08 00:00:00\",\"end_date_time\":\"2023-07-09 00:00:00\"},\"target\":{\"entity_id\":[\"calendar.journeychurchitapp_gmail_com\"]},\"response_variable\":\"service_result\"},{\"stop\":\"done\",\"response_variable\":\"service_result\"}]}","dataType":"json","responseType":"json","outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"results"}],"x":350,"y":500,"wires":[["ec6aa564089e342d","475bbab28154092c"]]},{"id":"475bbab28154092c","type":"template","z":"977c23b2e096b041","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"This is the payload: {{payload.context.id}} ! \nThe locations are {{payload.response.events[0].location}}","output":"str","x":700,"y":500,"wires":[["bd41b56d53ad33a1"]]},{"id":"bd41b56d53ad33a1","type":"debug","z":"977c23b2e096b041","name":"debug 7","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":980,"y":500,"wires":[]},{"id":"863c96d1.fe1118","type":"server","name":"Home Assistant","addon":true}]

I'm running HA 2023.07.1 with node-red 14.3.0

zachowj commented 1 year ago

@bfmorgan payload.response.events.0.location

bfmorgan commented 1 year ago

I have tried using mustache template for the start_date_time, like {{config}} or {{payload}} and they are not accepted even though the API doc states Data accepts Mustache Templates.

github-actions[bot] commented 1 year ago

This issue has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Gvolten commented 1 year ago

This needs to be implemented, because the new way of getting weather forecasts is through service responses. The old way is deprecated, and will be removed in Home Assistant Core 2024.3.

misterx8 commented 1 year ago

The problem is not just for weather forecasts. In reality, many services are being developed, both custom and resulting from integration, which provide a direct response. I think the implementation is a must, otherwise the call service block, which is fundamental in my opinion, will become unusable

WShadowborn commented 1 year ago

As mentioned you can use the API node however does fundamentally change many workflows starting to move away from the call service node so does need to be updated to accommodate this.

Gvolten commented 1 year ago

Well if anything can be done with the API node, are all home assistant nodes obsolete? No. They provide a quick and user friendly way to interact with the API. This obviously needs to be implemented.

zachowj commented 11 months ago

Looks like responses were added to call_service endpoint.

https://github.com/home-assistant/core/commit/618b666126a885bfb0ae44571bec0c44ff5bdada

sgurgul commented 10 months ago

I upgraded to 0.60 but somehow cannot see how to access responses from output messages.

Here is what I get from direct API call - this is expected outcome: image

But when I call call_service node with the same setting I get two output messages - none of them containing expected results: image

Is there some way I should configure call_service node to access these newly added results ?

zachowj commented 10 months ago

@sgurgul Are you running Home Assistant 2023.12+?

sgurgul commented 10 months ago

Yes:

I tried full OS reboot to check if it help but it did not change anything

zachowj commented 10 months ago

@sgurgul You'll need to use the output properties to set the results to a property of the message object.

image

If that doesn't solve your issue please open a different issue. As it would be unrelated to this one.

sgurgul commented 10 months ago

Yes, I confirm. That was the missing piece on my side. Setting output property gives me access to results. Thank you.

fherreror commented 10 months ago

I have the same problem, but trying to get Todo items. However, I don't see "results", but only "sent data". I have nodered, noderedcompanion and HA updated to last version. Is there something I am missing?

zachowj commented 10 months ago

@fherreror If you don't see "results" as an option in output properties I would double-check the version you're running.

fherreror commented 10 months ago

@fherreror If you don't see "results" as an option in output properties I would double-check the version you're running.

The thing is that, theoretically, everything is up to date:

`Add-on: Node-RED Flow-based programming for the Internet of Things

Add-on version: 16.0.2 You are running the latest version of this add-on. System: Home Assistant OS 11.2 (aarch64 / raspberrypi4-64) Home Assistant Core: 2023.12.1 Home Assistant Supervisor: 2023.11.6 `

`Welcome to Node-RED

14 Dec 11:20:46 - [info] Node-RED version: v3.1.0 14 Dec 11:20:46 - [info] Node.js version: v18.18.2`

The only thing that may be outdate is node-red-contrib-home-assistant-websocket, which is in v 0.59.0. However I cannot update it, as it requires nodered v 3.1.1, which is higher that the one used by the addon.

These are the output options available:

imagen I assumed that "data sent" whould return the data sent by the service, but it throws the same error.

I don't know what else to do in order to get de data sent by services

sgurgul commented 10 months ago

@fherreror If you update websocket node to version 0.60 (not higher) than it will work - you will get a "result" object available in NR containing results of call-service.

fherreror commented 10 months ago

@fherreror If you update websocket node to version 0.60 (not higher) than it will work - you will get a "result" object available in NR containing results of call-service.

Thanks. I have updated to 0.60.1, but then all my nodes with data field empty show error. This bug has been fixed in 0.60.1 I think, which is the version I theoretically have installed. Anyway, I think I will wait until addon updates :(

sgurgul commented 10 months ago

From my experiments, 0.60.0 is the last version to be used with current HA & NR. If you go with newer you will expect problems - either data field empty error (with 0.60.1) or incompatible NR version (0.61.* and newer).

On the other side, 0.60.0 works OK and it gives access to call-service results. It is also fully backward compatible - I did not need to change anything in my existing flows.

snzyeee commented 10 months ago

@sgurgul Hey, can you please tell me how to upgrade only to 0.60.0?

fherreror commented 10 months ago

@sgurgul Hey, can you please tell me how to upgrade only to 0.60.0? Download from here: https://registry.npmjs.org/node-red-contrib-home-assistant-websocket/-/node-red-contrib-home-assistant-websocket-0.60.0.tgz Then in nodered, instead of update package, go to install tab, and there is a buton to upload a package. Select the downloaded file and restart node red. But make a backup before, in case you need to restore.

From my experiments, 0.60.0 is the last version to be used with current HA & NR. If you go with newer you will expect problems - either data field empty error (with 0.60.1) or incompatible NR version (0.61.* and newer).

On the other side, 0.60.0 works OK and it gives access to call-service results. It is also fully backward compatible - I did not need to change anything in my existing flows.

Thanks, I will try 0.60.0

sgurgul commented 10 months ago

The binary of the speciic version of the node you can download from the nmpjs.org page. For 0.60.0 it would be like that: https://registry.npmjs.org/node-red-contrib-home-assistant-websocket/-/node-red-contrib-home-assistant-websocket-0.60.0.tgz

Once you have it you should do following:

Installation can be done in "manage palette" screen, "Install" tab - there is a button called "upload module tgz file". image

fherreror commented 10 months ago

0.60.0 works nice. thank you

snzyeee commented 10 months ago

Confirm working with 0.60.0

Thanks aswell for this detailed step by step tutorial.

isvec commented 10 months ago

Thank you @sgurgul, this was precisely what I needed to access my weather.get_forecasts data from the meteostation. Kudos!