『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 5521|回复: 4

[基础教程] REST 請教

[复制链接]

21

主题

164

帖子

1033

积分

金牌会员

Rank: 6Rank: 6

积分
1033
金钱
869
HASS币
0
发表于 2020-6-1 00:38:46 | 显示全部楼层 |阅读模式
如果有json 回傳, 希望用 rest + template 讀取 Sensor, yaml 應怎寫?
YAML

sensor:
  - platform: rest
    name: 'airsensors'
    scan_interval: '300'
    resource: 'http://10.0.0.1/sensors'
    value_template: 'OK'
    json_attributes:
      - result
  - platform: template
    sensors:
       co2:
         value_template: '{{ states.sensor.airsensors.attributes["result"]["datas"]["co2"] }}'
         unit_of_measurement: 'ppm'


Json

{
   "rc":{
      "c":0,
      "p":"操作成功"
   },
   "result":[
      {
         "battery":"92",
         "charge":1,
         "cityId":0000,
         "datas":{
            "co2":1042.0,
            "co2Desc":"浓度较高",
            "co2Level":1,
            "detectTime":1590935809000,
            "humidity":50.0,
            "humidityDesc":"舒适",
            "humidityLevel":0,
            "pm25":0.0,
            "pm25Desc":"优",
            "pm25Level":0,
            "temp":33.0,
            "tempDesc":"偏热",
            "tempLevel":1
         },
         "hardwareType":3,
         "prompts":[
            {
               "desc":"不适宜给婴儿洗澡",
               "tips":"室内二氧化碳浓度较高,不适宜婴儿长待,建议开启新风换气。"
            },
            {
               "desc":"不适宜学习",
               "tips":"室内二氧化碳浓度较高,令人昏昏欲睡,建议开启新风换气。"
            },
            {
               "desc":"适宜室外运动",
               "tips":"室外气象条件很好,适宜运动。"
            }
         ],
         "visible":1
      }
   ]
}


回复

使用道具 举报

6

主题

743

帖子

5958

积分

论坛元老

Rank: 8Rank: 8

积分
5958
金钱
5210
HASS币
20
发表于 2020-6-1 08:37:21 | 显示全部楼层
{{ states.sensor.airsensors.attributes.result[0].datas.co2  }}
回复

使用道具 举报

30

主题

493

帖子

4795

积分

论坛元老

佑桑

Rank: 8Rank: 8

积分
4795
金钱
4287
HASS币
156

教程狂人

发表于 2020-6-1 11:28:56 | 显示全部楼层
試試這個 https://jsonpath.com/
你帖上的 json 無法解析
ASRock H370 PRO4, 48GB, 8TBx3 + 2TBx3
ESXi 6.7 + Ununtu + Docker + HASS.IO -> Homeassistant OS
(Home Assistant Supervised 官方文件)
回复

使用道具 举报

3

主题

119

帖子

1377

积分

金牌会员

Rank: 6Rank: 6

积分
1377
金钱
1258
HASS币
10
发表于 2020-6-1 22:48:16 | 显示全部楼层
我的 拿去参考

  - platform: rest
    name: mirouter_network
    json_attributes:
      - downspeed
      - upspeed
      - download
      - upload
    resource: http://192.168.31.179:5010/mirouter/network

  - platform: rest
    name: mirouter_hdd
    json_attributes:
      - capacity
      - free
      - used
    resource: http://192.168.31.179:5010/mirouter/hdd

  - platform: rest
    name: bme280_sgp30
    json_attributes:
      - temperature
      - humidity
      - pressure
      - TVOC
      - eCO2
    resource: http://192.168.2.141/bme280_sgp30


  - platform: template
    sensors:
      downspeed:
        friendly_name: 'download speed'
        value_template: '{{ states.sensor.mirouter_network.attributes["downspeed"]  | float / 1024.0 | round(2) }}'
        unit_of_measurement: 'KB/s'
      upspeed:
        friendly_name: 'upload speed'
        value_template: '{{ states.sensor.mirouter_network.attributes["upspeed"]  | float / 1024.0 | round(2) }}'
        unit_of_measurement: 'KB/s'
      download:
        friendly_name: 'data downloaded'
        value_template: '{{ states.sensor.mirouter_network.attributes["download"]  | float / 1024.0 / 1024 / 1024 | round(2) }}'
        unit_of_measurement: 'GB'
      upload:
        friendly_name: 'data uploaded'
        value_template: '{{ states.sensor.mirouter_network.attributes["upload"]  | float / 1024.0 / 1024 / 1024 | round(2) }}'
        unit_of_measurement: 'GB'
      hdd_capacity:
        friendly_name: 'HDD capacity'
        value_template: '{{ states.sensor.mirouter_hdd.attributes["capacity"]  | float / 1024.0 / 1024 / 1024 | round(2) }}'
        unit_of_measurement: 'GB'
      hdd_free:
        friendly_name: 'HDD free'
        value_template: '{{ states.sensor.mirouter_hdd.attributes["free"]  | float / 1024.0 / 1024 / 1024 | round(2) }}'
        unit_of_measurement: 'GB'
      hdd_used:
        friendly_name: 'HDD used'
        value_template: '{{ states.sensor.mirouter_hdd.attributes["used"]  | float / 1024.0 / 1024 / 1024 | round(2) }}'
        unit_of_measurement: 'GB'

      temperature:
        friendly_name: 'temperature'
        value_template: '{{ "%f"|format(states.sensor.bme280_sgp30.attributes["temperature"]) }}'
        device_class: temperature
        unit_of_measurement: '°C'
      humidity:
        friendly_name: 'humidity'
        value_template: '{{ "%f"|format(states.sensor.bme280_sgp30.attributes["humidity"]) }}'
        device_class: humidity
        unit_of_measurement: '%'
      pressure:
        friendly_name: 'pressure'
        value_template: '{{ "%f"|format(states.sensor.bme280_sgp30.attributes["pressure"]) }}'
        device_class: pressure
        unit_of_measurement: 'mbar'
      tvoc:
        friendly_name: 'TVOC'
        value_template: '{{ "%d"|format(states.sensor.bme280_sgp30.attributes["TVOC"]) }}'
        unit_of_measurement: 'ppb'
      eco2:
        friendly_name: 'eCO2'
        value_template: '{{ "%d"|format(states.sensor.bme280_sgp30.attributes["eCO2"]) }}'
        unit_of_measurement: 'ppm'




curl http://192.168.2.141/bme280_sgp30
{
  "temperature": 33.21,
  "humidity": 46.78418,
  "pressure": 1005.515,
  "TVOC": 0,
  "eCO2": 400
}

http://192.168.31.179:5010/mirouter/hdd
{"capacity": 499398426624, "free": 60507889664, "used": 438890536960}


http://192.168.31.179:5010/mirouter/network
{"downspeed": 22151, "upload": 56459139061, "upspeed": 55409, "download": 70553008108}

评分

参与人数 1金钱 +20 HASS币 +10 收起 理由
+ 20 + 10 赠人玫瑰,手留余香!

查看全部评分

回复

使用道具 举报

21

主题

164

帖子

1033

积分

金牌会员

Rank: 6Rank: 6

积分
1033
金钱
869
HASS币
0
 楼主| 发表于 2020-6-2 00:42:55 | 显示全部楼层
mitu 发表于 2020-6-1 08:37
{{ states.sensor.airsensors.attributes.result[0].datas.co2  }}

這個可以解決, 謝謝您
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-26 03:48 , Processed in 0.068310 second(s), 28 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表