Set entity status in HA

I made a new binary sensor entity in Node-Red by ‘entity’ node.
I’d like to use this node as a Node-Red input. I should set the entity state (on-off) by HA script/automation action and I should use the changes of entity status in Node-Red. It will work if I set the entity status in HA developer tools, but how can I set the status of entity in a script (without python)?

You can’t set binary_sensor state in HA (outside of the dev tool which is meant for testing purposes), since there is no service you can call (unlike light, switch, input_boolean entities). Binary sensors are meant to be created by an integration or template, which then decides wether it’s on or off (not the user).

If your goal is to turn an entity on/off and use that as a trigger in Node-RED, you should use input_boolean (which you can create in HA, see link :point_left:). Then you can use a service call (like script/automation) to turn the input_boolean either on or off (or toggle) and then use that entity as a trigger with an events: state node for a flow in Node-RED. Basically an input_boolean is a virtual switch.

If you mean something else entirely and I have not understood your goal, please clarify.

Yes, I have used this solution (input_boolean), but I’d like to eliminate the unnecessary helper.

Well, it’s not unnecessary since this is the whole purpose of the input_boolean. As stated: you can’t control a binary sensor with automation/script from HA, since that’s not what it’s meant for. It’s even explained in the first line of the doc I linked at:

Binary sensors are similar to other sensors in that they monitor the states and conditions of different entities.

First you need the node red companion installed to use entities in HA. You can set the sensor on/off by sending msg.payload set as true or false. Then you can use the entity for a condition or trigger.

This listens to an mqtt feed from a pi. As long as it is functioning and sending messages, the binary stays on. If it stops sending messages and goes off line the trigger will send false to the entity node and will show up as off in HA

[{"id":"66b7a7f689d02e03","type":"mqtt in","z":"f80b6c338afd5483","name":"monitor/rpi/#","topic":"monitor/rpi/#","qos":"0","datatype":"auto","broker":"601bef1.d5b981","nl":false,"rap":false,"inputs":0,"x":590,"y":1620,"wires":[["55a0a06e48ec2cdc"]]},{"id":"55a0a06e48ec2cdc","type":"trigger","z":"f80b6c338afd5483","name":"2A online sensor","op1":"true","op2":"false","op1type":"bool","op2type":"bool","duration":"10","extend":true,"overrideDelay":false,"units":"min","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":890,"y":1620,"wires":[["fb62999b8ba2bef8"]]},{"id":"fb62999b8ba2bef8","type":"ha-entity","z":"f80b6c338afd5483","name":"Online sensor","server":"6b1110b5.183a4","version":2,"debugenabled":true,"outputs":1,"entityType":"binary_sensor","config":[{"property":"name","value":"2A pi"},{"property":"device_class","value":"connectivity"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":1260,"y":1620,"wires":[[]]},{"id":"601bef1.d5b981","type":"mqtt-broker","name":"Mosquitto","broker":"","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":4,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"6b1110b5.183a4","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]

Yes, I’ve used node red companion and regisrered Node Red entity in HA. I don’t want to send binary state from Node-Red to HA, but I’d like to get message from HA to Node-Red.

However, it’s a good idea. If I can’t send binary state from HA to NR without helper, I can use mqtt, so I needn’t make an unnecessary helper, which is not used anywhere else in HA.

Thank you, for your detailed answer.
It isn’t important that I use a binary sensor, but I’d like to send binary information from HA script to Node-Red. The input_boolean helper is unnecessary, because I don’t use it at all in HA automation/lovelace/automation… So I think I will use mqtt communication, without helper, if there isn’t a better solution.

Where is this message coming from, will another sensor trigger it? Or is this an arbitrary state that you will control?

If it is not tied to anything you will need to build an automation that fires a state_changed event to HA’s event bus with either true or false as the entity’s new_state or use mqtt.

Sounds like you want the entity node to be a switch type, not a binary_sensor.

I’m not using MQTT myself so correct me if I’m wrong.
Where would this MQTT message be saved? An entity, wouldn’t that be the same as a helper then?

Even if you somehow make the MQTT message persistent without an entity in a Node red flow then what would happen in case of a restart?

That is essentially the same as a boolean.
Just that it’s created at a different place.

As I understand it OP wants to disable a sequence without an entity and from Home Assistant.

If the retain flag is set for a topic, the broker holds the last value in memory. Every time a client subscribes to the topic, the broker resends the retained message.

1 Like

Maybe this is what you are looking for:

You can create binary sensors and set their states.

Yes, the switch was the perfect solution.

a short summary of my light automation:

I can turn the light group on with an Ikea switch on click.
If the light is on when I click again a light theme changed (full brightness (all lights is on in light group) → relax (all lights is on, brightness 10%) → Reading (some light is on ) → TV (some lights is on, brightness 10%).
I can turn on the light group on the Lovelace button card and light card and picture elements too.
If I change the state of individual lights the theme mode switch off .
When the state of a light group or a light in the light group changes (i.e. switch on/off lights, change brightness) a timer starts, which switch off the lights after a defined period (set by Lovelace). Before the end of the timer (warning time set by Lovelace) the light group flash. If we click Ikea switch on before warning time the theme be changed. After warning time (before the timer switches off the light) the Ikea switch click on restart the switch-off timer. And this Ikea switch click on automation has to send restart (off->on->off pulse) sign to Node-Red switch off timer node group.

And the Node-Red timer registers an entity that shows me the remaining time on Lovelace.

Thank you all for your help!

I can’t sing the solution (miss the solution button)

Be careful with switch node. It’s initial state is “on”! (or it was, but I’m not aware about the change/fix).
Why not use Current state node, or Events:state depending on the needs in your flow? This is the best way to handle what you wrote: ’I’d like to get message from HA to Node-Red.’

I don’t understand exactly. What do you mean by switch node?
I use an entity node that registers a switch entity in HA.
The Current state node and Events state node don’ t register entity at HA. So I have to use entity node, but I use an injection node, which injects an ‘off’ state to entity node at the start of node-red

Note, restarting NR is not the same as restarting HA.
In case you restart NR (because of update for example) you might change state of the switch entity.
Nothing wrong if you are aware and prepared for this. But Entity node configured to switch mode brings too many corner cases to my liking.