Node-RED Range ノードガイド

☁️ クラウドOK 環境とは?

目次

1. 概要

Range ノードは、数値を別の範囲にマッピング(変換)するノードです。入力値を指定した入力範囲から出力範囲に線形変換します。

ボリュームつまみに例えると:オーディオ機器のボリュームつまみを考えます。つまみの物理的な回転角度(0〜270度)を、音量(0〜100%)に変換するようなものです。Range ノードは、このような「ある範囲の値を別の範囲の値に変換する」処理を行います。

数値入力 → Range → Debug

ポイント:

変換の仕組み

出力値 = (入力値 - 入力最小) × (出力最大 - 出力最小) ÷ (入力最大 - 入力最小) + 出力最小

動作を図で見る

range ノードは、数値を「入力の範囲」から「出力の範囲」へ、同じ割合の位置に置き換えます。センサーの生の値を 0〜100 などの扱いやすい値に直すときに使います。

payload: 250rangepayload: 2501000入力の範囲0100出力の範囲同じ割合(25%)の位置
0〜1000 の 250 は、0〜100 に直すと 25 になる

2. 設定詳細

主要プロパティ

数値の入力範囲を出力範囲へ写像する。範囲外の値の扱いは「動作」で決める。

設定項目説明既定値
プロパティ変換する数値が入っている入力プロパティmsg.payload
動作入力値をどう写像するか。特に入力範囲を外れた値の扱いを決める(下の「動作の種類」から選ぶ)msg.payloadの値を拡大/縮小
入力値の範囲変換前の値が取りうる範囲。最小値と最大値を入力する(空)
出力値の範囲変換後に割り当てる範囲。最小値と最大値を入力する(空)
小数値を四捨五入し整数値へ変換変換結果を四捨五入して整数にするか(チェックボックス)オフ
名前エディター上のノード表示名(空)

「動作」の種類

「動作」セレクタで選べる4つの写像方法。例は入力範囲 0〜10、出力範囲 0〜100 で、範囲外の入力値 15 を与えた場合を示す。

選択肢説明入力15のとき
msg.payloadの値を拡大/縮小範囲で制限せず、比例関係のまま変換し続ける150
入力値の範囲外の値を最小値/最大値とし拡大/縮小入力範囲を外れた値を範囲の端に丸めてから変換する(クランプ)100
入力値の範囲外の値を範囲幅で割った余りとし拡大/縮小範囲を超えた分を折り返す(15 は範囲幅 10 で割った余り 5 として扱う)50
値を拡大/縮小(入力範囲外の時はメッセージを削除)入力が範囲外ならメッセージを通さない(出力されない)

変換例

入力範囲: 0 - 1023(ADC値) 出力範囲: 0 - 100(パーセント) 入力: 0 → 出力: 0 入力: 512 → 出力: 50.05 入力: 1023 → 出力: 100

3. 実用的な使用パターン

サンプルフローのインポート方法:

  1. 下のフローJSON(公式サンプルフロー、または応用フロー)をコピー
  2. Node-REDエディタで メニュー → 読み込み を選択
  3. JSONをペーストして「読み込み」をクリック

公式サンプルフロー

最初のサンプルフローは、Node-RED本体に同梱されている公式サンプル4つ(「Scale input value」「Scale input and round to integer」「Limit input」「Scale and wrap input」)を1つのタブにまとめ、名前の無かった inject ノードと debug ノードに日本語の名前を付けたものです。エディタの メニュー → 読み込み → サンプル → node-red → function → range からも、元の4つを開けます。

📥 公式サンプルフローJSON(クリックで展開)
[ { "id": "2afcee6e3e4ef550", "type": "tab", "label": "range", "disabled": false, "info": "", "env": [] }, { "id": "827a48c0.912d88", "type": "comment", "z": "2afcee6e3e4ef550", "name": "Map property between different numeric ranges", "info": "Range node can scale a number from one numeric range to another.\n\nSee Node-RED cookbook [item](https://cookbook.nodered.org/basic/map-between-different-number-ranges).", "x": 240, "y": 60, "wires": [] }, { "id": "bb23bd77.ce725", "type": "inject", "z": "2afcee6e3e4ef550", "name": "例1: 0", "repeat": "", "crontab": "", "once": false, "topic": "", "payload": "0", "payloadType": "num", "x": 170, "y": 120, "wires": [ [ "42ed281c.790b38" ] ] }, { "id": "42ed281c.790b38", "type": "range", "z": "2afcee6e3e4ef550", "minin": "0", "maxin": "1023", "minout": "0", "maxout": "5", "action": "clamp", "round": false, "property": "payload", "name": "", "x": 390, "y": 160, "wires": [ [ "56e6dd0f.436c24" ] ] }, { "id": "54659d5c.0283e4", "type": "inject", "z": "2afcee6e3e4ef550", "name": "例1: 512", "repeat": "", "crontab": "", "once": false, "topic": "", "payload": "512", "payloadType": "num", "x": 170, "y": 160, "wires": [ [ "42ed281c.790b38" ] ] }, { "id": "85ce0127.07b06", "type": "inject", "z": "2afcee6e3e4ef550", "name": "例1: 1023", "repeat": "", "crontab": "", "once": false, "topic": "", "payload": "1023", "payloadType": "num", "x": 170, "y": 200, "wires": [ [ "42ed281c.790b38" ] ] }, { "id": "56e6dd0f.436c24", "type": "debug", "z": "2afcee6e3e4ef550", "name": "例1: 結果", "active": true, "console": "false", "complete": "false", "x": 590, "y": 160, "wires": [] }, { "id": "990f33d3.3ffe2", "type": "comment", "z": "2afcee6e3e4ef550", "name": "Scale input value & round result to integer", "info": "Range node can map input value to output value according to mapping specification.\nThe result value is rounded to nearest integer if `Round result to the nearest integer?` checkbox is checked.", "x": 240, "y": 260, "wires": [] }, { "id": "b2639501.5ad638", "type": "inject", "z": "2afcee6e3e4ef550", "name": "例2: 0", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "0", "payloadType": "num", "x": 190, "y": 320, "wires": [ [ "dc4509ee.773038" ] ] }, { "id": "dc4509ee.773038", "type": "range", "z": "2afcee6e3e4ef550", "minin": "0", "maxin": "9", "minout": "0", "maxout": "128", "action": "scale", "round": true, "property": "payload", "name": "", "x": 360, "y": 320, "wires": [ [ "50991e7e.2ed24" ] ] }, { "id": "50991e7e.2ed24", "type": "debug", "z": "2afcee6e3e4ef550", "name": "例2: 結果", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "false", "statusVal": "", "statusType": "auto", "x": 520, "y": 320, "wires": [] }, { "id": "335e877f.d24e98", "type": "inject", "z": "2afcee6e3e4ef550", "name": "例2: 10", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "10", "payloadType": "num", "x": 190, "y": 360, "wires": [ [ "dc4509ee.773038" ] ] }, { "id": "5d88207d.4189", "type": "inject", "z": "2afcee6e3e4ef550", "name": "例2: 99", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "99", "payloadType": "num", "x": 190, "y": 400, "wires": [ [ "dc4509ee.773038" ] ] }, { "id": "f0bc6a80.d52578", "type": "comment", "z": "2afcee6e3e4ef550", "name": "Limit input value", "info": "Range node can map input value to output value according to mapping specification.\nThe result value is limited to specified range if `Scale and limit to the target range` is selected.", "x": 140, "y": 460, "wires": [] }, { "id": "69652849.749198", "type": "inject", "z": "2afcee6e3e4ef550", "name": "例3: 0", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "0", "payloadType": "num", "x": 170, "y": 520, "wires": [ [ "104f6f14.1c72e1" ] ] }, { "id": "104f6f14.1c72e1", "type": "range", "z": "2afcee6e3e4ef550", "minin": "0", "maxin": "100", "minout": "0", "maxout": "90", "action": "clamp", "round": false, "property": "payload", "name": "", "x": 330, "y": 520, "wires": [ [ "10fcbed4.9c8d71" ] ] }, { "id": "10fcbed4.9c8d71", "type": "debug", "z": "2afcee6e3e4ef550", "name": "例3: 結果", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "false", "statusVal": "", "statusType": "auto", "x": 500, "y": 520, "wires": [] }, { "id": "93bb4526.7d6e28", "type": "inject", "z": "2afcee6e3e4ef550", "name": "例3: 10", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "10", "payloadType": "num", "x": 170, "y": 560, "wires": [ [ "104f6f14.1c72e1" ] ] }, { "id": "6892dfd8.42386", "type": "inject", "z": "2afcee6e3e4ef550", "name": "例3: 100", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "100", "payloadType": "num", "x": 170, "y": 600, "wires": [ [ "104f6f14.1c72e1" ] ] }, { "id": "808e354d.8de148", "type": "comment", "z": "2afcee6e3e4ef550", "name": "Scale and wrap input value", "info": "Range node can map input value to output value according to mapping specification.\nThe result value is wrapped if `Scale and wrap within the target range` is selected.", "x": 170, "y": 660, "wires": [] }, { "id": "2f033c72.51dcc4", "type": "inject", "z": "2afcee6e3e4ef550", "name": "例4: 0", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "0", "payloadType": "num", "x": 170, "y": 720, "wires": [ [ "cdaa82c4.820de" ] ] }, { "id": "cdaa82c4.820de", "type": "range", "z": "2afcee6e3e4ef550", "minin": "0", "maxin": "9", "minout": "0", "maxout": "90", "action": "roll", "round": false, "property": "payload", "name": "", "x": 330, "y": 720, "wires": [ [ "77f8872b.2a9968" ] ] }, { "id": "77f8872b.2a9968", "type": "debug", "z": "2afcee6e3e4ef550", "name": "例4: 結果", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "false", "statusVal": "", "statusType": "auto", "x": 500, "y": 720, "wires": [] }, { "id": "84ac6a7e.77b8d8", "type": "inject", "z": "2afcee6e3e4ef550", "name": "例4: 10", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "10", "payloadType": "num", "x": 170, "y": 760, "wires": [ [ "cdaa82c4.820de" ] ] }, { "id": "92f554c3.b08ad8", "type": "inject", "z": "2afcee6e3e4ef550", "name": "例4: 100", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "100", "payloadType": "num", "x": 170, "y": 800, "wires": [ [ "cdaa82c4.820de" ] ] } ]

フローの読み解き

フローには、公式サンプルの4つの例が、上から順に並んでいます。それぞれの先頭に、英語の comment ノードがあります。どの例も、3つの inject → range → debug という同じ並びで、違うのは range の設定だけです。名前の無かった inject と debug には、「例1: 512」「例1: 結果」のように、例の番号を付けた名前を付けてあります。inject の名前の数が、そのボタンが送る msg.payload の値です。

例 comment ノードの見出し(意味) range の設定 ボタンと、「例N: 結果」に出る値
例1 Map property between different numeric ranges(値を、別の数の範囲に写す) 入力値の範囲 0〜1023、出力値の範囲 0〜5。「動作」は「入力値の範囲外の値を最小値/最大値とし拡大/縮小」。四捨五入はオフ 0 → 0、512 → 2.5024437927663734、1023 → 5
例2 Scale input value & round result to integer(入力を拡大/縮小し、結果を整数に丸める) 入力値の範囲 0〜9、出力値の範囲 0〜128。「動作」は「msg.payloadの値を拡大/縮小」。「小数値を四捨五入し整数値へ変換」がオン 0 → 0、10 → 142、99 → 1408
例3 Limit input value(入力の値を制限する) 入力値の範囲 0〜100、出力値の範囲 0〜90。「動作」は「入力値の範囲外の値を最小値/最大値とし拡大/縮小」 0 → 0、10 → 9、100 → 90
例4 Scale and wrap input value(入力を拡大/縮小し、折り返す) 入力値の範囲 0〜9、出力値の範囲 0〜90。「動作」は「入力値の範囲外の値を範囲幅で割った余りとし拡大/縮小」 0 → 0、10 → 10、100 → 10

それぞれの例で確かめることは、次のとおりです。

応用(サイト独自): 「動作」の比較と温度変換

ここから先は公式サンプルではなく、本サイトが用意した応用教材です。同じ入力を3つの「動作」で変換して比べる部分と、摂氏を華氏に変換する部分があり、下のパターン1・2の実例になっています。

📥 応用フローJSON(クリックで展開)
[ { "id": "range_sample_tab", "type": "tab", "label": "Range サンプル", "disabled": false, "info": "" }, { "id": "range_comment", "type": "comment", "z": "range_sample_tab", "name": "📏 Range ノード サンプル", "info": "様々なRange変換のデモンストレーション", "x": 160, "y": 40, "wires": [] }, { "id": "inject_50", "type": "inject", "z": "range_sample_tab", "name": "50を送信", "props": [{"p": "payload"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "50", "payloadType": "num", "x": 140, "y": 100, "wires": [["range_basic"]] }, { "id": "range_basic", "type": "range", "z": "range_sample_tab", "minin": "0", "maxin": "100", "minout": "0", "maxout": "255", "action": "scale", "round": true, "property": "payload", "name": "0-100 → 0-255", "x": 340, "y": 100, "wires": [["debug_basic"]] }, { "id": "debug_basic", "type": "debug", "z": "range_sample_tab", "name": "基本変換結果", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "statusVal": "payload", "statusType": "auto", "x": 560, "y": 100, "wires": [] }, { "id": "comment_limit", "type": "comment", "z": "range_sample_tab", "name": "━━━ 範囲制限デモ ━━━", "info": "", "x": 160, "y": 160, "wires": [] }, { "id": "inject_150", "type": "inject", "z": "range_sample_tab", "name": "150を送信(範囲外)", "props": [{"p": "payload"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "150", "payloadType": "num", "x": 170, "y": 220, "wires": [["range_limit", "range_scale", "range_wrap"]] }, { "id": "range_limit", "type": "range", "z": "range_sample_tab", "minin": "0", "maxin": "100", "minout": "0", "maxout": "255", "action": "clamp", "round": true, "property": "payload", "name": "クランプ", "x": 410, "y": 180, "wires": [["debug_limit"]] }, { "id": "debug_limit", "type": "debug", "z": "range_sample_tab", "name": "クランプ: 255", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "statusVal": "payload", "statusType": "auto", "x": 590, "y": 180, "wires": [] }, { "id": "range_scale", "type": "range", "z": "range_sample_tab", "minin": "0", "maxin": "100", "minout": "0", "maxout": "255", "action": "scale", "round": true, "property": "payload", "name": "拡大/縮小のみ", "x": 410, "y": 220, "wires": [["debug_scale"]] }, { "id": "debug_scale", "type": "debug", "z": "range_sample_tab", "name": "拡大/縮小: 383", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "statusVal": "payload", "statusType": "auto", "x": 590, "y": 220, "wires": [] }, { "id": "range_wrap", "type": "range", "z": "range_sample_tab", "minin": "0", "maxin": "100", "minout": "0", "maxout": "255", "action": "roll", "round": true, "property": "payload", "name": "折り返し", "x": 410, "y": 260, "wires": [["debug_wrap"]] }, { "id": "debug_wrap", "type": "debug", "z": "range_sample_tab", "name": "折り返し: 128", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "statusVal": "payload", "statusType": "auto", "x": 590, "y": 260, "wires": [] }, { "id": "comment_temp", "type": "comment", "z": "range_sample_tab", "name": "━━━ 温度変換デモ ━━━", "info": "", "x": 160, "y": 320, "wires": [] }, { "id": "inject_25c", "type": "inject", "z": "range_sample_tab", "name": "25°C", "props": [{"p": "payload"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "25", "payloadType": "num", "x": 130, "y": 380, "wires": [["range_temp"]] }, { "id": "range_temp", "type": "range", "z": "range_sample_tab", "minin": "0", "maxin": "100", "minout": "32", "maxout": "212", "action": "scale", "round": false, "property": "payload", "name": "摂氏→華氏", "x": 310, "y": 380, "wires": [["debug_temp"]] }, { "id": "debug_temp", "type": "debug", "z": "range_sample_tab", "name": "77°F", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "statusVal": "payload", "statusType": "auto", "x": 490, "y": 380, "wires": [] } ]

フローの読み解き

応用フローは、3つの部分に分かれています。2つ目と3つ目の部分は、先頭の comment ノードの名前で見分けられます。

部分 ノードの並び range の設定と、ボタンを押すと出るもの パターン
基本の変換(先頭の「📏 Range ノード サンプル」のすぐ下) 「50を送信」(inject)→「0-100 → 0-255」(range)→「基本変換結果」(debug) 入力値の範囲 0〜100、出力値の範囲 0〜255、四捨五入がオン。50 を送ると、127.5 が四捨五入されて 128 が出る ―(下のパターンには無い)
━━━ 範囲制限デモ ━━━ 「150を送信(範囲外)」(inject)から、「クランプ」「拡大/縮小のみ」「折り返し」の3つの range へ同じ msg を送り、それぞれの debug に出す 3つの range は、範囲(0〜100 → 0〜255)と四捨五入(オン)が同じで、「動作」だけが違う。150 を送ると、「クランプ: 255」に 255、「拡大/縮小: 383」に 383、「折り返し: 128」に 128 が出る。debug の名前に、出るはずの値を書いてある パターン1
━━━ 温度変換デモ ━━━ 「25°C」(inject)→「摂氏→華氏」(range)→「77°F」(debug) 入力値の範囲 0〜100、出力値の範囲 32〜212、四捨五入はオフ。出力値の範囲を 0 から始めないことで、掛け算と足し算(× 1.8 + 32)を1つのノードで行っている。25 を送ると 77 が出る パターン2

「動作」の4つ目の選択肢(値を拡大/縮小(入力範囲外の時はメッセージを削除))を使う range ノードは、公式サンプルにも応用フローにも含まれていません。

パターン1: 範囲制限モードの比較

応用フローの「範囲制限デモ」を参照してください。同じ入力値(150)に対して、3つのモードで異なる結果になります。

用途: 範囲外の入力値をどう扱うかを3つの方式で比較

150を送信 → クランプ → 255(上限に制限)

3つの動作の違い:

「動作」の選択肢入力150(範囲外)の結果説明
入力値の範囲外の値を最小値/最大値とし拡大/縮小255出力範囲の端に制限(クランプ)
msg.payloadの値を拡大/縮小383制限せず比例のまま変換
入力値の範囲外の値を範囲幅で割った余りとし拡大/縮小128範囲内に折り返し(循環)

パターン2: 温度変換(摂氏→華氏)

応用フローの「温度変換デモ」を参照してください。

用途: 摂氏の温度を華氏に変換

25°C → 摂氏→華氏 → 77°F

設定:

4. 演習問題

演習1: 基本的な範囲変換 初級

課題: 0〜100の入力を0〜255のPWM値に変換するフローを作成してください。

💼 使いどころ: パーセント値をLEDの明るさやモーターの速度を指定するPWM値に変換する場面で使います。

要求仕様:

成功の条件:

💡 ヒント

range ノードで「入力値の範囲」を 0-100、「出力値の範囲」を 0-255 に設定し、「小数値を四捨五入し整数値へ変換」にチェックを入れます。

✅ 解答例フロー
[ {"id": "ex1_tab", "type": "tab", "label": "演習1"}, {"id": "ex1_inject", "type": "inject", "z": "ex1_tab", "name": "75%", "props": [{"p": "payload"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "75", "payloadType": "num", "x": 130, "y": 100, "wires": [["ex1_range"]]}, {"id": "ex1_range", "type": "range", "z": "ex1_tab", "minin": "0", "maxin": "100", "minout": "0", "maxout": "255", "action": "scale", "round": true, "property": "payload", "name": "0-100 → 0-255", "x": 320, "y": 100, "wires": [["ex1_debug"]]}, {"id": "ex1_debug", "type": "debug", "z": "ex1_tab", "name": "PWM値", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "statusVal": "payload", "statusType": "auto", "x": 510, "y": 100, "wires": []} ]

演習2: センサー値の正規化 初級

課題: 12bit ADC(0〜4095)の値を0〜100%に正規化してください。

💼 使いどころ: 12bit ADCなどセンサーの生の値を0〜100%の指標に変換する場面で使います。

要求仕様:

成功の条件:

💡 ヒント

「小数値を四捨五入し整数値へ変換」をオフにすると小数点以下も保持されます(2048 は 50.01221… になります)。小数点以下1桁にそろえるには、後ろに Change ノードを置き、「値の代入」で msg.payload に「JSONata式」型の $round(payload, 1) を設定します。$round() の2つ目の引数が、残す小数点以下の桁数です。

✅ 解答例フロー
[ {"id": "ex2_tab", "type": "tab", "label": "演習2"}, {"id": "ex2_inject", "type": "inject", "z": "ex2_tab", "name": "2048 (50%)", "props": [{"p": "payload"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "2048", "payloadType": "num", "x": 150, "y": 100, "wires": [["ex2_range"]]}, {"id": "ex2_range", "type": "range", "z": "ex2_tab", "minin": "0", "maxin": "4095", "minout": "0", "maxout": "100", "action": "clamp", "round": false, "property": "payload", "name": "ADC→%", "x": 330, "y": 100, "wires": [["ex2_change"]]}, {"id": "ex2_change", "type": "change", "z": "ex2_tab", "name": "小数点1桁", "rules": [{"t": "set", "p": "payload", "pt": "msg", "to": "$round(payload, 1)", "tot": "jsonata"}], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 500, "y": 100, "wires": [["ex2_debug"]]}, {"id": "ex2_debug", "type": "debug", "z": "ex2_tab", "name": "%表示", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "statusVal": "payload", "statusType": "auto", "x": 670, "y": 100, "wires": []} ]

演習3: 範囲制限の活用 中級

課題: モーター制御で、入力が範囲外でも安全な値に制限するフローを作成してください。

💼 使いどころ: 速度指令が想定範囲を超えても上限・下限に収めて安全な出力を保つ場面で使います。

要求仕様:

成功の条件:

💡 ヒント

「動作」で「入力値の範囲外の値を最小値/最大値とし拡大/縮小」を選ぶと、範囲外の値は範囲の端に制限されます。

✅ 解答例フロー
[ {"id": "ex3_tab", "type": "tab", "label": "演習3"}, {"id": "ex3_inject1", "type": "inject", "z": "ex3_tab", "name": "50(正常)", "props": [{"p": "payload"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "50", "payloadType": "num", "x": 150, "y": 80, "wires": [["ex3_range"]]}, {"id": "ex3_inject2", "type": "inject", "z": "ex3_tab", "name": "150(範囲外)", "props": [{"p": "payload"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "150", "payloadType": "num", "x": 160, "y": 120, "wires": [["ex3_range"]]}, {"id": "ex3_inject3", "type": "inject", "z": "ex3_tab", "name": "-50(負の値)", "props": [{"p": "payload"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "-50", "payloadType": "num", "x": 160, "y": 160, "wires": [["ex3_range"]]}, {"id": "ex3_range", "type": "range", "z": "ex3_tab", "minin": "-100", "maxin": "100", "minout": "0", "maxout": "255", "action": "clamp", "round": true, "property": "payload", "name": "速度→PWM(制限付き)", "x": 400, "y": 120, "wires": [["ex3_debug"]]}, {"id": "ex3_debug", "type": "debug", "z": "ex3_tab", "name": "PWM出力", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "statusVal": "payload", "statusType": "auto", "x": 620, "y": 120, "wires": []} ]

演習4: 温度スケール変換 中級

課題: 摂氏温度を華氏温度に変換するフローを作成してください。

💼 使いどころ: 摂氏で測った温度を華氏表記が必要な機器や資料に合わせて変換する場面で使います。

要求仕様:

成功の条件:

💡 ヒント

入力範囲を -40〜100(摂氏)、出力範囲を -40〜212(華氏)に設定。-40度は摂氏と華氏で同じ値です。

✅ 解答例フロー
[ {"id": "ex4_tab", "type": "tab", "label": "演習4"}, {"id": "ex4_inject1", "type": "inject", "z": "ex4_tab", "name": "0°C", "props": [{"p": "payload"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "0", "payloadType": "num", "x": 130, "y": 80, "wires": [["ex4_range"]]}, {"id": "ex4_inject2", "type": "inject", "z": "ex4_tab", "name": "25°C", "props": [{"p": "payload"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "25", "payloadType": "num", "x": 130, "y": 120, "wires": [["ex4_range"]]}, {"id": "ex4_inject3", "type": "inject", "z": "ex4_tab", "name": "100°C", "props": [{"p": "payload"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "100", "payloadType": "num", "x": 140, "y": 160, "wires": [["ex4_range"]]}, {"id": "ex4_range", "type": "range", "z": "ex4_tab", "minin": "0", "maxin": "100", "minout": "32", "maxout": "212", "action": "scale", "round": false, "property": "payload", "name": "摂氏→華氏", "x": 330, "y": 120, "wires": [["ex4_debug"]]}, {"id": "ex4_debug", "type": "debug", "z": "ex4_tab", "name": "華氏温度", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "statusVal": "payload", "statusType": "auto", "x": 520, "y": 120, "wires": []} ]

5. まとめ

重要ポイント:

注意事項:

6. トラブルシューティング

問題原因解決方法
出力が NaN になる入力が数値でないChange ノードで数値型に変換
期待通りの値にならない範囲設定ミス入力/出力範囲を確認
小数が出力されるRound が無効Round にチェックを入れる
範囲外の値が出力される「動作」が「msg.payloadの値を拡大/縮小」「入力値の範囲外の値を最小値/最大値とし拡大/縮小」に変更

7. 実務活用例

ケース1: IoTセンサーの値変換

アナログセンサーの生データを人間が理解しやすい単位に変換。例:ADC値→温度、電圧→圧力など。

ケース2: モーター/LED制御

UIのスライダー値(0-100)をPWMデューティ比(0-255)に変換して、モーターやLEDを制御。

ケース3: データの正規化

異なるスケールのセンサーデータを統一した範囲に正規化して、比較や可視化を容易に。

ケース4: プロトコル変換

異なる機器間でのデータ形式変換。例:0-10Vアナログ信号を0-100%のデジタル値に変換。

📚 Node-RED 公式ドキュメント

🏠