Configuration REST platform / sensor error / MELCloud

Hello community :slight_smile:

I am currently facing a problem where I have not found a solution in the cases from our forum.

I always get the following error:
image

My REST sensor looks like this.
Can you identify an error here and possibly help me?

sensor:
  - platform: rest
    name: heat_pump_api
    resource_template: https://app.melcloud.com/Mitsubishi.Wifi.Client/user/ListDevices?id=XXXXXXXX&buildingID=XXXXX
    method: GET
    headers: 
        X-MitsContextKey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
    value_template: "OK"
    scan_interval: 60
    json_attributes_path: "$..Devices[?(@.DeviceID==XXXXXXXXX)].Device"
    json_attributes:
      - CurrentEnergyConsumed
      #- DailyHeatingEnergyConsumed
      #- DailyHotWaterEnergyConsumed
      #- DailyEnergyConsumedDaily
      #- HeatPumpFrequency
      #- DefrostMode
  - platform: template
    sensors:
      heat_pump_power:
        friendly_name: "Heat Pump Power"
        # availability_template: "{{ is_state('sensor.heat_pump_api', 'OK') }}"
        availability_template: >-
          {{ (states("sensor.heat_pump_api") not in ["unknown", "unavailable"]) and (state_attr('sensor.heat_pump_api', 'CurrentEnergyConsumed') != None) }}
        value_template: "{{ state_attr('sensor.heat_pump_api', 'CurrentEnergyConsumed') }}"
        device_class: power
        unit_of_measurement: "kW"
        unique_id: 010320231

Many thanks in advance :smiley:

Not without seeing the (correctly formatted) returned data.

Hello @tom_I,

is that what you mean?

{ Success: false, ErrorMessage: ""}
2023-05-26 08:41:25.807 WARNING (MainThread) [homeassistant.components.rest.sensor] REST result could not be parsed as JSON
2023-05-26 08:41:25.894 DEBUG (MainThread) [homeassistant.components.rest.sensor] Erroneous JSON:

If not, what returned data do you mean?

Thanks

The data from your rest resource.

Hello @tom_I,
I’m stuck here, where exactly can I find the data you need (rest resource)?

I don’t have any data about the REST sensor other than my config and the error message.

Maybe I am just misunderstanding something here.

Regards

Put the URL into a browser and paste the result here with any confidential stuff redacted.

Don’t change the data structure when you do that.

Without that, we’re working blind. It’s like me asking you to explain page 348 of my book.

So if I log into the MELcloud and call up the browser code via the F12 key, I can read out the necessary data.

However, if I insert the URL into the browser like this, I get the following message.

{ Success: false, ErrorMessage: ""}

Which is very strange, because the required values (for example: CurrentEnergyConsumed) are read out and displayed in the HA.

How? And if so, what’s your rest sensor for?

Then the text becomes a bit longer when I try to explain it:)

MELCloud is Mitsubishi Electric’s cloud service for controlling air conditioners and heat pumps.

I have a heat pump on it.
The MELCloud is also available as an integration in HA.
The problem is that MELCloud receives more data than is displayed to the “normal” user.

For example, only the flow temperature and return temperature are displayed.

Now comes the point where the REST sensors are used.

When I log in to the homepage via Firefox or Chrome, I can call up the HTML code of the page via F12.
There you can access information such as: Defrost time, Compressor frequency, Compressor working time.

For example, I ask for the following data:

  - CurrentEnergyConsumed
  #- DailyHeatingEnergyConsumed
  #- DailyHotWaterEnergyConsumed
  #- DailyEnergyConsumedDaily
  #- HeatPumpFrequency
  #- DefrostMode

I have commented out the # for testing purposes, so only the “Current Energy Consumption” is queried.

I have explained this in this thread:

Further up in this thread I posted my config.
The X-MitsContextKey: makes access and identification possible for HA to evaluate the MELCloud data, actually more of an authentication code.

In my config above, everything is with big XXXXXX user related data, ex.
Device ID.

I hope that explains it a bit :sweat_smile:

Well, one way or another you need to see what the rest sensor is getting back. If it’s that success: false error message, then the problem is with your authentication.

Try commenting out the attribute line and use:

value_template: "{{ value[:240] }}"

The sensor state will then be the first 240 characters of the response, which might help work out what’s going on.

Hey,
if I replace “value_template” with your code, HA doesn’t find the sensor anymore and it is “unavailable”.
So I don’t get any useful data out here either :confused:

Why are RESt sensor errors so difficult to detect -.-

Paste the YAML config for the sensor after you’ve made those changes. Also check your full logs for any error messages.

The log does not say anything different than before.
I think it has something to do with Mitsubishi Electric and not with HA or do you think otherwise?

sensor:
  - platform: rest
    name: heat_pump_api
    resource_template: https://app.melcloud.com/Mitsubishi.Wifi.Client/user/ListDevices?id=XXXXXXXX&buildingID=XXXXX
    method: GET
    headers: 
        X-MitsContextKey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
    value_template: "OK"
    scan_interval: 60
    json_attributes_path: "$..Devices[?(@.DeviceID==XXXXXXX)].Device"
    json_attributes:
      - CurrentEnergyConsumed
      #- DailyHeatingEnergyConsumed
      #- DailyHotWaterEnergyConsumed
      #- DailyEnergyConsumedDaily
      #- HeatPumpFrequency
      #- DefrostMode
  - platform: template
    sensors:
      heat_pump_power:
        friendly_name: "Heat Pump Power"
        # availability_template: "{{ is_state('sensor.heat_pump_api', 'OK') }}"
        availability_template: >-
          {{ (states("sensor.heat_pump_api") not in ["unknown", "unavailable"]) and (state_attr('sensor.heat_pump_api', 'CurrentEnergyConsumed') != None) }}
        #value_template: "{{ state_attr('sensor.heat_pump_api', 'CurrentEnergyConsumed') }}"
        value_template: "{{ value[:240] }}"
        device_class: power
        unit_of_measurement: "kW"
        unique_id: 010320231
{ Success: false, ErrorMessage: ""}
2023-05-29 19:18:10.644 WARNING (MainThread) [homeassistant.components.rest.sensor] REST result could not be parsed as JSON
2023-05-29 19:18:10.647 DEBUG (MainThread) [homeassistant.components.rest.sensor] Erroneous JSON:
{ Success: false, ErrorMessage: ""}

Maybe this will help, from the Add-On Log Viewer.

2023-05-29 21:36:43.436 DEBUG (MainThread) [homeassistant.components.rest.data] Updating from https://app.melcloud.com/Mitsubishi.Wifi.Client/user/ListDevices?id=XXXXXXX&buildingID=XXXXX
2023-05-29 21:36:43.601 INFO (MainThread) [httpx] HTTP Request: GET https://app.melcloud.com/Mitsubishi.Wifi.Client/user/ListDevices?id=XXXXXXX&buildingID=XXXXXX "HTTP/1.1 401 Unauthorized"
2023-05-29 21:36:43.604 DEBUG (MainThread) [homeassistant.components.rest.sensor] Data fetched from resource: 
{ Success: false, ErrorMessage: ""}
2023-05-29 21:36:43.604 WARNING (MainThread) [homeassistant.components.rest.sensor] REST result could not be parsed as JSON
2023-05-29 21:36:43.608 DEBUG (MainThread) [homeassistant.components.rest.sensor] Erroneous JSON: 
{ Success: false, ErrorMessage: ""}

If I interpret this correctly, he can’t retrieve the data because he doesn’t have authorization? (HTTP/1.1 401 Unauthorized)?

Correct. You need to resolve that — I can’t help as I don’t have that system.