Obtain Temperature Unit from HA Configuration

I’m writing a Node Red flow that will trigger a subsequent flow when outdoor temperature is below freezing. My instance of HA is configured for °C. I’ve added a node that reacts when the temperature falls below 0°C. I’d like to add code to this function so that if the temperature setting in HA is changed to °F, the function would then prove true when the temperature fall below 32°F. I found a variable in HA named unit_of_measurement. It seems connected to how HA distinguishes between 0°C and 32°F. I’ve tried extracting this value using the following;

//############################
//# Obtain Current Temperature
//############################

const globalHomeAssistant = global.get('homeassistant');
var temperature = globalHomeAssistant.homeAssistant.states["sensor.stratford_temperature"].state;
var temperature_uom = globalHomeAssistant.homeAssistant.states["attributes.unit_of_measurement"].state;

if ((temperature_uom = "°C" && temperature < 0) || (temperature_uom = "°F" && temperature < 32)) {
    return [msg];
}
return [null];

Using an Inject node, I see there’s an error in the code;

“TypeError: Cannot read properties of undefined (reading ‘state’)”

but I cannot figure out what it is.

Your assistance with this section of code would be appreciated.

Edited Oct 18, 2022: Moved from ESP32 to Node-Red topic.

login to Home Assistant then go Settings / System / General and tick F… you may have to restart not sure.

or if it is just for that sensor go

Settings / Device & Services / ESPHome / into your sensor / into entities / into the entity and settings and select F in units of measurement.

@Blacky, I mistakenly posted this topic in the ESPhome category instead of the Node Red category. I’ve since moved the post to the appropriate topic.

Thanks for taking the time to reply. What you’re describing is how to change the “default” temperature setting in HA. I’m familiar with that process. What I’d like to know is how to format a function in NodeRed, that determines what that setting is. Has the user set it to °C or °F.

You need to get the attributes for the particular sensor:

const units = globalHomeAssistant.homeAssistant.states["sensor.stratford_temperature"].attributes.unit_of_measurement;

So to get both state and units:

const sensor = globalHomeAssistant.homeAssistant.states["sensor.stratford_temperature"];
const state = sensor.state;
const units = sensor.attributes.unit_of_measurement;

@michaelblight , thank you for the detailed description. This is exactly the information I need.

Regards