Node-red-contrib-home-assistant-websocket

Hello,
Thanks you for providing this.
I am using hass.io addon from Frenck. It is not using upgraded. So, I have tried this yet. I don’t understand what this new feature does. Can you give an example?
GV

Can the wait_until node work with multiple entities? As in, I want one out of 3 sensors to go on, and as soon as one of them goes on, the automation continues. Is it possible to do this?

You can put this 3 sensors in a group an wait until this group turns on.

I guess so.

It would be cool to have a node that could do this automatically though, or update the wait_until node to support multiple entities.

New Release 0.21.0


The sensor node has been renamed to entity and a new type switch has been added. Requires the NR custom component version 0.4.x or newer.
Some of the possible uses for the switch entity:

  • Start a flow using the service call nodered.trigger. This can be used in any place that you can do a service call (e.g. Node-RED, lovelace). When a switch is triggered the output path will depend on if the HA switch is on (top) or off (bottom).
  • As a switch node where the condition is if the HA switch is on/off.

Sample Flow using the new switch entity:
image

[{"id":"b1ddb7a.6547548","type":"inject","z":"56b1c979.b2c618","name":"Toggle Switch","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":182,"y":224,"wires":[["dfb609e7.8f9f38"]]},{"id":"dfb609e7.8f9f38","type":"api-call-service","z":"56b1c979.b2c618","name":"Toggle Switch On/Off","version":1,"debugenabled":false,"service_domain":"switch","service":"toggle","entityId":"switch.nodered_entity_switch","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":388,"y":224,"wires":[[]]},{"id":"ae4f1ef6.1afcf","type":"inject","z":"56b1c979.b2c618","name":"Trigger Switch","topic":"","payload":"{\"data\": {\"entity_id\": \"switch.nodered_entity_switch\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":182,"y":272,"wires":[["9d8bb1d9.47b08"]]},{"id":"9d8bb1d9.47b08","type":"api-call-service","z":"56b1c979.b2c618","name":"nodered.trigger switch no payload","version":1,"debugenabled":false,"service_domain":"nodered","service":"trigger","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":428,"y":272,"wires":[["cdf62164.236c7"]]},{"id":"52ad42c3.813b0c","type":"ha-entity","z":"56b1c979.b2c618","name":"","server":"9c202bd8.b64db8","version":1,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"nodered_entity_switch"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:airplane"},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":358,"y":160,"wires":[["cd9b188.f1707e8"],["441482ca.d5a8dc"]]},{"id":"cd9b188.f1707e8","type":"debug","z":"56b1c979.b2c618","name":"on","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":514,"y":144,"wires":[]},{"id":"6ef82461.aaf80c","type":"inject","z":"56b1c979.b2c618","name":"Input to Switch","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":192,"y":160,"wires":[["52ad42c3.813b0c"]]},{"id":"441482ca.d5a8dc","type":"debug","z":"56b1c979.b2c618","name":"off","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":514,"y":176,"wires":[]},{"id":"cdf62164.236c7","type":"api-call-service","z":"56b1c979.b2c618","name":"nodered.trigger switch object payload","version":1,"debugenabled":false,"service_domain":"nodered","service":"trigger","entityId":"","data":"{\"payload\": {\"abc\": 123}}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":438,"y":320,"wires":[["ab5d1645.d48538"]]},{"id":"ab5d1645.d48538","type":"api-call-service","z":"56b1c979.b2c618","name":"nodered.trigger switch string payload","version":1,"debugenabled":false,"service_domain":"nodered","service":"trigger","entityId":"","data":"{\"payload\": \"abc\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":438,"y":368,"wires":[["24987085.42514"]]},{"id":"24987085.42514","type":"api-call-service","z":"56b1c979.b2c618","name":"nodered.trigger switch boolean payload","version":1,"debugenabled":false,"service_domain":"nodered","service":"trigger","entityId":"","data":"{\"payload\": true }","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":448,"y":416,"wires":[["beb6e063.28cbb"]]},{"id":"beb6e063.28cbb","type":"api-call-service","z":"56b1c979.b2c618","name":"nodered.trigger switch number payload","version":1,"debugenabled":false,"service_domain":"nodered","service":"trigger","entityId":"","data":"{\"payload\": 123 }","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":438,"y":464,"wires":[[]]}]

Changelog

Features

  • entity: Rename sensor node to entity and add new entity type switch (059c340)

  • wait-until: Entity Id can be a exact match, substring or Regex match (9738bb5)

  • wait-until: Timeout property can be a JSONata expression to allow for dynamic timeout values (9dcbe1a), closes #206

Bug Fixes

  • Prior to 0.90.0 is_admin doesn’t exist check for it to exist and not true (3f07f55), closes #207

0.21.1 (2020-02-26)

Bug Fixes

  • fix breaking change caused by rebranding hassio (c6dcaac)

0.21.2 (2020-02-28)

Bug Fixes

  • wait-until: use correct entity id for check against current state (f2d47c5), closes #210

0.21.5 (2020-03-09)

Bug Fixes

  • Use correct entity type when removing node from integration (f842578), closes #217

Buy me a coffee For those that enjoy my work.

2 Likes

This keeps getting better and better! Thanks for the updates Kermit.

Does this mean we can have the wait_until node track multitple entities, and pass through the message when of them turns true?
Is it also possible to combine entities and only pass through when all of the entity states are true? Both of these would be great and open up a lot of new doors for automations.

Yes.

You can currently do this using other means.

image

[{"id":"9308bc81.4a6de","type":"trigger-state","z":"56b1c979.b2c618","name":"","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"sensor.one,sensor.two,sensor.three","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"id":"t5q2kjhy0hq","targetType":"entity_id","targetValue":"sensor.one","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"},{"id":"dxb67aubkgv","targetType":"entity_id","targetValue":"sensor.two","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"},{"id":"wnht4zs3l0e","targetType":"entity_id","targetValue":"sensor.three","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"}],"constraintsmustmatch":"all","outputs":3,"customoutputs":[{"outputId":"2po12l94wg8","messageType":"payload","messageValue":"{\"pass\": true}","messageValueType":"json","comparatorPropertyType":"always","comparatorPropertyValue":"","comparatorType":"is","comparatorValue":""}],"outputinitially":false,"state_type":"str","x":260,"y":208,"wires":[[],[],["135e1f54.0ba001"]]},{"id":"29d981a7.f2f0de","type":"inject","z":"56b1c979.b2c618","name":"Start waiting","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":166,"y":272,"wires":[["135e1f54.0ba001"]]},{"id":"135e1f54.0ba001","type":"function","z":"56b1c979.b2c618","name":"","func":"savedMessage = context.get(\"savedMessage\");\nif(msg.payload && msg.payload.pass === true) {\n    node.status({});\n    if(savedMessage !== undefined) {\n        return savedMessage;\n    }\n    return;\n}\n\ncontext.set(\"savedMessage\", msg);\nnode.status({text: \"waiting\"});","outputs":1,"noerr":0,"x":338,"y":272,"wires":[["f4e02155.4aa8a"]]},{"id":"43bda0e1.c41bf","type":"inject","z":"56b1c979.b2c618","name":"Fake Pass","topic":"","payload":"{\"pass\": true}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":156,"y":320,"wires":[["135e1f54.0ba001"]]},{"id":"f4e02155.4aa8a","type":"debug","z":"56b1c979.b2c618","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":512,"y":272,"wires":[]}]
1 Like

Is there a way to use the entity type switch to create a switch in HA to trigger a flow in node red?

From above:

I believe I discovered a bug where using the trigger state node and having multiple binary sensors in there (from the custom addon you made, all binary sensors) results in wildly fluctuating values, sometimes on / off but most of the time its values over 6 digits, sometimes minus 6 digits, it’s all over the place. Definitely not a simple binary sensor on/off value though. Any idea what might cause it?

I’m seeing errors in the logs too. Looks like there might be some trouble with the websocket connection? I’m not having any other issues though, all automations work fine.


28 Feb 02:49:53 - [warn] Communication send error: Error: WebSocket is not open: readyState 3 (CLOSED)
28 Feb 02:49:57 - [info] [server:Home Assistant] WebSocket Connecting http://192.168.200.23:8123
28 Feb 02:49:57 - [info] [server:Home Assistant] WebSocket Connecting http://192.168.200.23:8123
28 Feb 02:49:57 - [info] [server:Home Assistant] WebSocket Connecting http://192.168.200.23:8123
28 Feb 02:49:57 - [info] [server:Home Assistant] WebSocket Connecting http://192.168.200.23:8123
28 Feb 02:49:57 - [info] [server:Home Assistant] WebSocket Connecting http://192.168.200.23:8123
28 Feb 02:49:57 - [info] [server:Home Assistant] WebSocket Connected to http://192.168.200.23:8123
28 Feb 02:49:57 - [info] [server:Home Assistant] WebSocket Connected to http://192.168.200.23:8123
28 Feb 02:49:57 - [info] [server:Home Assistant] WebSocket Connected to http://192.168.200.23:8123
28 Feb 02:50:00 - [error] [ha-entity:trigger_state ] Node-RED custom integration needs to be installed in Home Assistant for this node to function correctly.
28 Feb 02:50:00 - [error] [ha-entity:trigger_state ] Node-RED custom integration needs to be installed in Home Assistant for this node to function correctly.
28 Feb 02:50:00 - [error] [ha-entity:trigger_state ] Node-RED custom integration needs to be installed in Home Assistant for this node to function correctly.

And yes, I do have the custom integration installed to the latest version as of today.

EDIT: More trigger-state nodes are bugging out, even when filtering out for a single entity ID, it’s showing massively fluctuating values every second or so, completely out of the ordinary and what it’s supposed to do.


image

I’ve got the same problem. It was late, I noticed this problem, and rolled back to a previous version before I had a chance to debug. In my case I was specifying multiple entities in a trigger-state node using a regex match. The result was the same behavior though: seemingly random values quickly cycling in the flows. CPU usage tied to Node-RED went through the roof too.

Yep, same thing here. @Kermit might be worth taking a look at the trigger state node in the latest version.

@TaperCrimp which version was working correctly for you?

I just checked the palette on my working version and it’s listed as node-red-contrib-home-assistant-websocket 0.20.2. The upgrade to 0.21.2 is when everything started acting up.

I didn’t do any in-depth troubleshooting, but the trigger state node seemed to behave normally if I manually listed every entity. It’d cycle random values if I plugged in a regex expression for the entities. It was almost like the regex was grabbing values from everything instead of just the entities that matched.

@reckless @TaperCrimp Try version 0.21.3 and see if that fixes your issues.

4 Likes

So far it looks like that fixed it! All my trigger states look normal now. Thanks for the quick fix.

1 Like

All good! Thanks again.

Hey all. @Kermit - first of all thank you for the awesome integration.

I wanted to gauge if there was any interest in adding a Finite State Machine node. I’ve found myself using more and more implementations of node-red-contrib-state-machine in my automations for everything from non-binary presence detection, to motion lighting with local overrides and better occupancy detection. These are usually coupled with a sensor (now entity) node to maintain the status in HA and a switch node for splitting out the different states.

The biggest pitfall I’ve had is that the current state-machine node has no persistent status, so every time the flow restarts (or Node-RED) I lose the state and it has to be reset manually. Not good for the WAF when the “Good Morning” automation triggers in the middle of the night due to a add-on update.

I created a test version which integrates node-red-contrib-state-machine, a switch node, and a Home Assistant Sensor node (for visibility in HA) into one, which uses the existing persistent storage to maintain and recover the state.

Each state can have an individual timeout set. When that state is activated, it will automatically start a timer and will transition if the timer is allowed to expire. The timer is also persistent, so if the flow is restarted, the timer will still attempt to restart and expire at the same time as the original expiration.

If there’s interest in something like this I can make a PR for it (source is here for reference). I personally think this would be a very powerful tool.

Replaces something like this:

I have a problem on HA 0.106.2 and have had it for the last couple of updates.

Node red add on version 6.1.3, but has been ongoing for a few versions now, since I reinstalled and setup Home Assistant over my old hassbian set-up a few weeks ago.

I am running home assistant on a pi3b+ with official power supply, installed and boot from ssd.

This is stopping alexa from being able to control lights etc and causing lag generally in the system.

Logger: alexapy.alexawebsocket
First occured: 4:07:31 PM (28 occurences)
Last logged: 6:32:30 PM

WebSocket Error: <class 'concurrent.futures._base.TimeoutError'>

I am also getting this error repeatedly as well:

Logger: alexapy.alexawebsocket
First occured: 4:44:04 PM (5 occurences)
Last logged: 7:06:34 PM

WebSocket Error: <class 'aiohttp.streams.EofStream'>

I stopped the node red add-on and haven’t had any error messages for the last 5 hours or so.

alexapy.websocket errors continue repeating and building up in the logs and the whole system becoming laggy and sometimes crashing.

The only flow I have in node red is one switch and one script connected to alexa.

Did you have NodeRED running while on your hassbian setup? If so did you get the same errors?

I did have it running installed on the same pi, not the add on.
I have tried asking in the add on pages, but got directed here.

I wonder if it is something to do with the alexa nodes I am using.