Template Error

I use a Template to get the current version of Home Assistant using this:

sensor:
  - platform: rest
    resource: https://s3.amazonaws.com/hassio-version/stable.json
    name: Hass Current Stable Version
    scan_interval: 7200
    value_template: '{{ value_json.homeassistant.qemux86 }}'

If you open the resource in a browser, it gives this JSON:

{
  "channel": "stable",
  "supervisor": "138",
  "homeassistant": {
    "default": "0.81.2",
    "qemux86": "0.81.2",
    "qemux86-64": "0.81.2",
    "qemuarm": "0.81.2",
    "qemuarm-64": "0.81.2",
    "intel-nuc": "0.81.2",
    "raspberrypi": "0.81.2",
    "raspberrypi2": "0.81.2",
    "raspberrypi3": "0.81.2",
    "raspberrypi3-64": "0.81.2",
    "tinker": "0.81.2",
    "odroid-c2": "0.81.2",
    "odroid-xu": "0.81.2"
  },
  "hassos": {
    "ova": "1.12",
    "rpi": "1.12",
    "rpi0-w": "1.12",
    "rpi2": "1.12",
    "rpi3": "1.12",
    "rpi3-64": "1.12",
    "tinker": "2.2",
    "odroid-c2": "2.2"
  },
  "hassos-cli": "7"
}

What I would really like to do is report on the value for qemux86-64 but it returns no value for that. It doesn’t seem to like the - in the string. What is the correct syntax for that?

Does this work:

value_template: "{{ 'value_json.homeassistant.qemux86-64' }}"

no that returns this:
value_template: “value_json.homeassistant.qemux86-64”

What about:

value_template: "{{  \'value_json.homeassistant.qemux86-64\' }}"

Or using a single quote to escape the single quote (two single quotes inside double quotes):

value_template: "{{  ''value_json.homeassistant.qemux86-64'' }}"

Error rendering template: TemplateSyntaxError: unexpected char ‘\’ at 714

Error rendering template: TemplateSyntaxError: expected token ‘end of print statement’, got ‘value_json’

edit: didn’t quote correctly…

Hi there,

Not sure if you came right with your query…
After doing some reading, I found that this works for my Pi installation:

value_template: “{{ value_json[‘homeassistant’][‘raspberrypi3-64’] }}”

So, in your case the following should work:

value_template: “{{ value_json[‘homeassistant’][‘qemux86-64’] }}”

3 Likes

It’s because when you call out an object, it expects a full object. This is a byproduct of coding. In coding, you are not alloud to use characters for variable/object names. Your object name is 'value_json.homeassistant.qemux86-64'. Well, code doesn’t like the -, because that means subtract. So the template is trying to subtract 64 from value_json.homeassistant.qemux86. In order to use special characters with objects, you need to use a method that works on a dictionary (because this json object is a dictionary). So these methods should work:

value_template: "{{ value_json.homeassistant['qemux86-64'] }}"

or

value_template: "{{ value_json['homeassistant']['qemux86-64'] }}"

or

value_template: "{{ value_json.homeassistant.get('qemux86-64') }}"

or

value_template: "{{ value_json.get('homeassistant').get('qemux86-64') }}"

EDIT: Just to expand on this. If you have spaces in the name or the name starts with a number, you’ll need to use one of these methods as well.

2 Likes

I figured there would be square brackets involved but didn’t know the correct syntax.

1 Like

Thanks to you both. Of course it was the only thing I never tried! I must have googled like mad and I did try and quotes and everything under the sun but never cracked it. Thanks also to @tom_l

Thanks all for your help and information!