JSON Lookup table issue

I am trying to create a (Coefficient of Performance) lookup table in Home Assistant, and from what I figured out so far I need to crate JSON file that contains the lookup table data.

The JSON file contains:

{
  "temperature": {
    "-22": {"COP": 1.40},
    "-20": {"COP": 1.52},
    "-15": {"COP": 1.54},
    "-10": {"COP": 1.57},
    "-5": {"COP": 1.70},
    "0": {"COP": 1.75},
    "5": {"COP": 1.80},
    "10": {"COP": 2.04},
    "15": {"COP": 2.23},
    "17": {"COP": 2.43},
    "20": {"COP": 2.61},
    "25": {"COP": 2.81},
    "30": {"COP": 3.06},
    "35": {"COP": 3.20},
    "40": {"COP": 3.32},
    "45": {"COP": 3.37},
    "47": {"COP": 3.40},
    "50": {"COP": 3.79},
    "55": {"COP": 3.90},
    "60": {"COP": 3.91},
    "65": {"COP": 3.83},
    "70": {"COP": 3.76},
    "75": {"COP": 3.71},
    "80": {"COP": "N/A"},
    "85": {"COP": "N/A"},
    "90": {"COP": "N/A"},
    "95": {"COP": "N/A"},
    "100": {"COP": "N/A"},
    "105": {"COP": "N/A"},
    "110": {"COP": "N/A"},
    "115": {"COP": "N/A"},
    "120": {"COP": "N/A"},
    "125": {"COP": "N/A"},
    "129": {"COP": "N/A"}
  }
}

I also seem to need to create a sensor for the file with:

sensor:
  - platform: file
    name: "COP Data"
    file_path: "/config/cop_data.json"
    value_template: "{{ value_json }}"

And final sensor for the output:


sensor:
  - platform: template
    sensors:
      heating_cop:
        friendly_name: "Heating COP"
        value_template: >
          {% set temp = states('sensor.current_temperature') | int %}
          {% set data = states.sensor.cop_data.state | from_json %}
          {{ data.temperature[temp].COP }}
        unit_of_measurement: "W/W"

I can’t seem to get the file sensor to work and that leads the Heating COP sensor to show unavailable. Also tried using RESTful Sensor instead of file type with follwing but can’t get that to work either.


  - platform: rest
    name: COP Data
    resource: http://192.168.1.10:8123/local/cop_data.json
    value_template: "{{ value_json }}"

Can anyone help me out with what I am doing incorrectly with the syntax or config? What I ultimately want to do is compare the data from lookup table result to my calculated COP and check if its within range (+ or - 10%)

https://www.home-assistant.io/integrations/file/

Perhaps if you remove all the line breaks it will help.

Also states can only be 255 characters long. Your file contains 895 characters. Which means the file sensor can only return 255 characters maximum. Same for the rest sensor. Attributes have a larger limit but all you have to do is use the value template in the file sensor to do all the work to whittle down the value_json variable to less than 255 characters rather than using a separate template sensor.

Hi teknowiz,

What about this?

2 Likes

I saw the noted file integration limitation regarding reading last line only but thought it was only for GUI integration and not applicable to YAML config. A bit lost on how to make it work your proposed changes based on the lookup data table that I have to reference to compare.

That looks like a interesting approach, never heard of this integration before. Not sure how to make it work with the data since it seems to require source string (The entity to monitor/compensate) so not sure what I would use for that parameter.

Change your file to this (no line-breaks):

{"temperature":{"0":{"COP":1.75},"5":{"COP":1.8},"10":{"COP":2.04},"15":{"COP":2.23},"17":{"COP":2.43},"20":{"COP":2.61},"25":{"COP":2.81},"30":{"COP":3.06},"35":{"COP":3.2},"40":{"COP":3.32},"45":{"COP":3.37},"47":{"COP":3.4},"50":{"COP":3.79},"55":{"COP":3.9},"60":{"COP":3.91},"65":{"COP":3.83},"70":{"COP":3.76},"75":{"COP":3.71},"80":{"COP":"N/A"},"85":{"COP":"N/A"},"90":{"COP":"N/A"},"95":{"COP":"N/A"},"100":{"COP":"N/A"},"105":{"COP":"N/A"},"110":{"COP":"N/A"},"115":{"COP":"N/A"},"120":{"COP":"N/A"},"125":{"COP":"N/A"},"129":{"COP":"N/A"},"-22":{"COP":1.4},"-20":{"COP":1.52},"-15":{"COP":1.54},"-10":{"COP":1.57},"-5":{"COP":1.7}}}

It is still valid json.

Delete your template sensor and change your file sensor to this:

sensor:
  - platform: file
    name: "Heating COP"
    unit_of_measurement: "W/W"
    file_path: "/config/cop_data.json"
    value_template: >
          {% set temp = states('sensor.current_temperature') | int %}
          {% set data = value_json %}
          {{ data.temperature[temp].COP }}

However I’m not sure this will work as your temperature sensor would need to be quantised to only the levels in your lookup table. Is that true?

e.g. what happens if your temperature is -21 degrees?

There is no lookup for that.

If not quantized to those exact values, then use the compensation integration as suggested by SG. Use the temperature sensor as the source sensor.

compensation:
  heating_cop:
    source: sensor.current_temperature
    unit_of_measurement:  "W/W"
    data_points:
      - [-22, 1.40]
      - [-20, 1.52]
      - [-15, 1.54]
      - [ etc...

This will interpolate values for all values in-between your set of defined points so the temperature does not have to match a specific value in the list of points you provide.

2 Likes

Thank you very much both of you, I think I got it working Compensation sensor, took me bit to figure out the senor names for the entities being created since it based it on input sensor naming. Also had to convert the data points from F to C since my input senor was in C but it all seems to be working so far.

image

1 Like

Looking Awesome!
The Compensation Author would be proud. I’d tag him but, well you know, FAQ rules and all that…