zachowj / hass-node-red

Companion Component for node-red-contrib-home-assistant-websocket to help integrate Node-RED with Home Assistant Core
MIT License
469 stars 185 forks source link

Set state to unavailable #206

Closed walberjunior closed 10 months ago

walberjunior commented 1 year ago

zachowj Thank you for your work.

I use NR to request the status of my alarm and create all the sensors. But if for some reason the NR loses the connection with the alarm, the sensors keep the last state received. I use the script below to change the sensors to "unavailable", but it would be nice if NR had this function natively.

"""Set the state or other attributes for the specified entity."""

# ========================================================================================
# python_scripts/set_state.py
# modified from -
# https://community.home-assistant.io/t/how-to-manually-set-state-value-of-sensor/43975/37
# ========================================================================================

# ----------------------------------------------------------------------------------------
# Set the state or other attributes for the specified entity.
# Updates from @xannor so that a new entity can be created if it does not exist.
# ----------------------------------------------------------------------------------------

inputEntity = data.get("entity_id")
if inputEntity is None:
    logger.warning("===== entity_id is required if you want to set something.")
else:
    inputStateObject = hass.states.get(inputEntity)
    if inputStateObject is None and not data.get("allow_create"):
        logger.warning("===== unknown entity_id: %s", inputEntity)
    else:
        if not inputStateObject is None:
            inputState = inputStateObject.state
            inputAttributesObject = inputStateObject.attributes.copy()
        else:
            inputAttributesObject = {}

        for item in data:
            newAttribute = data.get(item)
            logger.debug("===== item = {0}; value = {1}".format(item, newAttribute))
            if item == "entity_id":
                continue  # already handled
            elif item == "allow_create":
                continue  # already handled
            elif item == "state":
                inputState = newAttribute
            else:
                inputAttributesObject[item] = newAttribute

        hass.states.set(inputEntity, inputState, inputAttributesObject)
zachowj commented 1 year ago

You want to be able to set the state of a sensor to unavailable from node-red? Not when the NR disconnects from HA automatically set the state of the sensor to unavailable?

walberjunior commented 1 year ago

You want to be able to set the state of a sensor to unavailable from node-red?

Yes. So I can find out if there was a connection problem with the alarm.

thomasvt1 commented 1 year ago

Would be a nice feature indeed!

I have node-red running as a tcp serial client, and pusing the values to home assistant. But it would be nice to set everything to unavailable when the tcp connection drops

andregoncalvespires commented 1 year ago

It would really be a great alternative.

tboudri commented 1 year ago

Sorry for the late reply. I test my zwave sensors every 2 hours bij send a ping to all the devices. Then wait 10 seconds and then read the state from every sensor this should be 'alive'. Unfortunatly this only works with zwave devices not zigbee

github-actions[bot] commented 10 months ago

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