[Example] Get state_changed events based on area

This is a more fleshed-out version that I posted here: https://www.reddit.com/r/homeassistant/comments/ejvmbn/area_information_in_node_red_flows/fd2eacu

Posting it here so I can refer people to it.



@Kermit I am using a modified version of your function here to make an array of areas that map to entities in that area which are lights. My goal is if there is a motion in an area I want to turn on lights in that area. Perhaps this is even covered in some presence tutorial somewhere I was not able to find. This would be similar to having music follow you room-to-room or lights.

As I states what I am trying to accomplish is to get all of the sister entities in an area that detects motion which are lights. Thus I found this code to work perfectly but, new to this, I am wondering if there is a much easier and cleaner way without using any JS or as minimal as possible:

const area_to_lights = {};

msg.entities.forEach(e => {
    if(!e.device_id) return;
    const device = msg.devices.find(d => d.id === e.device_id);
    const area = msg.areas.find(a => a.area_id === device.area_id);

    if(area && e.entity_id.startsWith("light.light_hue_")) {
        if (typeof area_to_lights[area.area_id] === 'undefined' || area_to_lights[area.area_id] === null) {
            area_to_lights[area.area_id] = [e.entity_id];
        } else {

global.set("map_area_to_lights", area_to_lights);

return msg;


Not sure if or where it is in the docs for HA it is but you use area_id in place of entity_id it should do all the lights if you use the light domain. light.turn_on