Node-red-contrib-home-assistant-websocket

At this point I don’t have to many ideas. I’m assume both docker containers are running the same machine so them losing a connection to each other really isn’t possible.

Does rode-red itself seem unresponsive when this happens or just the HA nodes? If you try and run a call-service node when it’s in this state does it go through? Same with a get-template node?

[{"id":"9e66f282.eef6","type":"inject","z":"ffbd7f06.4a014","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":428,"y":848,"wires":[["4c27dcd.649ff24"]]},{"id":"4c27dcd.649ff24","type":"api-call-service","z":"ffbd7f06.4a014","name":"turn on light","server":"2dad33ee.42bf5c","service_domain":"light","service":"toggle","data":"{\"entity_id\":\"light.light1\"}","mergecontext":"","output_location":"","output_location_type":"none","x":598,"y":848,"wires":[["5e7c158a.53330c"]]},{"id":"5e7c158a.53330c","type":"debug","z":"ffbd7f06.4a014","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":774,"y":848,"wires":[]},{"id":"7d3de24f.8a109c","type":"inject","z":"ffbd7f06.4a014","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":428,"y":896,"wires":[["715374a4.b3d23c"]]},{"id":"715374a4.b3d23c","type":"api-render-template","z":"ffbd7f06.4a014","name":"sun state","server":"2dad33ee.42bf5c","template":"{{states.sun.sun.state}}","x":588,"y":896,"wires":[["ee4fbbc4.540648"]]},{"id":"ee4fbbc4.540648","type":"debug","z":"ffbd7f06.4a014","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":774,"y":896,"wires":[]}]

Well, it was a networking issue in the end. I am running the HA docker in “host” networking, while the NodeRed docker was running in Bridge. I did some more trial and erroring, and set the Node-Red docker to Host networking as well, this seems to have fixed the problem, and everything stays connected(thus far, 12 hours later… :slight_smile:)

Thanks for helping me out though!

Similar thing happened to me as well. HA and NR are both on bridge. The IP I used for HA on NR however was the docker 17.n.n.n IP. It would work for a few minutes and then would get ‘not connected’ errors for the same flow. I then changed the HA IP in NR to use host:8123 and it worked better. Weird thing is it only happened on entities connected via mqtt.

New Release v0.10.1

Bug Fixes

  • api: Fix for saving locationType (bd20bfa)

  • call-service: Fix for having undefined output location and type (3a0d8b0)

  • wait-until: fixed scope for setContextValue (3fdfa27)

Features

  • api: Allows input to set/override config values (7296cd2), closes #100

  • Able to reference entity in comparators (ece176b)

  • api: Output type added to http api: binary, json,txt (9305c7d)

  • events-all: Added states_loaded and services_loaded (b8097b8)

  • events-all: Will includes HA client events (ccf9fbc), closes #75

  • get-entities: Added total count of entities as an output option (3061151)

  • render-template: Added custom outputs (66d504d)

Full Changelog


Buy me a coffee For those that enjoy my work.

2 Likes

Hi @Kermit, thanks a lot for your great work! Could you maybe explain a little bit how to use the states_added event? Because I added states_loaded to the Event Type field of an events: all node, but I cannot get it to work. The node doesn’t seem to filter and spits out all events :frowning:

A new event_type, home_assistant_client, was added for client events, the node-red home assistant module, these include:

  • connecting - when trying to connect to HA
  • connected - after the authorization has been accepted
  • disconnected - when the socket stops attempting to connect or disconnected after having been connected
  • error - when a disconnect happens of the websocket with an error
  • states_loaded - the first time all states are loaded from HA
  • services_loaded - the first time all services are loaded from HA

[{"id":"959319c4.b4ed08","type":"server-events","z":"5eb3594f.d294b8","name":"","server":"ef067c6f.620e6","event_type":"home_assistant_client","x":328,"y":704,"wires":[["ced91c7f.a3e17"]]},{"id":"ced91c7f.a3e17","type":"debug","z":"5eb3594f.d294b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":546,"y":704,"wires":[]}]
1 Like

Thank you so much! I must have completely missed that somehow :grinning: Got it up and running now perfectly!

Thumbs up on making the template node’s outputs customizable!

I’m having an issue with the call-service node where, even though the Service is hard-coded (‘turn off’), it seems to be inheriting payload.service (e.g. from a previous call-service execution). Is there any chance this could be a bug with the call-service node implementation, wherein it prioritizes the inheritance of payload.service over a hardcoded Service value?

The issue I’m running into -

Capture

Overwriting “hard-coded” config values of a node is sort of a node-red paradigm. In the “Turn Garage Lights On” call-service node set the Output Location to something other than msg.payload or None if you don’t really care about the data.

Hi @Kermit ant thanks for adding new features to this node!

states_loaded and services_loaded will not be shown after each restart, but only the first time HA will start from a new installation, right?

Thanks

@Kermit Hi,

Love the addon, have a really big bug/feature request (haha, unsure which one it is),
“Current_State” node cannot get entity_id from msg.payload and has to be hardcoded :frowning: (getting ‘value cannot be empty’ error if I leave it empty, and putting {{payload}} or msg.payload doesn’t work)

is there any way to push the entity_id from the payload?

thanks

As version 0.10.2 states_loaded and services_loaded will fire each time a successful connection is made to Home Assistant. When a connection is made the module requests all states and services from HA and when it has receives them it will fire the events.

If you leave the entity id field empty and pass in msg.payload.entity_id this should work.

[{"id":"5a4d366e.cf57f8","type":"api-current-state","z":"5eb3594f.d294b8","name":"","server":"ef067c6f.620e6","outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":432,"y":480,"wires":[["52d2042f.08a0ac"]]},{"id":"1fb2a9e6.981176","type":"inject","z":"5eb3594f.d294b8","name":"","topic":"","payload":"{\"entity_id\": \"sun.sun\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":274,"y":480,"wires":[["5a4d366e.cf57f8"]]},{"id":"52d2042f.08a0ac","type":"debug","z":"5eb3594f.d294b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":614,"y":480,"wires":[]}]

@Kermit I’was only getting the connected state and no loaded ones.

I just noticed the new upgrade to 0.10.2 and now it’s working! Thanks

@Kermit
Its not working for me, I’m getting the entity_id from a “current_state” before this one, like this:

Summary

[{
“id”: “8741c85.f218d38”,
“type”: “tab”,
“label”: “Remove Head Test”,
“disabled”: false,
“info”: “”
},
{
“id”: “fff8a14a.6627a”,
“type”: “inject”,
“z”: “8741c85.f218d38”,
“name”: “”,
“topic”: “”,
“payload”: “”,
“payloadType”: “date”,
“repeat”: “”,
“crontab”: “”,
“once”: false,
“onceDelay”: 0.1,
“x”: 93.53969192504883,
“y”: 116.79364013671875,
“wires”: [
[
“51930236.4de0fc”
]
]
},
{
“id”: “bd5a42d9.89781”,
“type”: “debug”,
“z”: “8741c85.f218d38”,
“name”: “”,
“active”: true,
“tosidebar”: true,
“console”: false,
“tostatus”: false,
“complete”: “payload”,
“targetType”: “msg”,
“x”: 1121.71435546875,
“y”: 170.42857360839844,
“wires”:
},
{
“id”: “51930236.4de0fc”,
“type”: “api-current-state”,
“z”: “8741c85.f218d38”,
“name”: “”,
“server”: “2757cc6a.31a344”,
“outputs”: 1,
“halt_if”: “”,
“halt_if_type”: “str”,
“halt_if_compare”: “is”,
“override_topic”: false,
“entity_id”: “input_text.currenthead”,
“state_type”: “str”,
“state_location”: “payload”,
“override_payload”: “msg”,
“entity_location”: “data”,
“override_data”: “msg”,
“x”: 329,
“y”: 204,
“wires”: [
[
“25efe6a0.75759a”
]
]
},
{
“id”: “bf79784b.05e2f8”,
“type”: “api-current-state”,
“z”: “8741c85.f218d38”,
“name”: “”,
“server”: “2757cc6a.31a344”,
“outputs”: 1,
“halt_if”: “”,
“halt_if_type”: “str”,
“halt_if_compare”: “is”,
“override_topic”: false,
“entity_id”: “”,
“state_type”: “str”,
“state_location”: “payload”,
“override_payload”: “msg”,
“entity_location”: “data”,
“override_data”: “msg”,
“x”: 841,
“y”: 234,
“wires”: [
[
“bd5a42d9.89781”
]
]
},
{
“id”: “25efe6a0.75759a”,
“type”: “function”,
“z”: “8741c85.f218d38”,
“name”: “”,
“func”: “var entity_id = msg.payload;\nmsg.payload = "{\"entity_id\":\"input_text."+entity_id+"\"}";\nreturn msg;”,
“outputs”: 1,
“noerr”: 0,
“x”: 595.5,
“y”: 215,
“wires”: [
[
“bf79784b.05e2f8”,
“9ee3fb1b.5ccdb8”
]
]
},
{
“id”: “9ee3fb1b.5ccdb8”,
“type”: “debug”,
“z”: “8741c85.f218d38”,
“name”: “”,
“active”: true,
“tosidebar”: true,
“console”: false,
“tostatus”: false,
“complete”: “false”,
“x”: 794.5,
“y”: 132,
“wires”:
},
{
“id”: “2757cc6a.31a344”,
“type”: “server”,
“z”: “”,
“name”: “Home Assistant”,
“legacy”: false,
“hassio”: true,
“rejectUnauthorizedCerts”: true,
“ha_boolean”: “y|yes|true|on|home|open”,
“connectionDelay”: true
}
]


in the function (json is escaped in code):

var entity_id = msg.payload;
msg.payload = “{"entity_id": "input_text.”+entity_id+“"}”;
return msg;

still getting
““value” is not allowed to be empty”

Use

msg.payload = {
    entity_id: `input_text.${msg.payload}`
};
return msg;

or

const entity_id = msg.payload
msg.payload = {
    entity_id: "input_text." + msg.payload
};
return msg;
1 Like

awesome, it works!

thanks!

Hi @Kermit,

Sorry if it has been asked before but do you really need to use get template before a message call service in order to put that state into a message?

Example:

    {
    "id": "f4b7b8b1.d0c9c",
    "type": "api-call-service",
    "z": "1325b9db.a8ecb6",
    "name": "Message",
    "server": "e5e437c7.ff14c",
    "service_domain": "notify",
    "service": "michael",
    "data": "{\"message\":\"Airco will turn off in {{ states.input_number.airco_slider.state | int }} minutes \"}",
    "mergecontext": "",
    "output_location": "payload",
    "output_location_type": "msg",
    "x": 220,
    "y": 60,
    "wires": [
        [
            "e84c7d7.4f3058"
        ]
    ]
}
[{"id":"1325b9db.a8ecb6","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"f4b7b8b1.d0c9c","type":"api-call-service","z":"1325b9db.a8ecb6","name":"Message","server":"e5e437c7.ff14c","service_domain":"notify","service":"michael","data":"{\"message\":\"Airco will turn off in {{ states.input_number.airco_slider.state | int }} minutes \"}","mergecontext":"","output_location":"payload","output_location_type":"msg","x":220,"y":60,"wires":[["e84c7d7.4f3058"]]},{"id":"e84c7d7.4f3058","type":"debug","z":"1325b9db.a8ecb6","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":370,"y":60,"wires":[]},{"id":"a55342d8.f7c7b8","type":"inject","z":"1325b9db.a8ecb6","name":"Inject","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":60,"wires":[["f4b7b8b1.d0c9c"]]}]

No, you can access states directly from the call-service node. Review the info panel of the call-service node for a more detailed explanation.

make the data field of the call-service node

{"message":"Airco will turn off in {{states.input_number.airco_slider}} minutes "}
2 Likes

Thanks you helped me into the right direction!
I have been using states.input_number.airco_slider.state instead of states.input_number.airco_slider