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.
@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.