Rest sensor with numeric keys

Hi everyone,
I think I am fairly close to getting my first “custom” sensor to work, but so far it will not provide any meaningful output. For the moment I am just querying the fan status of a Systemair HVAC unit, where the output can be 4 different single digit numbers. And I know the polling works - in the browser - except not inside HA. Does anybody see any obvious mistakes?

sensor:
  - platform: rest
    resource: http://192.168.44.5/mread
    name: Ventilation mode
    unique_id: vent_mode
    scan_interval: 60
    params:
      "1130": >
        "1"
    value_template: "{{ value_json.1130 }}"

Testing the code inside Firefox works as expected, where key=1 marks which values are wanted, and sent in the json text as query, and then a response is given, where “2” is the correct at the moment. So it is all about getting the same set up correctly in HA.

I have prepared this dict to map the index to a readable text, but that’s for the next step. First I must get the number output right.
{% set descriptions = {"0": "Off","2": "Low","3": "Normal","4": "High"} %}

The issue is using keys that start with numbers. This should be avoided whenever possible, but in your case it doesn’t appear there is anything you can do about it.

You should be able to work around the problem using bracket notation:

  value_template: "{{ value.json['1130'] }}"

It’s safer, in fact, to always use bracket notation.

The reason for the problem is that there are a number of things that .1130 can mean. The system tries things in order:

  • functions (like .values)
  • indexing (like .1 for the second item)
  • attribute names (like .temperature)

In this case, it’s not found a dictionary function (method) called 1130(), so it’s looking for the 1131st item in the dictionary and failing because a) dictionaries don’t support numeric indexing and b) there aren’t that many elements.

Bracket notation solves all of these by removing the function test (still use dot notation for functions, and dict['values'] works if you have a key that is also a function name) and allowing you to distinguish between numeric names and numbers (dict['1'] vs list[1]).

2 Likes

Thanks. I have changed to bracket notation, and at the moment it seems to run without errors. But there is no value outputted to HA either. Is it a way to troubleshoot the exact communication that is going on here?

bilde

Troubleshoot with:

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

to see what is being returned. The 250 is to stop a long response breaking the 255-char state length limit.

You could perhaps use resource_template rather than params to build the URL?

Thanks. Getting closer now. But I also hardcoded the entire json-string into the resource (meaning /mread?{“1130”:1}=). So it seems to be something with the params. Can you clarify that if by using the params, is resulting in a complete json-string added to the resource? Meaning, questionmark, brackets and so on? Or should the questionmark also be in the resource? I couldn’t find a proper explanation to this in the docs, which is also difficult to troubleshoot.

bilde

params should build a query string but no brackets. I’d expect your original example to give:

http://192.168.44.5/mread?1130=1

What is the exact URL you want?

As you’re now getting the JSON response in your sensor, you can go back to

value_template: "{{ value_json['1130'] }}"
1 Like

The receiving end is picky, and must have brackets, just tested with and without. And the key(s) must also be in quotes. But the value_json[‘1130’] works at least now :slight_smile: Also, I have to restart all HA, and not just reload the configuration.yaml to change between the value outputs.
Perhaps I should build a resource_template with whatever string I need? I guess this is the only way to have quotes on the numeric keys also.