Implement global room configuration for Node-RED integration
- Introduced a single global room configuration file to streamline the management of rooms and lights, reducing redundancy in configuration. - Updated various integration scripts to utilize the global configuration, enhancing maintainability and clarity. - Improved documentation to reflect the new configuration approach, including instructions for loading and modifying the room configuration. This update simplifies the integration process and improves the overall user experience for managing lights and rooms in Node-RED.
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
// Writes to state.rooms.cmd_livingroom (NVL variable cmd_livingroom).
|
||||
const ROOM_NAME = 'cmd_livingroom';
|
||||
// Writes to state.rooms.<roomKey>. Room from entity_id via global roomConfig.entityToRoom. See /root/.node-red/room-config.js.
|
||||
const entityId = (msg.topic || '').toString();
|
||||
const parts = entityId.split('_');
|
||||
const last = parts[parts.length - 1] || '';
|
||||
const num = parseInt(last, 10);
|
||||
const lightNum = (!isNaN(num) && num >= 1) ? Math.min(6, num) : 1;
|
||||
const config = global.get('roomConfig');
|
||||
const entityToRoom = (config && config.entityToRoom) ? config.entityToRoom : { living_room_new: 'cmd_livingroom' };
|
||||
// Parse entity_id e.g. input_boolean.living_room_1 → base "living_room_1", then room + light
|
||||
const base = (entityId.split('.')[1] || '').replace(/\s/g, '');
|
||||
const numMatch = base.match(/_(\d+)$/);
|
||||
const lightNum = (numMatch && parseInt(numMatch[1], 10) >= 1) ? Math.min(6, parseInt(numMatch[1], 10)) : 1;
|
||||
const baseWithoutNum = base.replace(/_?\d+$/, '') || base;
|
||||
const ROOM_NAME = entityToRoom[baseWithoutNum] || entityToRoom[base] || 'cmd_livingroom';
|
||||
|
||||
// Normalize state: HA / trigger-state can send payload as string, object with .state, or data.new_state.state
|
||||
let rawState = msg.payload;
|
||||
@@ -24,7 +27,7 @@ if (isOn) r['ha_l' + lightNum + '_on'] = true;
|
||||
else r['ha_l' + lightNum + '_off'] = true;
|
||||
flow.set('nvlInState', state);
|
||||
|
||||
node.warn('[HA to NVL] set cmd_livingroom ha_l' + lightNum + '_' + (isOn ? 'on' : 'off') + '=true, buildAndSend');
|
||||
node.warn('[HA to NVL] set ' + ROOM_NAME + ' ha_l' + lightNum + '_' + (isOn ? 'on' : 'off') + '=true, buildAndSend');
|
||||
|
||||
// PLC uses R_TRIG (rising edge). If "off" still doesn’t work, add a flow: after this node, 80ms delay then clear ha_l*_off and buildAndSend again (pulse).
|
||||
msg.payload = { buildAndSend: true };
|
||||
|
||||
Reference in New Issue
Block a user