Amazon Echoから家電を制御 3 ~テレビを我が手中に収めてやった ch操作編~

# 基本操作に続き
今度はchを操作します。
やることは[Amazon Echoから家電を制御 2 ~テレビを我が手中に収めてやった 基本操作編~](https://qiita.com/tstkkmd/items/75771caeac9f00f3b668)と同じことの繰り返しになります。


# テレビのchを制御するフロー
<img width="1067" alt="スクリーンショット 2018-07-30 11.26.13.png" src="https://qiita-image-store.s3.amazonaws.com/0/231763/dc45f308-daa0-8659-794c-13c6a2487e7b.png">

### linkノード
[Amazon Echoから家電を制御 2 ~テレビを我が手中に収めてやった 基本操作編~](https://qiita.com/tstkkmd/items/75771caeac9f00f3b668)で書いたフローから仮想的にリンクしています。
一つのフローとして描くと、とんでもなく要塞化してしまいますので便利です。

### changeノード briをpayloadに移動
msg.briの値をmsg.payloadに渡しています。

### switchノード
直前のノードから渡されたmsg.payloadの値によって分岐します。
chとしてありえない値には画面番号的に特別な機能を持たせてあります。

* "90"〜"92" 地デジ、HDMI1、HDMI2に切り替えます。
* "98" ザッピングします。2周したら番組表を表示します。
* "99" 番組表を表示します。
* "100" OKを発射してからmuteを発射します。FireTVを視聴中に電話に出る際に使っています。

この辺りの機能はテレビの機種や、ライフスタイルによって変化する部分かと思います。

### execノード
ch.sh以外は直接引数を書いて決め打ちで発射しています。
ディレクトリは以下のように。

```bash
.
├── CH
│   ├── 1
│   ├── 10
│   ├── 11
│   ├── 12
│   ├── 2
│   ├── 20 -> ../twinview
│   ├── 3
│   ├── 4
│   ├── 5
│   ├── 6
│   ├── 7
│   ├── 8
│   ├── 9
│   ├── 90 -> ../chidegi
│   ├── 91 -> ../hdmi1
│   ├── 92 -> ../hdmi2
│   ├── 99 -> ../epg
│   ├── down
│   └── up
├── back
├── bs
├── chidegi
├── data
├── epg
├── hdmi1
├── hdmi2
├── input
├── mute
├── off -> poweroff
├── ok
├── on -> poweron
├── power
├── poweroff
├── poweron
├── rec
├── tvsleep
├── twinview
├── voldown
└── volup
```

録画やデータ放送なども以前は設定していましたが、実際使ってみるとそんなに便利じゃなかったので今は設定していません。
チャンネル変更や特別なことをする時はこれを使い、

```bash:ch.sh
#!/bin/bash
cd /home/hoge/python-broadlink/cli
./broadlink_cli --device @ROOM.device --send @TV/CH/$1
```

基本的な操作をする時はこれを使います。

```bash:tv.sh
#!/bin/bash
cd /home/hoge/python-broadlink/cli
./broadlink_cli --device @ROOM.device --send @TV/$1
```

ご覧の通り、違いは引数を参照するディレクトリしか違いません。


# I have!!

```json
[{"id":"21599434.1f0c7c","type":"tab","label":"ch","disabled":false,"info":""},{"id":"b1c2727e.40586","type":"delay","z":"21599434.1f0c7c","name":"1.25s","pauseType":"delay","timeout":"1250","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":810,"y":220,"wires":[["c1ba7d99.f39e"]]},{"id":"f9d41b9.c297e68","type":"exec","z":"21599434.1f0c7c","command":"/home/hoge/python-broadlink/cli/tv.sh","addpay":false,"append":"mute","useSpawn":"false","timer":"","oldrc":false,"name":"mute","x":1010,"y":60,"wires":[[],[],[]]},{"id":"1cffb144.49b167","type":"exec","z":"21599434.1f0c7c","command":"/home/hoge/python-broadlink/cli/tv.sh","addpay":false,"append":"ok","useSpawn":"false","timer":"","oldrc":false,"name":"ok","x":810,"y":60,"wires":[["f9d41b9.c297e68"],[],[]]},{"id":"cbf101b2.562e88","type":"change","z":"21599434.1f0c7c","name":"briをpayloadに移動","rules":[{"t":"move","p":"bri","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":270,"y":300,"wires":[["f4c233fe.2d3cf","55f0d8d4.c7215"]]},{"id":"c1ba7d99.f39e","type":"exec","z":"21599434.1f0c7c","command":"/home/hoge/python-broadlink/cli/ch.sh","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"ch.sh","x":1010,"y":260,"wires":[[],[],[]]},{"id":"a1ad9cd8.d4e52","type":"exec","z":"21599434.1f0c7c","command":"/home/hoge/python-broadlink/cli/tv.sh","addpay":false,"append":"chidegi","useSpawn":"false","timer":"","oldrc":false,"name":"地デジ","x":810,"y":140,"wires":[[],[],[]]},{"id":"f4c233fe.2d3cf","type":"switch","z":"21599434.1f0c7c","name":"1-100","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"100","vt":"num"},{"t":"btwn","v":"1","vt":"num","v2":"12","v2t":"num"},{"t":"btwn","v":"13","vt":"num","v2":"97","v2t":"num"},{"t":"eq","v":"99","vt":"num"},{"t":"eq","v":"98","vt":"num"}],"checkall":"true","repair":false,"outputs":5,"x":450,"y":300,"wires":[["1cffb144.49b167"],["a1ad9cd8.d4e52","b1c2727e.40586"],["c1ba7d99.f39e"],["c1ba7d99.f39e"],["65a5ace5.5019a4"]]},{"id":"65a5ace5.5019a4","type":"template","z":"21599434.1f0c7c","name":"i=0","field":"i","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"0","output":"str","x":630,"y":380,"wires":[["86b78717.a5174"]]},{"id":"9f5722a1.c5d6d8","type":"exec","z":"21599434.1f0c7c","command":"/home/hoge/python-broadlink/cli/ch.sh","addpay":false,"append":"1","useSpawn":"false","timer":"","oldrc":false,"name":"1","x":630,"y":520,"wires":[["cf0a4d4f.152fd8"],[],[]]},{"id":"86b78717.a5174","type":"switch","z":"21599434.1f0c7c","name":"i<2","property":"i","propertyType":"msg","rules":[{"t":"else"},{"t":"lt","v":"2","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":810,"y":380,"wires":[["96ca31ef.b22d5"],["2a29632.b04709c"]]},{"id":"985d1544.e938a8","type":"function","z":"21599434.1f0c7c","name":"i++","func":"msg.i++;\n\nreturn msg;","outputs":1,"noerr":0,"x":630,"y":440,"wires":[["86b78717.a5174"]]},{"id":"96ca31ef.b22d5","type":"exec","z":"21599434.1f0c7c","command":"/home/hoge/python-broadlink/cli/tv.sh","addpay":false,"append":"epg","useSpawn":"false","timer":"","oldrc":false,"name":"番組表","x":1010,"y":380,"wires":[[],[],[]]},{"id":"673e65d1.b6f32c","type":"exec","z":"21599434.1f0c7c","command":"/home/hoge/python-broadlink/cli/ch.sh","addpay":false,"append":"2","useSpawn":"false","timer":"","oldrc":false,"name":"2","x":1010,"y":520,"wires":[["b8a265c8.6bab3"],[],[]]},{"id":"d196266d.94bf18","type":"exec","z":"21599434.1f0c7c","command":"/home/hoge/python-broadlink/cli/ch.sh","addpay":false,"append":"4","useSpawn":"false","timer":"","oldrc":false,"name":"4","x":1010,"y":640,"wires":[["53744e6.1c21eb"],[],[]]},{"id":"5741e0b9.3426e8","type":"exec","z":"21599434.1f0c7c","command":"/home/hoge/python-broadlink/cli/ch.sh","addpay":false,"append":"5","useSpawn":"false","timer":"","oldrc":false,"name":"5","x":1010,"y":760,"wires":[["985d1544.e938a8"],[],[]]},{"id":"5f29dc7d.557ab4","type":"exec","z":"21599434.1f0c7c","command":"/home/hoge/python-broadlink/cli/ch.sh","addpay":false,"append":"6","useSpawn":"false","timer":"","oldrc":false,"name":"6","x":630,"y":640,"wires":[["408b8f3f.073c9"],[],[]]},{"id":"6b2bba4a.a23134","type":"exec","z":"21599434.1f0c7c","command":"/home/hoge/python-broadlink/cli/ch.sh","addpay":false,"append":"8","useSpawn":"false","timer":"","oldrc":false,"name":"8","x":630,"y":760,"wires":[["9b2ea04d.7a38c"],[],[]]},{"id":"cf0a4d4f.152fd8","type":"delay","z":"21599434.1f0c7c","name":"","pauseType":"delay","timeout":"6","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":820,"y":520,"wires":[["673e65d1.b6f32c"]]},{"id":"b8a265c8.6bab3","type":"delay","z":"21599434.1f0c7c","name":"","pauseType":"delay","timeout":"6","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":820,"y":580,"wires":[["5f29dc7d.557ab4"]]},{"id":"408b8f3f.073c9","type":"delay","z":"21599434.1f0c7c","name":"","pauseType":"delay","timeout":"6","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":820,"y":640,"wires":[["d196266d.94bf18"]]},{"id":"53744e6.1c21eb","type":"delay","z":"21599434.1f0c7c","name":"","pauseType":"delay","timeout":"6","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":820,"y":700,"wires":[["6b2bba4a.a23134"]]},{"id":"9b2ea04d.7a38c","type":"delay","z":"21599434.1f0c7c","name":"","pauseType":"delay","timeout":"6","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":820,"y":760,"wires":[["5741e0b9.3426e8"]]},{"id":"2a29632.b04709c","type":"delay","z":"21599434.1f0c7c","name":"","pauseType":"delay","timeout":"6","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":820,"y":460,"wires":[["9f5722a1.c5d6d8"]]},{"id":"470b1eb8.4d67e","type":"link in","z":"21599434.1f0c7c","name":"TV->","links":["a80096a5.8428a8"],"x":135,"y":300,"wires":[["cbf101b2.562e88"]]},{"id":"55f0d8d4.c7215","type":"debug","z":"21599434.1f0c7c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":450,"y":120,"wires":[]}]
```


# 今後の課題
msg.payload="98"の時にtemplateノードに流れてch1〜ch8を6秒ごとに切り替え、テレビ任せでザッピングします。
フローが特に要塞っぽくなっている原因ですので、なんとかノードを横に繋ぐだけで単純化というか、スッキリした見た目にしたいところです。


# まとめ
おおよそ横にノードを繋ぐだけで普段テレビでしていることをalexa任せにできました。
excelでifやvlookup程度の知識しかない私でも、IoT的な遊びができるNode-REDはほんと助かります。


# 投稿の数日後、増築しました
要塞感溢れるフローですが、要塞の増築をしました。
amazon Echoは全く経由しませんが、iPhoneやMac、windowsのwebブラウザから家電を制御します。
[node-red-dashboardでwebリモコンを作ってみた](https://qiita.com/tstkkmd/items/5d205c8aacf32b9e3ecf)
コードを書かないでなんとかする。という理念の元、実現できました。


# Amazon Echoで遊ぼう
### 前提環境
[大凡1.5ｈで構築するraspberry pi + node-red + samba](https://qiita.com/tstkkmd/items/2c6dfde68ebbc90b0bb1)
### Amazon Echoから家電を制御シリーズ
[Amazon Echoから家電を制御 0 ~RMmini3 + python-broadlinkで学習~](https://qiita.com/tstkkmd/items/71db748dc75127a84fe8)
[Amazon Echoから家電を制御 1 ~node-red-contrib-alexa-local~](https://qiita.com/tstkkmd/items/10fb586be83de96bfe24)
[Amazon Echoから家電を制御 1.25 ~node-red-contrib-alexa-localの日本語の情報が少ないのでもう少し調べてみる~](https://qiita.com/tstkkmd/items/6884e38dd6a4f9965e42)
[Amazon Echoから家電を制御 2 ~テレビを我が手中に収めてやった 基本操作編~](https://qiita.com/tstkkmd/items/75771caeac9f00f3b668)
[Amazon Echoから家電を制御 3 ~テレビを我が手中に収めてやった ch操作編~](https://qiita.com/tstkkmd/items/a8c7a5ab001d6e299366)
[node-red-dashboardでwebリモコンを作ってみた](https://qiita.com/tstkkmd/items/5d205c8aacf32b9e3ecf)