Files
kkelomatic_home/docs/integration/nvl-to-ha-http-call.js
nearxos f0d883b8c2 Remove obsolete INFORMATION_NEEDED.md and streamline README.md for clarity
- Deleted the INFORMATION_NEEDED.md file as it was no longer necessary for documentation.
- Revised README.md to enhance the overview of the home automation system, focusing on how services and CODESYS connectivity work.
- Updated project structure in README.md for better organization and clarity, including links to relevant documentation.

This update simplifies the documentation and improves the overall user experience for developers and users of the home automation system.
2026-02-08 17:09:30 +02:00

31 lines
1.1 KiB
JavaScript

// Use when you don't use the Action node: prepares one msg for the "http request" node to call HA.
// Accepts either: (1) Action format: payload.action + payload.target.entity_id, or (2) payload.entity_id + msg.service.
// Set ha_base_url and ha_token in flow or env.
const HA_BASE_URL = flow.get('ha_base_url') || 'http://homeassistant.local:8123';
const HA_TOKEN = flow.get('ha_token') || env.get('HA_TOKEN') || '';
const p = msg.payload || {};
let entityId = p.target && p.target.entity_id && p.target.entity_id[0];
let domain = 'input_boolean';
let service = 'turn_off';
if (p.action) {
const parts = p.action.split('.');
domain = parts[0];
service = parts[1] || 'turn_off';
}
if (!entityId) entityId = p.entity_id;
if (!entityId) return null;
if (!p.action) domain = (entityId + '').split('.')[0];
const url = HA_BASE_URL.replace(/\/$/, '') + '/api/services/' + domain + '/' + service;
msg.url = url;
msg.method = 'POST';
msg.headers = {
'Authorization': 'Bearer ' + HA_TOKEN,
'Content-Type': 'application/json'
};
msg.payload = { entity_id: entityId };
return msg;