0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Node-REDに「今何時?」サブフローを作る

Last updated at Posted at 2021-06-05

はじめに

Node-REDでスマートホーム(家電制御)をする際に実際に使ってる「ちょいテク」のご紹介です。
標準ノードを使用したサブフローなので、追加のノードも必要ありません。

環境

  • Node-RED v1.3.5

サブフロー作成

サブフロー内はChangeノード1つです。

スクリーンショット 2021-06-04 12.09.41.png

Changeノードの中身は以下のようになっています。
環境変数ZONEの設定が前提になっており、サブフローのテンプレートで設定できるようにします。(後述)
他はJSONataで変数に設定します。

msg.hour:時を代入します。(数値)
msg.min:分を代入します。(数値)
msg.year:年を代入します。(数値)
msg.month:月を代入します。(数値)
msg.day:日を代入します。(数値)
msg.dow:曜日を代入します。(英語文字列)

スクリーンショット 2021-06-04 12.09.17.png

サブフローのテンプレートは以下のようになっています。
(TIME)ZONEをドロップダウンリストで指定できるようにしておきます。個人的には「+0900」しか使わないので、「+0900」しか定義していません。

スクリーンショット 2021-06-04 12.08.04.png

サブフローを書き出したJSONは以下になります。

[{"id":"32238322.9dad8c","type":"subflow","name":"今何時?","info":"\n**msg.hour**:時を代入します。(数値)\n\n**msg.min**:分を代入します。(数値)\n\n**msg.year**:年を代入します。(数値)\n\n**msg.month**:月を代入します。(数値)\n\n**msg.day**:日を代入します。(数値)\n\n**msg.dow**:曜日を代入します。(英語文字列)","category":"","in":[{"x":60,"y":60,"wires":[{"id":"82d3f0aa.7008e"}]}],"out":[{"x":330,"y":60,"wires":[{"id":"82d3f0aa.7008e","port":0}]}],"env":[{"name":"ZONE","type":"str","value":"+0900","ui":{"icon":"font-awesome/fa-clock-o","label":{"en-US":"タイムゾーン"},"type":"select","opts":{"opts":[{"l":{"en-US":"Asia/Tokyo"},"v":"+0900"}]}}}],"meta":{},"color":"#C0DEED","icon":"font-awesome/fa-clock-o"},{"id":"82d3f0aa.7008e","type":"change","z":"32238322.9dad8c","name":"","rules":[{"t":"set","p":"zone","pt":"msg","to":"ZONE","tot":"env"},{"t":"set","p":"millis","pt":"msg","to":"$millis()","tot":"jsonata"},{"t":"set","p":"hour","pt":"msg","to":"$number($fromMillis(millis,'[H01]',zone))","tot":"jsonata"},{"t":"set","p":"min","pt":"msg","to":"$number($fromMillis(millis,'[m01]',zone))","tot":"jsonata"},{"t":"set","p":"dow","pt":"msg","to":"$fromMillis(millis,'[F]',zone)","tot":"jsonata"},{"t":"set","p":"year","pt":"msg","to":"$number($fromMillis(millis,'[Y]',zone))","tot":"jsonata"},{"t":"set","p":"month","pt":"msg","to":"$number($fromMillis(millis,'[M]',zone))","tot":"jsonata"},{"t":"set","p":"day","pt":"msg","to":"$number($fromMillis(millis,'[D]',zone))","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":200,"y":60,"wires":[[]]},{"id":"7afe6529.4b24ec","type":"subflow:32238322.9dad8c","z":"c33d685b.315298","name":"","x":740,"y":640,"wires":[[]]}]

使い方の例

インジェクトノードでうまく繰り返し処理が指定できない場合に有効です。
私は主に時刻により処理内容が変化するものについて、フローが無駄に増えないよう集約させるために使っています。

スクリーンショット 2021-06-04 12.45.09.png

フローを増やさない事は、複数のインジェクトノードで処理が重なるのを未然に防ぐ事にもなるので、うまく活用してます。

おわりに

スマートホームでは時刻取得をよく使うので、このようなノード(サブフロー)を作っておくとめちゃくちゃ便利です。

Node-REDが想定するユーザ層はよくわかりませんが、日時を扱う際はfunctionノードを使うかJSONataを使うかの2択になってしまいます。functionノードの利用は「ローコード」とは言えませんし、JavaScript言語が使える事で導入した人にJSONataというのは扱い易いとは言えないです。
結局は「調べて使う」=「調べられる人」が前提のユーザを対象にしているのかもしれませんが、個人的には初心者の方にも広く使ってもらいたいですね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?