Home 【連載記事】Azureを使って一人暮らしの父を見守る(3)Logic Appsのタイマーで定期チェック偏
Post
Cancel

【連載記事】Azureを使って一人暮らしの父を見守る(3)Logic Appsのタイマーで定期チェック偏

はじめに

一人暮らしの父(80歳Over)を見守るため、Webカメラで動体監視を行い、毎日リビングで過ごしているかを見守るシステムを構築します。

何本かに記事を分けて構築を進めましたが、今回が最終回で以下の構成になりました。

今回の記事では黄色の範囲について説明しています。

img

本エントリでは、Azure Logic AppsのタイマートリガーとLINEとの連携にスポットを当てています。
動体検知イベントを受けた後のLogic AppsとLINEとの連携については「(2)Logic AppsとLINE連携偏」を参照ください。
WebカメラとAzure Logic Appsとの連携については「(1)WebカメラとLogic Appsの連携偏」を参照ください。

今回やること

前回までの構築で、以下のことはできるようになりました。

  • 動体検知時にLINEに毎回通知
  • LINEにメッセージを書くと、前回検知日時を返信

このままですと、日中動体検知の度にLINEに通知が来てしまい、正直それはそれで面倒です。
ただし、朝一番に父がリビングへ下りてきた際には「あっ、今日もいつもの時間に起きてきたナ」を知りたいため、通知させようと思います。他に、外出予定でもないのに何時間も日中に動体検知がされていない場合も心配なので通知させようと思います。

なので、以下の機能を追加します。

  • 朝イチの動体検知時のみLINEに通知
  • 定期的に前回動体検知時間を見て、一定時間経っているときにもLINEに通知

朝イチのみLINEへ通知

日付が変わった場合の初回のみという判定は、前回までに作成したLogic Appsに、動体検知イベントのLINEへ通知前に一つ判定ロジックを追加しました。

img

条件の中身は以下のとおりですが、Azure Table Storageから取ってきた前回の動体検知日時と現在日時それぞれの「曜日」を0~6の値で取得して、「異なったら」日が変わった(朝イチ)と判定しています。

1
2
dayOfWeek(items('For_each_4')?['datetime'])
dayOfWeek(body('changetimezone'))

一週間まるまる動体検知イベントが無かったら誤動作すると思いますが、それは運用でカバーしますw

この条件を入れるだけで、一日になんどもLINE通知は来ることがなくなりました。

定期チェック

新規に同じAzureリソースグループ内にLogic Appsを作成します。

トリガーは繰り返しで毎時1回起動するようにしました。

img

現在時刻を日本時間に変換するときに、2つ使っています。片方は年月日時分秒取っていますが、もう片方は時間のみ取得しています。

img

この後の条件分岐で、現在「時」が6時~20時の間だけ動作するようにしました。夜中や早朝にLINEで通知されてもしんどいので。

img

その後は前回の記事にあるように、Azure Table Storageから前回動体検知日時を取得して、経過時間が120分以上だったときにLINEで通知を送るようにしました。

img

動作確認

文面はこれから変更はすると思いますが、とりあえず目的は達成できたかんじです。

朝イチの動体検知通知と、LINEメッセージ送信後の返信

img

定期チェックで一定時間以上動体検知していない場合の通知メッセージ(画面は開発中のため5分以上で通知されています)

img

おわりに

とりあえず完成させることができましたが、Logic Appsを本格的にやっていなかったため、関数の指定方法など色々苦労もありました。

個人的にはFunctionsでPython使ったほうが早く書けた気もしますが、ノーコードで(私にとっては)実用的なシステムが手軽に作れたと思います。

付録:Logic Appsのコード

このまままるっと再利用できるとは思いませんが、今回構築したLogic Appsのコードを以下に示します。
アクションの名前とかデフォルトのままだったり、気分で変更していたりとなかなか汚いコードですが、参考になればと。

[イベント取得時]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "For_each": {
                "actions": {
                    "条件": {
                        "actions": {
                            "For_each_4": {
                                "actions": {
                                    "条件_2": {
                                        "actions": {
                                            "For_each_3": {
                                                "actions": {
                                                    "HTTP_2": {
                                                        "inputs": {
                                                            "body": {
                                                                "messages": [
                                                                    {
                                                                        "text": "本日初めて動体検知しました。\n@{body('changetimezone')}",
                                                                        "type": "text"
                                                                    }
                                                                ]
                                                            },
                                                            "headers": {
                                                                "Authorization": "Bearer ********=",
                                                                "Content-Type": "application/json"
                                                            },
                                                            "method": "POST",
                                                            "uri": "https://api.line.me/v2/bot/message/broadcast"
                                                        },
                                                        "runAfter": {},
                                                        "type": "Http"
                                                    }
                                                },
                                                "foreach": "@body('JSON_の解析_2')?['value']",
                                                "runAfter": {},
                                                "type": "Foreach"
                                            }
                                        },
                                        "expression": {
                                            "and": [
                                                {
                                                    "not": {
                                                        "equals": [
                                                            "@dayOfWeek(items('For_each_4')?['datetime'])",
                                                            "@dayOfWeek(body('changetimezone'))"
                                                        ]
                                                    }
                                                }
                                            ]
                                        },
                                        "runAfter": {},
                                        "type": "If"
                                    }
                                },
                                "foreach": "@body('JSON_の解析_2')?['value']",
                                "runAfter": {
                                    "最新のモーション検出日時を保存": [
                                        "Succeeded"
                                    ]
                                },
                                "type": "Foreach"
                            },
                            "JSON_の解析_2": {
                                "inputs": {
                                    "content": "@body('最終検出日時を取得2')",
                                    "schema": {
                                        "properties": {
                                            "odata.metadata": {
                                                "type": "string"
                                            },
                                            "value": {
                                                "items": {
                                                    "properties": {
                                                        "datetime": {
                                                            "type": "string"
                                                        },
                                                        "odata.etag": {
                                                            "type": "string"
                                                        }
                                                    },
                                                    "required": [
                                                        "odata.etag",
                                                        "datetime"
                                                    ],
                                                    "type": "object"
                                                },
                                                "type": "array"
                                            }
                                        },
                                        "type": "object"
                                    }
                                },
                                "runAfter": {
                                    "最終検出日時を取得2": [
                                        "Succeeded"
                                    ]
                                },
                                "type": "ParseJson"
                            },
                            "最新のモーション検出日時を保存": {
                                "inputs": {
                                    "body": {
                                        "datetime": "@{body('changetimezone')}"
                                    },
                                    "host": {
                                        "connection": {
                                            "name": "@parameters('$connections')['azuretables']['connectionId']"
                                        }
                                    },
                                    "method": "put",
                                    "path": "/Tables/@{encodeURIComponent('motion')}/entities(PartitionKey='@{encodeURIComponent('motion')}',RowKey='@{encodeURIComponent('lastmotion')}')"
                                },
                                "runAfter": {
                                    "JSON_の解析_2": [
                                        "Succeeded"
                                    ]
                                },
                                "type": "ApiConnection"
                            },
                            "最終検出日時を取得2": {
                                "inputs": {
                                    "host": {
                                        "connection": {
                                            "name": "@parameters('$connections')['azuretables']['connectionId']"
                                        }
                                    },
                                    "method": "get",
                                    "path": "/Tables/@{encodeURIComponent('motion')}/entities",
                                    "queries": {
                                        "$filter": "RowKey eq 'lastmotion'",
                                        "$select": "datetime"
                                    }
                                },
                                "runAfter": {},
                                "type": "ApiConnection"
                            }
                        },
                        "else": {
                            "actions": {
                                "For_each_2": {
                                    "actions": {
                                        "変数の設定": {
                                            "inputs": {
                                                "name": "経過時間",
                                                "value": "@div(div(sub(ticks(actionBody('changetimezone')),ticks(items('For_each_2')?['datetime'])),10000000),60)"
                                            },
                                            "runAfter": {},
                                            "type": "SetVariable"
                                        }
                                    },
                                    "foreach": "@body('JSON_の解析')?['value']",
                                    "runAfter": {
                                        "JSON_の解析": [
                                            "Succeeded"
                                        ]
                                    },
                                    "type": "Foreach"
                                },
                                "For_each_5": {
                                    "actions": {
                                        "HTTP": {
                                            "inputs": {
                                                "body": {
                                                    "messages": [
                                                        {
                                                            "text": "前回は@{items('For_each_5')?['datetime']}に動体検知しています。\n(@{variables('経過時間')}分経過)",
                                                            "type": "text"
                                                        }
                                                    ],
                                                    "to": "@{items('For_each')?['source']?['userId']}"
                                                },
                                                "headers": {
                                                    "Authorization": "Bearer ********=",
                                                    "Content-Type": "application/json"
                                                },
                                                "method": "POST",
                                                "uri": "https://api.line.me/v2/bot/message/push"
                                            },
                                            "runAfter": {},
                                            "type": "Http"
                                        }
                                    },
                                    "foreach": "@body('JSON_の解析')?['value']",
                                    "runAfter": {
                                        "For_each_2": [
                                            "Succeeded"
                                        ]
                                    },
                                    "type": "Foreach"
                                },
                                "JSON_の解析": {
                                    "inputs": {
                                        "content": "@body('最終検出日時を取得')",
                                        "schema": {
                                            "properties": {
                                                "odata.metadata": {
                                                    "type": "string"
                                                },
                                                "value": {
                                                    "items": {
                                                        "properties": {
                                                            "datetime": {
                                                                "type": "string"
                                                            },
                                                            "odata.etag": {
                                                                "type": "string"
                                                            }
                                                        },
                                                        "required": [
                                                            "odata.etag",
                                                            "datetime"
                                                        ],
                                                        "type": "object"
                                                    },
                                                    "type": "array"
                                                }
                                            },
                                            "type": "object"
                                        }
                                    },
                                    "runAfter": {
                                        "最終検出日時を取得": [
                                            "Succeeded"
                                        ]
                                    },
                                    "type": "ParseJson"
                                },
                                "最終検出日時を取得": {
                                    "inputs": {
                                        "host": {
                                            "connection": {
                                                "name": "@parameters('$connections')['azuretables']['connectionId']"
                                            }
                                        },
                                        "method": "get",
                                        "path": "/Tables/@{encodeURIComponent('motion')}/entities",
                                        "queries": {
                                            "$filter": "RowKey eq 'lastmotion'",
                                            "$select": "datetime"
                                        }
                                    },
                                    "runAfter": {},
                                    "type": "ApiConnection"
                                }
                            }
                        },
                        "expression": {
                            "and": [
                                {
                                    "equals": [
                                        "@items('For_each')?['type']",
                                        "motion"
                                    ]
                                }
                            ]
                        },
                        "runAfter": {},
                        "type": "If"
                    }
                },
                "foreach": "@triggerBody()?['events']",
                "runAfter": {
                    "変数を初期化する": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "changetimezone": {
                "inputs": {
                    "baseTime": "@body('現在の時刻')",
                    "destinationTimeZone": "Tokyo Standard Time",
                    "formatString": "u",
                    "sourceTimeZone": "UTC"
                },
                "kind": "ConvertTimeZone",
                "runAfter": {
                    "現在の時刻": [
                        "Succeeded"
                    ]
                },
                "type": "Expression"
            },
            "変数を初期化する": {
                "inputs": {
                    "variables": [
                        {
                            "name": "経過時間",
                            "type": "integer"
                        }
                    ]
                },
                "runAfter": {
                    "changetimezone": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "現在の時刻": {
                "inputs": {},
                "kind": "CurrentTime",
                "runAfter": {},
                "type": "Expression"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {
            "$connections": {
                "defaultValue": {},
                "type": "Object"
            }
        },
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {
                        "properties": {
                            "events": {
                                "items": {
                                    "properties": {
                                        "message": {
                                            "properties": {
                                                "id": {
                                                    "type": "string"
                                                },
                                                "text": {
                                                    "type": "string"
                                                },
                                                "type": {
                                                    "type": "string"
                                                }
                                            },
                                            "type": "object"
                                        },
                                        "replyToken": {
                                            "type": "string"
                                        },
                                        "source": {
                                            "properties": {
                                                "type": {
                                                    "type": "string"
                                                },
                                                "userId": {
                                                    "type": "string"
                                                }
                                            },
                                            "type": "object"
                                        },
                                        "timestamp": {
                                            "type": "integer"
                                        },
                                        "type": {
                                            "type": "string"
                                        }
                                    },
                                    "required": [
                                        "replyToken",
                                        "type",
                                        "timestamp",
                                        "source",
                                        "message"
                                    ],
                                    "type": "object"
                                },
                                "type": "array"
                            }
                        },
                        "type": "object"
                    }
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {
        "$connections": {
            "value": {
                "azuretables": {
                    "connectionId": "/subscriptions/*+*******/resourceGroups/motion-test-rg/providers/Microsoft.Web/connections/azuretables-1",
                    "connectionName": "azuretables-1",
                    "id": "/subscriptions/********/providers/Microsoft.Web/locations/japaneast/managedApis/azuretables"
                }
            }
        }
    }
}

[周期実行]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "changetimezone": {
                "inputs": {
                    "baseTime": "@body('現在の時刻')",
                    "destinationTimeZone": "Tokyo Standard Time",
                    "formatString": "u",
                    "sourceTimeZone": "UTC"
                },
                "kind": "ConvertTimeZone",
                "runAfter": {
                    "現在の時刻": [
                        "Succeeded"
                    ]
                },
                "type": "Expression"
            },
            "変数を初期化する": {
                "inputs": {
                    "variables": [
                        {
                            "name": "経過時間",
                            "type": "integer"
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "時のみ取得": {
                "inputs": {
                    "baseTime": "@body('現在の時刻')",
                    "destinationTimeZone": "Tokyo Standard Time",
                    "formatString": "HH",
                    "sourceTimeZone": "UTC"
                },
                "kind": "ConvertTimeZone",
                "runAfter": {
                    "changetimezone": [
                        "Succeeded"
                    ]
                },
                "type": "Expression"
            },
            "条件": {
                "actions": {
                    "For_each": {
                        "actions": {
                            "変数の設定": {
                                "inputs": {
                                    "name": "経過時間",
                                    "value": "@div(div(sub(ticks(actionBody('changetimezone')),ticks(items('For_each')?['datetime'])),10000000),60)"
                                },
                                "runAfter": {},
                                "type": "SetVariable"
                            },
                            "条件_2": {
                                "actions": {
                                    "HTTP": {
                                        "inputs": {
                                            "body": {
                                                "messages": [
                                                    {
                                                        "text": "前回動体検知してから @{variables('経過時間')}分経過しています。",
                                                        "type": "text"
                                                    }
                                                ]
                                            },
                                            "headers": {
                                                "Authorization": "Bearer ********=",
                                                "Content-Type": "application/json"
                                            },
                                            "method": "POST",
                                            "uri": "https://api.line.me/v2/bot/message/broadcast"
                                        },
                                        "runAfter": {},
                                        "type": "Http"
                                    }
                                },
                                "expression": {
                                    "and": [
                                        {
                                            "greater": [
                                                "@variables('経過時間')",
                                                120
                                            ]
                                        }
                                    ]
                                },
                                "runAfter": {
                                    "変数の設定": [
                                        "Succeeded"
                                    ]
                                },
                                "type": "If"
                            }
                        },
                        "foreach": "@body('JSON_の解析')?['value']",
                        "runAfter": {
                            "JSON_の解析": [
                                "Succeeded"
                            ]
                        },
                        "type": "Foreach"
                    },
                    "JSON_の解析": {
                        "inputs": {
                            "content": "@body('エンティティの取得')",
                            "schema": {
                                "properties": {
                                    "odata.metadata": {
                                        "type": "string"
                                    },
                                    "value": {
                                        "items": {
                                            "properties": {
                                                "datetime": {
                                                    "type": "string"
                                                },
                                                "odata.etag": {
                                                    "type": "string"
                                                }
                                            },
                                            "required": [
                                                "odata.etag",
                                                "datetime"
                                            ],
                                            "type": "object"
                                        },
                                        "type": "array"
                                    }
                                },
                                "type": "object"
                            }
                        },
                        "runAfter": {
                            "エンティティの取得": [
                                "Succeeded"
                            ]
                        },
                        "type": "ParseJson"
                    },
                    "エンティティの取得": {
                        "inputs": {
                            "host": {
                                "connection": {
                                    "name": "@parameters('$connections')['azuretables']['connectionId']"
                                }
                            },
                            "method": "get",
                            "path": "/Tables/@{encodeURIComponent('motion')}/entities",
                            "queries": {
                                "$filter": "RowKey eq 'lastmotion'",
                                "$select": "datetime"
                            }
                        },
                        "runAfter": {},
                        "type": "ApiConnection"
                    }
                },
                "expression": {
                    "and": [
                        {
                            "greater": [
                                "@int(body('時のみ取得'))",
                                5
                            ]
                        },
                        {
                            "lessOrEquals": [
                                "@int(body('時のみ取得'))",
                                20
                            ]
                        }
                    ]
                },
                "runAfter": {
                    "時のみ取得": [
                        "Succeeded"
                    ]
                },
                "type": "If"
            },
            "現在の時刻": {
                "inputs": {},
                "kind": "CurrentTime",
                "runAfter": {
                    "変数を初期化する": [
                        "Succeeded"
                    ]
                },
                "type": "Expression"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {
            "$connections": {
                "defaultValue": {},
                "type": "Object"
            }
        },
        "triggers": {
            "繰り返し": {
                "recurrence": {
                    "frequency": "Hour",
                    "interval": 1
                },
                "type": "Recurrence"
            }
        }
    },
    "parameters": {
        "$connections": {
            "value": {
                "azuretables": {
                    "connectionId": "/subscriptions********/resourceGroups/motion-test-rg/providers/Microsoft.Web/connections/azuretables-1",
                    "connectionName": "azuretables-1",
                    "id": "/subscriptions/********/providers/Microsoft.Web/locations/japaneast/managedApis/azuretables"
                }
            }
        }
    }
}
This post is licensed under CC BY 4.0 by the author.

Synology NASのSurveillance StationがVersion UPしてtp-linkのTapo C200に対応した

Azure Container Instanceを使ってMinecraft Bedrock Edition(統合版)サーバを立ち上げる