Setting up sensor using rest and json

I am trying to extract values for sensors from this site:

I have so far been playing with this:


  - platform: rest
    name: MSI
    resource: https://api.minerstat.com/v2/stats/SENSURED/hauglandmsi
    scan_interval: 30
    timeout: 60
    force_update: true
    verify_ssl: false
    value_template: 'OK'
    json_attributes:
      - hashrate
      - uptime
      - accepted_share
      - rejected_share
      - temp
      - power
      - usd_day
      - usd_week
      - usd_month
  - platform: template
    sensors:
      msi_hashrate:
        value_template: '{{  state_attr(''sensor.msi'', ''hashrate'') }}'
        unit_of_measurement: 'MH/s'
      msi_accepted_shares:
        value_template: '{{  state_attr(''sensor.msi'', ''accepted_share'') }}'
        unit_of_measurement: 'shares'
      msi_rejected_shares:
        value_template: '{{ state_attr(''sensor.msi'', ''rejected_share'') }}'
        unit_of_measurement: 'shares'
      msi_power:
        value_template: '{{ state_attr(''sensor.msi'',''power'')[0][''power''] }}'
        unit_of_measurement: 'W'
      msi_temperature:
        value_template: '{{ state_attr(''sensor.msi'', ''temp'')[0][''temperature'']|round(1) }}'
      msi_uptime:
        value_template: >-
          {%- set uptime  = state_attr('sensor.msi', 'uptime') | round -%}
          {%- set sep     = ':' -%}
          {%- set space   = ' - ' -%}
          {%- set TIME_MAP = {
              'week': (uptime / 604800) % 604800,
               'day': (uptime / 86400) % 7,
              'hour': (uptime / 3600) % 24,
            'minute': (uptime / 60) % 60,
            'second': (uptime % 60)
          }
          -%}

          {%- for unit, duration in TIME_MAP.items() if duration >= 1 -%}
            {%- if unit == 'week' -%}
              {{ (duration | string).split('.')[0] }} {{ unit }}
              {%- if duration >= 2 -%}
                s
              {%- endif -%}
              {{ space }}
            {%- endif -%}

            {%- if unit == 'day' -%}
              {{ (duration | string).split('.')[0] }} {{ unit }}
              {%- if duration >= 2 -%}
                s
              {%- endif -%}
              {{ space }}
            {%- endif -%}

            {%- if unit == 'hour' -%}
              {%- if duration <= 10 -%}
                {{ '0' }}{{ (duration | string).split('.')[0] }}{{ sep }}
              {%- endif -%}
              {%- if duration >= 10 -%}
                {{ (duration | string).split('.')[0] }}{{ sep }}
              {%- endif -%}
            {%- endif -%}

            {%- if unit == 'minute' -%}
              {%- if duration <= 10 -%}
                {{ '0' }}{{ (duration | string).split('.')[0] }}{{ sep }}
              {%- endif -%}
              {%- if duration >= 10 -%}
                {{ (duration | string).split('.')[0] }}{{ sep }}
              {%- endif -%}
            {%- endif -%}

            {%- if unit == 'second' -%}
              {%- if duration <= 10 -%}
                {{ '0' }}{{ (duration | string).split('.')[0] }}
              {%- endif -%}
              {%- if duration >= 10 -%}
                {{ (duration | string).split('.')[0] }}
              {%- endif -%}
            {%- endif -%}

Which only results in either unknown or unavailable status from my created sensors.

Can anyone point me in the right direction?

It’s really hard to see the JSON response’s structure. I would recommend installing a browser plug-in, for example “JSONView” in Chrome.

However, from your screenshot it appears that there is only a single top-level element “HAUGLANDMSI”, and the json_attributes list only supports top-level elements, so that’s why your list doesn’t work.

From the information provided, I’d start with the following configuration (possibly that list entry may need to be lower-case), and then use template sensors to extract further information.

    json_attributes:
      - HAUGLANDMSI
1 Like

Also, text as image is a big no-no. Impossible to test your issue :wink:

Trying to reach the API as outlined give me:

{"error":"Invalid Access Key"}

so unless you filtered the access key from your rest sensor, it won’t work.

2 Likes

I think there is normally an access key in the URL where the configuration says “SENSURED”.

1 Like

As the others have said, paste the JSON as text please.

I think you need to make use of the rest integration:

and work out the path to each of the elements you want. Something like this, with the first couple of sensors supplied for you:

rest:
  - resource: https://api.minerstat.com/v2/stats/SENSURED/hauglandmsi
    scan_interval: 30
    sensor:
      - name: "Uptime"
        value_template: "{{ value_json['HAUGLANDMSI']['info']['uptime'] }}"

      - name: "Temp"
        value_template: "{{ value_json['HAUGLANDMSI']['hardware'][0]['temp'] }}"
1 Like

I am sorry, here the json is as text. I am away from pc and stuck on iOS so everything takes way longer than necessary, but thanks for all your replies and patience so far.


{"HAUGLANDMSI":{"info":{"type":"nvidia","system":"win","status":"online","inactive":0,"status_reason":"","status_cpu":"idle","uptime":"3d 1h 49min","sync":5,"time":"2021-06-23 14:41:50","note":0,"profit_switch":0,"name":"hauglandmsi","version":"6.0.0.0","groups":"nvidia,windows,Haugland","cmd":"","electricity":0,"hot":75,"veryHot":89,"devices":1,"consumption":0,"os":{"status":"online","sync":5,"uptime":"8d 21h 39min","cpu_temp":0,"cpu_load":0,"freespace":885.119,"freemem":0,"localip":"192.168.1.68"}},"hardware":[{"name":"GeForce RTX 2070 with Max-Q Design","temp":73,"accepted":1020,"power":79,"speed":33.702205,"bus":"01:00.0","core":945,"coreMax":2100,"memory":6300,"memoryMax":5501,"load":100}],"revenue":{"usd_day":2.716,"usd_day_dual":0,"usd_day_cpu":0,"usd_week":19.011,"usd_month":81.476,"usd_month_dual":0,"usd_month_cpu":0,"btc_day":8.0e-5,"btc_week":0.000557,"btc_month":0.002386,"coin":0.00135157,"coin_dual":0,"coin_cpu":0,"cprice":2009.40873,"cprice_dual":0,"cprice_cpu":0},"mining":{"client":"TREX","client_version":"0.20.4","client_cpu":"","client_cpu_version":"","crypto":"BINANCE (eth)","crypto_dual":"","crypto_cpu":"","pool":"ethash.poolbinance.com:8888","pool_dual":"","pool_cpu":"","hashrate":{"hashrate":33.702205,"hashrate_unit":"MH","hashrate_dual":0,"hashrate_unit_dual":"","hashrate_cpu":0,"hashrate_unit_cpu":""},"shares":{"accepted_share":1020,"accepted_share_dual":0,"rejected_share":0,"rejected_share_dual":0,"accepted_share_cpu":0,"rejected_share_cpu":0}}}}

This is correct. I can’t add the accesskey unfortunately.

Great. If you paste it in here:

it’ll show you the structure and let you continue with the sensor setup I started above.

I clicked on the temp line and got this:

image

which confirmed that my template above is correct, and you can do that to add the other sensors.

Whilst “SENSURED” is a word, I think you mean “CENSORED” :wink:

1 Like

Ok, this works, thank you so much. Now I have to use this to find out the most important sensors: status, hashrate, usd_day, week and month, accepted and rejected shares :grin:

Haha, it’s a hasty combination of Norwegian and English :joy:

1 Like

Should be straightforward, but do shout if you get stuck.

Didn’t take long.

Tried to add hashrate like this:

rest:
  - resource: https://api.minerstat.com/v2/stats/CENSORED/hauglandmsi
    scan_interval: 30
    sensor:
      - name: "Uptime"
        value_template: "{{ value_json['HAUGLANDMSI']['info']['uptime'] }}"

      - name: "Temp"
        value_template: "{{ value_json['HAUGLANDMSI']['hardware'][0]['temp'] }}"

      - name: "Hashrate"
        value_template: "{{ value_json['HAUGLANDMSI']['mining'][hashrate]['hashrate'] }}"

Based on the Jain Viewer path:

But it outputs “unknown”

You forgot the quotes.

For info, rather than “[0]”, you could have used, e.g.

      - name: "Temp"
        value_template: "{{ value_json['HAUGLANDMSI']['hardware'] | selectattr('bus','eq','01:00.0') | map(attribute='temp') | list | first }}"

Not an issue here, unless you add some hardware that would change the list order.

1 Like

Ah, thank you.

Why didn’t you need quote for [0] but I needed for [hashrate]?

[0] is an index: the first item in a list. ['hashrate'] is a named key in a dictionary.

You’d use ['0'] if you had a key that was called “0”. So:

[1,2,3][0] == 1 (first item)
{'4': 'four', '0': 'zero'}['0'] == 'zero' (item named "0")

You would only ever use an unquoted key if it were a variable:

{% set x = 'hashrate' %}
{{ value_json['HAUGLANDMSI']['mining'][x] }}
1 Like

[0] means it is first item in the value_json['HAUGLANDMSI']['hardware'] list. It is an integer.
['hashrate'] means "get me the value whose key is ‘hashrate’ from the value_json['HAUGLANDMSI']['mining'] key-value dictionnary

1 Like

i also have issue making this work, i tried with chatpgt and got this solution

sensor:
  - platform: rest
    unique_id: garbage_collection
    resource: https://api-universal.appbolaget.se/waste/addresses/27d67c79-ab09-443f-a756-5022bc0d707e
    headers:
      module: universal
      unit: dd905ce7-b16d-4422-be36-564169af4035
    value_template: "{{ value_json.data }}"
    json_attributes:
      - services
      - updated_at
    name: "AvfallshÀmtning Kristianstad renhÄllning"

  - platform: template
    sensors:
      garbage_collection_karl_1:
        value_template: '{{ state_attr("sensor.garbage_collection", "services")[0]["collection_at"] }}'
      garbage_collection_karl_2:
        value_template: '{{ state_attr("sensor.garbage_collection", "services")[1]["collection_at"] }}'
      garbage_collection_tradgardsavfall:
        value_template: '{{ state_attr("sensor.garbage_collection", "services")[2]["collection_at"] }}'
      garbage_collection_last_updated:
        value_template: '{{ state_attr("sensor.garbage_collection", "updated_at") }}'

automation:
  - alias: Update garbage_collection sensor at midnight
    trigger:
      platform: time
      at: '00:00:00'
    action:
      service: homeassistant.update_entity
      entity_id: sensor.garbage_collection

the response from the call looks like this done in postman or curl using

{
    "message": "Successfully fetched item",
    "status": 200,
    "data": {
        "uuid": "27d67c79-ab09-443f-a756-5022bc0d707e",
        "alias": "krab-oestra-vramsvaegen-42-tollarp",
        "address": "Östra VramsvĂ€gen 42",
        "city": "Tollarp",
        "property_id": "228169",
        "services": [
            {
                "description": "Fyrfack 370 liter, KĂ€rl 1 varannan vecka",
                "type": "KĂ€rl 1",
                "collection_at": "2024-10-08",
                "precision": "day"
            },
            {
                "description": "Fyrfack 370 liter, KĂ€rl 2 var 4:e vecka",
                "type": "KĂ€rl 2",
                "collection_at": "2024-10-15",
                "precision": "day"
            },
            {
                "description": "TrÀdgÄrdsavfallsabonnemang",
                "type": "TrÀdgÄrdsavfall",
                "collection_at": "2024-09-25",
                "precision": "day"
            }
        ],
        "deviations": null,
        "updated_at": "2024-09-25 00:27:52"
    }
}

but i got unavailable in the entities section the the sensors or see any sensor called garbage_collection

ChatGPT is terrible at HA usually, although this looks pretty good. It’s using legacy format template configuration though.

The sensor’s ID is derived from the name not the unique_id. Look for sensor.avfallshamtning_kristianstad_renhallning.

i made it work, just needed to do a a full restart of home assistant