本帖最后由 chinyaolin 于 2020-11-27 00:02 编辑
前言
在 這篇 文中回應了壇友的問題, 似乎大家對於今日詩詞有點興趣, 就特別拿出來分享一下
參考連結
一、获取 Token
最建議的方法是:在你執行 HA 的主機上執行以下指令, data 裡就是需要的 token
(因為未來你將會在同一台主機中使用這個 token, 保證了取得/調用都是同一台主機)
$ curl https://v2.jinrishici.com/token
{"status":"success","data":"aaabbbcccdddeeefffggg"}
$
"data":"aaabbbcccdddeeefffggg"
當然,你也可以直接打開 https://v2.jinrishici.com/token 這個網址
二、接口测试
使用剛剛取得的 token, 執行以下指令, 看看能不能正常取得資訊
$ curl -X GET -H 'X-User-Token: aaabbbcccdddeeefffggg' -i 'https://v2.jinrishici.com/sentence'
...省略 header 並將輸出的 json 整理一下...
{
"status": "success",
"data": {
"id": "5b8b9572e116fb3714e71056",
"content": "深秋帘幕千家雨,落日楼台一笛风。",
"popularity": 7140,
"origin": {
"title": "题宣州开元寺水阁阁下宛溪夹溪居人",
"dynasty": "唐代",
"author": "杜牧",
"content": [......省略......],
"translate": null
},
"matchTags": [......省略......],
"recommendedReason": "",
"cacheAt": "2020-11-24T16:31:58.065213"
},
"token": "aaabbbcccdddeeefffggg",
"ipAddress": "xxx.xxx.xxx.xxx",
"warning": null
}
$
'X-User-Token: aaabbbcccdddeeefffggg'
"token": "aaabbbcccdddeeefffggg"
三、在 HA 建立 sensor
# 由「今日詩詞 一言API」取得每日一句, 每59分鐘更新一次 URL: https://www.jinrishici.com/
- platform: rest
name: short_part_of_poem
method: GET
resource: https://v2.jinrishici.com/sentence
headers:
Content-Type: application/json
X-User-Token: aaabbbcccdddeeefffggg
value_template: '{{value_json["data"]["id"]}}'
json_attributes_path: $.data
json_attributes:
- id
- content
- origin
- warning
scan_interval: 3540
X-User-Token: aaabbbcccdddeeefffggg
四、安裝 HTML-Jinja2 卡片
網址在這裡 https://github.com/PiotrMachowsk ... inja2-Template-card
或者是透過 HACS 來安裝
五、看看效果
卡片的寫法
content: >-
<center><h2> {{ state_attr("sensor.short_part_of_poem", "content") }} </h2>
</center> <p align="right">
{{ state_attr("sensor.short_part_of_poem", "origin")["dynasty"] }}
{{ state_attr("sensor.short_part_of_poem", "origin")["author"] }} -
{{ state_attr("sensor.short_part_of_poem", "origin")["title"] }} </p>
ignore_line_breaks: true
type: 'custom:html-template-card'
|