How to access native HA presence (HA / Unifi) in nodered, so it can go to MQTT

Apologies for the question, I’ve missed a concept somewhere.
I have presence running natively in HA, using HA / Unifi ( not via nodered plugin)
How can I access the native HA presence (HA / Unifi) in node red, so I can send it to MQTT and reuse.

Did you mean “presence”?

:rofl: Yes, presence, sorry to many jobs on the go. Hopefully presence will be persistent.

1 Like

All HA states are stored in the Node Red global variable homeassistant.
In the upper right corner select the last icon and press the update icon for globals.

Many thanks for the pointer, I cant find the update icon for global variables anywhere in NR or HA. Which screen is it in
I wonder if my setup is missing pieces.
I’m using the HASS - docker with HA, MQTT, NR,

First off chk to see if globals are enabled. In any HA node go to the server section and click the little pencil, is globals box checked, if not enable it. You can find them on the right side where the debug is under context.

Before we get into their use could you go into more detail, what entities are you looking to send out over mqtt? What is the end goal of this automation?

Many thanks.

I’m moving from OpenHab to HA.
HA is running HASS ,MQTT, NodeRed, HA Core 2024.4.4, Frontend 20240404.2 thru docker on RPI5.
I’m running 50 ish tasmota sonoff, esphome devices via MQTT.
I have a bunch of automation’s in nodered (NR)
I was unable to get the Unifi component in NR working, but did in HA, it works out presence ok.
I’d like to get the HA presence from HA into NR, and MQTT so it can be used in my existing Automatons, and elsewhere.

I’ve looked in NR on a few nodes but cant see anything about global variables.
Also googled to death but did not really come across anything substantial on global variables in HA or NR.

In NR I can see a bunch HA nodes eg. API, call service, current state etc. etc. Could someone post a screen shot of where it might be.

can you just grab the state of the relevant entities you want to monitor in HA, via the entity node, and then transform the state into whatever MQTT message you want ?

if you use the current state node, this will send a message whenever the selected entity changes in HA.

Content of a function node to extract global variables from the sun sensor.

msg.sun = global.get('homeassistant').homeAssistant.states["sun.sun"];
msg.sun_state = global.get('homeassistant').homeAssistant.states["sun.sun"].state;
msg.sun_attr_next_dawn = global.get('homeassistant').homeAssistant.states["sun.sun"].attributes.next_dawn;
return msg;

NodeRed documentation on context storage: Working with context : Node-RED

1 Like

Many, many thanks, I never searched for context

Many thanks.
HA says sun is installed and configured by default. Sun - Home Assistant

I get an error in NR with the sun script above

function : (error)
“TypeError: Cannot read properties of undefined (reading ‘homeAssistant’)”

A refresh of the NR global context is empty

Follow the arrows on the picture.
Then you should see the value for the get(‘…’)
Open that one to see next level.
Next level is in my setup called homeAssistant which in the function node is seen by the .homeAssistant

Global is empty, maybe there’s an issue with the HA config.

Read what Mikefilla wrote earlier in the thread

Apologies I think the wires got crossed.
The enabling of globals is what I was unable to find.
I’d been hunting for something like global variable or like, I wasn’t looking for context store. I now see “Enable global context store”

Many thanks for helping trouble shoot, and the suggestions.

This approach with HA managing the devices is working great.