node-red-contrib-polygon 0.1.0

Piecewise linear interpolation with up to 20 XY pairs; robust endpoint extrapolation.

npm install node-red-contrib-polygon

node-red-contrib-polygon

npm CI

Piecewise linear interpolation Node-RED node with up to 20 (X,Y) breakpoint pairs, optional endpoint extrapolation, and strict monotonic X validation.


📌 English

What this node does

polygon computes an output value from an input (msg.payload) by linearly interpolating between user-defined breakpoints. It supports 20 (X,Y) rows, each with a Use checkbox to activate the pair. Outside the endpoint range, it can extrapolate or clamp—your choice. Settings persist correctly in the editor.

Key features

  • 20 × (Use, X, Y) rows in the edit dialog
  • Strictly increasing X enforced:
    • Editor-side validation highlights the faulty fields and marks the node invalid (red triangle) when used X’s are not strictly increasing.
    • Runtime guard: an invalid configuration (e.g. from an imported flow) is shown as a red status on deploy, and every message raises an error (catchable with a Catch node).
  • Endpoint behavior selectable:
    • Extrapolate (default ON): linear continuation beyond first/last point.
    • Clamp: hold Y at the nearest endpoint.
  • Exact-knot handling: if IN == Xk (within a tiny epsilon), OUT = Yk.
  • One output:
    1. OUT – cloned incoming message with computed msg.payload, plus msg.n_bp, msg.segment
  • Status text indicators for quick debugging (e.g., seg k, extrap, clamped, X order invalid, etc.)

Installation

Easiest: Menu → Manage palette → Install, search for node-red-contrib-polygon.

Or from the command line, in the Node-RED userDir (usually ~/.node-red, Docker: /data), then restart Node-RED:

cd ~/.node-red      # or: cd /data (Docker)
npm install node-red-contrib-polygon

Configuration & algorithm

Let the active (Use ✓) points be (X1,Y1)
(Xn,Yn), n ≄ 2, with strictly increasing X:

  • If IN is inside: find k with Xk < IN < Xk+1 and compute
    OUT = Yk + (IN − Xk) * (Yk+1 − Yk) / (Xk+1 − Xk)
  • If exactly on a knot IN == Xk (epsilon match): OUT = Yk
  • If IN < X1:
    • Extrapolate ON: use slope of (X1,Y1)–(X2,Y2)
    • Extrapolate OFF: OUT = Y1 (clamp)
  • If IN > Xn:
    • Extrapolate ON: use slope of (Xn−1,Yn−1)–(Xn,Yn)
    • Extrapolate OFF: OUT = Yn (clamp)
  • Input: msg.payload must be a number or a numeric string (e.g. "21.5"). Anything else ("12abc", "unavailable", true, null
) is rejected.
  • Faults (no output message; red status; error raised, catchable with a Catch node):
    • Checked row with missing or non-numeric X/Y
    • Not enough points (n < 2)
    • X order invalid (Xk ≄ Xk+1 among used rows)
    • Non-numeric input

Output (OUT): a copy of the input message with the computed value in msg.payload, plus:

  • msg.n_bp – number of active pairs
  • msg.segment – 0-based index of the segment used: k for Xk < IN < Xk+1 (counting active points from 0), k−1 when IN hits an inner knot Xk, and -1 outside the range (clamped/extrapolated) or on the first/last knot

Quick test

Set two points and enable extrapolation:

  • Use1: X=0, Y=0
  • Use2: X=10, Y=100
  • Extrapolate: ON

Expected:

  • IN=-5 → OUT=-50 (left extrapolation)
  • IN=0 → OUT=0 (knot)
  • IN=5 → OUT=50 (interpolation)
  • IN=10 → OUT=100 (knot)
  • IN=15 → OUT=150 (right extrapolation)

Troubleshooting

  • Node doesn’t appear in the palette: install from the userDir (~/.node-red or /data), then restart Node-RED and hard-refresh the editor (Ctrl+F5).
  • Error "msg.payload must be a number": the input is not numeric (e.g. a Home Assistant sensor reporting unavailable). Filter such messages before the node, or handle them with a Catch node.
  • Keeps clamping instead of extrapolating: ensure the Extrapolate checkbox is ON and X’s are strictly increasing.

🇭đŸ‡ș Magyar

Mit csinĂĄl ez a node?

A polygon a bemeneti msg.payload alapjĂĄn szĂĄmĂ­t kimenetet a törĂ©spontok között lineĂĄris interpolĂĄciĂłval. A szerkesztƑben 20 sor ĂĄll rendelkezĂ©sre (Use, X, Y), a szĂ©lsƑ tartomĂĄnyokon pedig extrapolĂĄl vagy klampel – beĂĄllĂ­tĂĄstĂłl fĂŒggƑen. A beĂĄllĂ­tĂĄsokat helyesen megƑrzi.

FƑ funkciók

  • 20 × (Use, X, Y) sor
  • SzigorĂșan növekvƑ X kikĂ©nyszerĂ­tĂ©se:
    • SzerkesztƑ: a hibĂĄs mezƑket kiemeli, a node-ot Ă©rvĂ©nytelennek jelöli (piros hĂĄromszög), ha a pipĂĄlt sorok X Ă©rtĂ©kei nem szigorĂșan növekvƑk.
    • FutĂĄsidƑ: hibĂĄs konfigurĂĄciĂł (pl. importĂĄlt flow) esetĂ©n deploy utĂĄn piros stĂĄtusz, Ă©s minden ĂŒzenetre hibĂĄt ad (Catch node-dal elkaphatĂł).
  • VĂ©gpont-kezelĂ©s vĂĄlaszthatĂł:
    • ExtrapolĂĄlĂĄs (alapbĂłl BE): lineĂĄris folytatĂĄs az elsƑ/utolsĂł szakasz meredeksĂ©gĂ©vel.
    • KlampelĂ©s: Y az adott vĂ©gpontnĂĄl marad.
  • Pontos csomĂłpont (X-egyezĂ©s): ha IN == Xk, akkor OUT = Yk.
  • Egy kimenet:
    1. OUT – a bejövƑ ĂŒzenet mĂĄsolata szĂĄmĂ­tott msg.payload-dal, plusz msg.n_bp, msg.segment
  • ÁllapotjelzĂ©sek a debughoz (seg k, extrap, clamped, X order invalid, stb.)

Telepítés

LegegyszerƱbb: MenĂŒ → Manage palette → Install, keresĂ©s: node-red-contrib-polygon.

Vagy parancssorbĂłl a Node-RED userDir-ben (jellemzƑen ~/.node-red, Dockerben /data), majd indĂ­tsd Ășjra a Node-RED-et:

cd ~/.node-red      # vagy: cd /data (Docker)
npm install node-red-contrib-polygon

BeĂĄllĂ­tĂĄs & algoritmus

Legyenek az aktĂ­v (Use ✓) pontok (X1,Y1)
(Xn,Yn), n ≄ 2, szigorĂșan növekvƑ X-szel:

  • Ha IN belĂŒl van: keresĂŒnk k-t, ahol Xk < IN < Xk+1, Ă©s szĂĄmolunk:
    OUT = Yk + (IN − Xk) * (Yk+1 − Yk) / (Xk+1 − Xk)
  • Ha pontosan IN == Xk: OUT = Yk
  • Ha IN < X1:
    • Extrap BE: az (X1,Y1)–(X2,Y2) szakasz meredeksĂ©gĂ©vel
    • Extrap KI: OUT = Y1 (klampel)
  • Ha IN > Xn:
    • Extrap BE: az (Xn−1,Yn−1)–(Xn,Yn) szakasz meredeksĂ©gĂ©vel
    • Extrap KI: OUT = Yn (klampel)
  • Bemenet: msg.payload szĂĄm vagy szĂĄmot tartalmazĂł string (pl. "21.5"). Minden mĂĄst ("12abc", "unavailable", true, null
) elutasĂ­t.
  • HibĂĄk (nincs kimeneti ĂŒzenet; piros stĂĄtusz; hiba, Catch node-dal elkaphatĂł):
    • PipĂĄlt sorban hiĂĄnyzĂł vagy nem szĂĄm X/Y
    • KevĂ©s pont (n < 2)
    • X sorrend hibĂĄs (Xk ≄ Xk+1 a hasznĂĄlt sorokban)
    • Nem szĂĄm bemenet

Kimenet (OUT): a bejövƑ ĂŒzenet mĂĄsolata a szĂĄmĂ­tott msg.payload-dal, tovĂĄbbĂĄ:

  • msg.n_bp – hasznĂĄlt pontpĂĄrok szĂĄma
  • msg.segment – a hasznĂĄlt szegmens 0-tĂłl szĂĄmozott indexe: k, ha Xk < IN < Xk+1 (az aktĂ­v pontokat 0-tĂłl szĂĄmozva), k−1, ha IN egy belsƑ Xk csomĂłpontra esik, Ă©s -1 a tartomĂĄnyon kĂ­vĂŒl (klampelĂ©s/extrapolĂĄciĂł) vagy az elsƑ/utolsĂł csomĂłponton

Gyors prĂłba

Két pont, extrapolålås BE:

  • Use1: X=0, Y=0
  • Use2: X=10, Y=100

Vårt értékek:

  • IN=-5 → OUT=-50
  • IN=0 → OUT=0
  • IN=5 → OUT=50
  • IN=10 → OUT=100
  • IN=15 → OUT=150

HibaelhĂĄrĂ­tĂĄs

  • Nem jelenik meg a palettĂĄn: a userDir-bƑl telepĂ­ts (~/.node-red vagy /data), indĂ­ts Ășjra, Ă©s Ctrl+F5.
  • Klampel extrapolĂĄlĂĄs helyett: kapcsold BE az ExtrapolĂĄlĂĄs pipĂĄt Ă©s ellenƑrizd a szigorĂș X-sorrendet.
  • „msg.payload must be a number” hiba: a bemenet nem szĂĄm (pl. Home Assistant szenzor unavailable ĂĄllapota). SzƱrd ki ezeket a node elƑtt, vagy kezeld Catch node-dal.

Development

npm install
npm test

License

MIT

Node Info

Version: 0.1.0
Updated 6 days ago
License: MIT
Rating: not yet rated

Categories

Actions

Rate:

Downloads

180 in the last week

Nodes

  • polygon

Keywords

  • node-red
  • interpolation
  • linear
  • breakpoints
  • extrapolation

Maintainers