Node-red-contrib-home-assistant-websocket

Sorry, I am not seeing exactly why you think we need to incorporate a state-machine into this package as the only part you’d be using is creating a sensor in HA which you have shown in the replacement image that’s it pretty simple to do already.

If I am missing something please let me know.

The biggest thing for me is actually the persistent state. You’ve got a very nice method for storing and recovering the states across restarts. I was on the fence between implementing a version of state machine with persistent storage or extending the sensor node to use a state machine. The latter was actually easier. Also by definition, it’s not very easy to ‘force’ a complex state machine to a certain state through just inputs.

The other piece that I implemented that is helpful (at least for me) is an optional builtin timeout for each state, which can be (a) seen in Lovelace as attributes and (b) recovered on a restart of NR or the flow. So if I have a 24 hour timeout set, the expiration is visible in Home Assistant as an attribute (for display or other automation) and if something restarts 12hours in, I can recover and the timer will still expire at the same time as it was originally supposed to.

Overall, this becomes a combination of a sensor, input_boolean, and timer in one node/entity. Similar to this custom component except in NodeRED.

For me this is useful for things like motion lights (triggered, motion on, motion off, override, etc) and occupancy detection (arrived, home, departed, away, extended away, returned). All visible from HA, but extensible in NodeRED.

Yes, these can be (and I have) implemented as separate nodes, I just figured it may be something some people find useful as a single node.

Ok, sounds good as you said some people might find it useful.

I was quite excited with this component, but I am now facing an issue as in my HA is not capturing the nodes results.
info: Running websockets 0.21.4, Node-Red 6.1.3 and HA 0.106.5 on PI4

All of my nodes are returning the data requested:

But when looking into HA, none of them is active:
image

Try the latest release v0.21.5 and see if that fixes your issue.

Updated to v0.21.5 and same behavior

What do your HA logs show? Try turning on debugging for the component

custom_components.nodered: debug

I got an enormous log file which thrown information most of it I cannot really decrypt

One thought, is there some sort of error throwing in case the results returned by the node are over 255 characters? Asking this as I now have out of 4 sensors 3 unavailable (the ones I need to set) but I created an additional for testing Hello World node which is working properly

That would be it. https://github.com/home-assistant/core/blob/5f5cb8bea8b92ebb0f30ed30b680b82d5f441250/homeassistant/core.py#L727-L731

I’ll see about adding a check for the state length.

Cool!
I know I need to apply some sort of rule to get less than those 255 values, but I’m unclear where (in which node): on my http request OR on the call service request?

New Release 0.22.0

Bug Fixes

  • Process msg property even when set to false (7028eae), closes #219

  • trigger-state: Restore default value for state_type (4a26221)

  • Wait for persistent data to be loaded before registering with HA (2f76691), closes #223

Features

  • Improve colors to work better in dark themes (d153070)

0.22.1 (2020-03-30)

Bug Fixes

  • Revert HA websocket lib update (d07ff35), closes #225

0.22.2 (2020-04-03)

Bug Fixes

  • “NaN” for events-state-changed (5aa5b71)

  • final final fix for NaN bug (10325fa)

  • Final fix for NaN ??? (b363e27)

  • null state for event and states data (9dc1a7b)

0.22.6 (2020-06-25)

This release brings the fixes for Home Assistant allowing a WebSocket connection before all integrations are fully loaded.

All event node statuses now use the ring shape and a new status of running was added to show when HA is in the running state. Event nodes will not start receiving events until HA is in the running state. This should be the same functionality as HA pre v0.111.0. The call-service, fire event, and API nodes can be used at any point after the connection is made and don’t need to wait until the running state.

If you’re using the Node-RED component those nodes will become functional when the component is loaded in HA, which could be before HA enters the running state.

Bug Fixes

  • Wait until HA is in running state to emit events (92cdb64), closes #246
  • Wait until integration is loaded before attempting to register entities (1d8eb04), closes #247

Buy me a coffee For those that enjoy my work.

1 Like

Is there any efficiency gain in using a single state change node with a couple of entity ids via regex (or even light.*) or individual change node per entity? Does the server entity put these all into a single websocket connection, or open one per node?

There only one websocket connection to HA per server config and most users are not going to see any difference from using one node vs. many.

Fantastic; thank you for the response!

Is it possible to use this to extract attributes as entities and have them report in HA? What I’m looking to do is read attributes from various wireless devices (ie. battery, rssi) and expose them as sensors in HA. I’m also curious if this is something I could use to convert an entity. I use a Zen Zigbee thermostat that will not identify as “climate.”, however the attributes are there for the taking.

For starters, I have been able to use the Get Entities Node to pull some attributes. Feeding that into a Sensor node is where I get lost. I use NodeRed for anything I can, but I’m not too strong in creating any complex flows.

Yes, if you have the custom integration installed.

[{"id":"7d7f0c35.2ea0e4","type":"api-current-state","z":"ffbd7f06.4a014","name":"","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"sun.sun","state_type":"str","state_location":"","override_payload":"none","entity_location":"payload","override_data":"msg","blockInputOverrides":false,"x":436,"y":896,"wires":[["bef2ba78.08c948"]]},{"id":"480d0b3f.166054","type":"inject","z":"ffbd7f06.4a014","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":896,"wires":[["7d7f0c35.2ea0e4"]]},{"id":"cd1c2df1.0f447","type":"split","z":"ffbd7f06.4a014","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"topic","x":802,"y":896,"wires":[["2ca8c0bf.6035"]]},{"id":"bef2ba78.08c948","type":"change","z":"ffbd7f06.4a014","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.attributes","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":896,"wires":[["cd1c2df1.0f447"]]},{"id":"2ca8c0bf.6035","type":"switch","z":"ffbd7f06.4a014","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"next_dawn","vt":"str"},{"t":"eq","v":"next_dust","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":930,"y":896,"wires":[["b062e4d8.bbf278"],["f0fc60c5.5ba9f"]]},{"id":"f0fc60c5.5ba9f","type":"ha-entity","z":"ffbd7f06.4a014","name":"","server":"2dad33ee.42bf5c","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"Next Dust"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":1078,"y":896,"wires":[[]]},{"id":"b062e4d8.bbf278","type":"ha-entity","z":"ffbd7f06.4a014","name":"","server":"2dad33ee.42bf5c","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"Next Dawn"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":1078,"y":848,"wires":[[]]}]

If your entity already exists in HA why not use the template sensor?

2 Likes

I am using the custom component for other flows. I’ve used templates for a few one-off’s. What I was hoping is being able to extract multiple attributes easier rather than doing multiple templates for each sensor and associated attribute (using multiple Wyze motion and door).

As far as the thermostat, HA just doesn’t see it as a thermostat (climate). It sees it as “fan.zen” and a battery that it cannot read. That really comes down to the ZHA device handler, but my hope was to be able to extract the attributes from what it does see and possibly use a change node (or simply load the various attributes read) to combine and get the device loaded the way it should. I know I could use a template for that also. As I reply, I see it makes more sense, but tbh that’s above my current skill level. :confounded:

Create a template sensor from the temperature attribute. Assuming that temperature is the property of the attributes of fan.zen.

sensor:
  - platform: template
    sensors:
      thermostat:
        friendly_name: "Thermostat Temperature"
        unit_of_measurement: °F
        value_template: "{{ state_attr('fan.zen', 'temperature') }}"

Create a generic thermostat using the fan.zen as the switch. This might not work and then you would need to create a template switch using fan.zen. Then using the newly created sensor for the target temperature.

climate:
  - platform: generic_thermostat
    name: Thermostat
    heater: fan.zen
    target_sensor: sensor.thermostat

Here’s the link for the docs of the generic thermostat that shows you what other config variables can be set.

https://www.home-assistant.io/integrations/generic_thermostat/#configuration-variables

Unfortunately, running the template isn’t as straight forward as I would hope. The original entity only adds as:

Poking around the ZHA device management, I have a Thermostat cluster with all of the appropriate attributes. This is what leads me to think it involves modifying the device handler in some way.

As for the Wyze sensors, I can extract attributes of interest perfectly with your previous example. It is less laborious imo than templating for each sensor, but at the same time I had thought using the get entities might simplify it further.

What is the recommended way for a sensor or a switch of this contrib, if I need it multiple times in my flows? Problem is: I cannot simply duplicate an existing node of this contrib, as it then generates another entity in HA (eg. “switch.a” and “switch.a_2”).

Does this mean, that the only solution (if I need multiple instances) would be a single node of this contrib that is unconnected to anything – just to create the entity in HA, and then normal events:state nodes to read the entity’s state wherever I need it? (And normal call service nodes to toggle the entity’s state if I created switches with this contrib?)

Here is a switch example to illustrate. Note the unconnected contrib node at the top and the normal HA nodes below to read and write it’s state:
image

[{"id":"8194dfd4.54ba6","type":"server-state-changed","z":"30d7cbcd.b5e854","name":"","server":"cb38d2a3.10198","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"switch.my_switch","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"x":260,"y":160,"wires":[["cb40ae8b.2fca5"]]},{"id":"cb40ae8b.2fca5","type":"debug","z":"30d7cbcd.b5e854","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":510,"y":160,"wires":[]},{"id":"b3666fda.daaa","type":"inject","z":"30d7cbcd.b5e854","name":"","topic":"","payload":"on","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":220,"wires":[["7ab70985.ab6608"]]},{"id":"7ab70985.ab6608","type":"api-call-service","z":"30d7cbcd.b5e854","name":"","server":"cb38d2a3.10198","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_{{payload}}","entityId":"switch.my_switch","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":400,"y":240,"wires":[[]]},{"id":"d10ace81.18328","type":"inject","z":"30d7cbcd.b5e854","name":"","topic":"","payload":"off","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":260,"wires":[["7ab70985.ab6608"]]},{"id":"3e8dc9c3.dc4d36","type":"ha-entity","z":"30d7cbcd.b5e854","name":"my_switch","server":"cb38d2a3.10198","version":1,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"my_switch"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":190,"y":100,"wires":[[],[]]},{"id":"cb38d2a3.10198","type":"server","z":"","name":"Home Assistant"}]

Or am I overlooking something here?