REST Sensor - Nested JSON by "Technische Alternative"

Hi!

I have another beginner question.
From a “Technische Alternative UVR1611 + CMI” heating control I receive the following JSON-data (shown in abbreviated form)

{

    "Header":{
        "Version":5,
        "Device":"80",
        "Timestamp":1627911243
    },
    "Data":{
        "Inputs":[
            {
                "Number":1,
                "AD":"A",
                "Value":{
                    "Value":40.0,
                    "Unit":"1"
                }
            },
            {
                "Number":2,
                "AD":"A",
                "Value":{
                    "Value":70.5,
                    "Unit":"1"
                }
            },
            {
                "Number":3,
                "AD":"A",
                "Value":{
                    "Value":34.9,
                    "Unit":"1"
                }
            }
        ],
        "Outputs":[
            {
                "Number":1,
                "AD":"A",
                "Value":{
                    "State":0,
                    "Value":0,
                    "Unit":"0"
                }
            },
            {
                "Number":2,
                "AD":"A",
                "Value":{
                    "State":0,
                    "Value":0,
                    "Unit":"0"
                }
            },
            {
                "Number":3,
                "AD":"D",
                "Value":{
                    "Value":0,
                    "Unit":"0"
                }
            },
            {
                "Number":4,
                "AD":"D",
                "Value":{
                    "Value":0,
                    "Unit":"0"
                }
            },
            {
                "Number":5,
                "AD":"D",
                "Value":{
                    "Value":0,
                    "Unit":"0"
                }
            },
            {
                "Number":6,
                "AD":"A",
                "Value":{
                    "State":1,
                    "Value":30,
                    "Unit":"0"
                }
            }
        ]
    },
    "Status":"OK",
    "Status code":0

}

Now I like to add a sensor via REST-Api, that shows me for instance the value of input number 1 - 40.0.
But when adding a sensor with the configuation

value_template: ‘{{states.sensor.SENSORNAME.Data.Inputs[1].Value[1].Value}}’

it can’t receive any data, although it should be “40.0”?
I’ve tried several different ways to implement value_template, but it won’t work (“Header” and “Status” are no problem to receive).

Can you help me figuring out my mistake?

Kind regards,
gstoetti94

EDIT

Thanks to @koying and @Hellis81 there is the solution:

      cmi34_tkollektor:
        value_template: '{{ state_attr("sensor.cmi34","Data")["Inputs"][0]["Value"]["Value"] }}'
        friendly_name: 'TKollektor'

At first sight, it should be:

{{value_json["Data"]["Inputs"][0]["Value"]["Value"]}}

value_json is the JSON payload returned by the REST call

1 Like

Unfortunately it does not work?

  - platform: rest
    name: cmi34
    scan_interval: 30
    resource: http://*USER*:*PASSWORD*@*IP-Adress*/INCLUDE/api.cgi?jsonnode=1&jsonparam=I,O
    json_attributes:
      - Header
      - Data
      - Status
    value_template: '{{ value_json.cmi34 }}'
  - platform: template
    sensors:
       cmi34_status:
        value_template: '{{ states.sensor.cmi34.attributes["Status"] }}'
        friendly_name: 'cmi34_Status'  

      cmi34_tkollektor:
        value_template: '{{ value_json["Data"]["Inputs"][0]["Value"]["Value"] }}'
        friendly_name: 'TKollektor'

This is my sensor-config, as a result of several tutorials.

I changed value_template in my config.yaml, but still it does not show the value of input number 1, just “not available”?
“Status” has no problems, so at least the connection is fine?

You can only use value_json in the rest sensor.

You need to use the same format as your status sensor. Meaning:

      cmi34_tkollektor:
        value_template: '{{ states.sensor.cmi34.attributes["Data"]["Inputs"][0]["Value"]["Value"] }}'
        friendly_name: 'TKollektor'

Or better:

      cmi34_tkollektor:
        value_template: '{{ states_attr('sensor.cmi34','Data')["Inputs"][0]["Value"]["Value"] }}'
        friendly_name: 'TKollektor'

Since it won’t display errors at startup.

3 Likes

Of the importance of including ALL information in your post… :roll_eyes:

Also, the value_template of the REST sensor leads to nothing…
Try something like

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

Thank you!
The first -

      cmi34_tkollektor:
        value_template: '{{ states.sensor.cmi34.attributes["Data"]["Inputs"][0]["Value"]["Value"] }}'
        friendly_name: 'TKollektor'

works fine and shows the correct value!

For some reason, the Home-Assistant File-Editor and the Config-Check show an error when using

value_template: '{{ states_attr('sensor.cmi34','Data')["Inputs"][0]["Value"]["Value"] }}

Error loading /config/configuration.yaml: while parsing a block mapping
in “/config/configuration.yaml”, line 385, column 9
expected , but found ‘’
in “/config/configuration.yaml”, line 385, column 42

Sorry! It’s my second day in home assistant/yaml and I’m just trying to understand the basic functions! :see_no_evil:

I will try to change the value_template of the REST-Sensor

It’s state_attr, without “s”

1 Like

I keep falling for that trap…
states.sensor…
states()
state_attr()

It just doesn’t make sense to me…

1 Like

You cannot use the same type of quote inside a string than the enclosing ones (I assume the missing final quote in your post is a copy/paste mistake)

try

value_template: '{{ state_attr("sensor.cmi34","Data")["Inputs"][0]["Value"]["Value"] }}'

or

value_template: >
  {{ state_attr('sensor.cmi34','Data')["Inputs"][0]["Value"]["Value"] }}

1 Like
value_template: '{{ state_attr("sensor.cmi34","Data")["Inputs"][0]["Value"]["Value"] }}'

Workes perfectly!

Thanks to both of you!

Hello @gstoetti94,

i am complet new to HA just managed to get my water and gas into HA. I would like to get the data of my TA UVR1611 + CMI in HA, but did not find a working instruction. Could you pleas explain how you managed to get the data in your HA?

Thank you and best regards
Alex

Hi @alex1976 and @gstoetti94 ,

i’m running into some issues with the CMI and HA, so could you please share the CMI related part of your configuration.yaml with me?

Kind Regards
Martin

Hi! @gstoetti94
I’m newbie to HM and I tried to use the following template to connect with my CMI:

- platform: rest
    name: cmi34
    scan_interval: 30
    resource: http://admin:[email protected]/INCLUDE/api.cgi?jsonnode=1&jsonparam=I,O
    json_attributes:
      - Header
      - Data
      - Status
    value_template: '{{ state_attr("sensor.cmi34","Data")["Inputs"][2]["Value"]["Value"] }}'

  - platform: template
    sensors:
       cmi34_status:
        value_template: '{{ state.sensor.cmi34.attributes["Status"] }}'
        friendly_name: 'cmi34_Status'  

      cmi34_tkollektor:
        value_template: '{{ state.sensor.cmi34.attributes["Data"]["Inputs"][2]["Value"]["Value"] }}'
        friendly_name: 'TKollektor'

error message in Dev Tools Template :

UndefinedError: ‘None’ has no attribute ‘Inputs’

The URL seam to work cause a get a json file back:

{ "Header":{ "Version":6, "Device":"80", "Timestamp":1666538131 }, "Data":{ "Inputs":[ { "Number":1, "AD":"A", "Value":{ "Value":49.9, "Unit":"1" } } , { "Number":2, "AD":"A", "Value":{ "Value":57.5, "Unit":"1" } } , { "Number":3, "AD":"D", "Value":{ "Value":0, "Unit":"43" } } , { "Number":4, "AD":"A", "Value":{ "Value":48.9, "Unit":"1" } } , { "Number":5, "AD":"D", "Value":{ "Value":0, "Unit":"43" } } , { "Number":6, "AD":"A", "Value":{ "Value":52.3, "Unit":"1" } } , { "Number":7, "AD":"A", "Value":{ "Value":55.5, "Unit":"1" } } , { "Number":8, "AD":"D", "Value":{ "Value":0, "Unit":"43" } } , { "Number":9, "AD":"A", "Value":{ "Value":54.7, "Unit":"1" } } , { "Number":10, "AD":"A", "Value":{ "Value":25.7, "Unit":"1" } } , { "Number":11, "AD":"D", "Value":{ "Value":0, "Unit":"43" } } , { "Number":12, "AD":"A", "Value":{ "Value":14.5, "Unit":"1" } } , { "Number":13, "AD":"A", "Value":{ "Value":22.3, "Unit":"1" } } , { "Number":14, "AD":"A", "Value":{ "Value":49.2, "Unit":"1" } } , { "Number":15, "AD":"A", "Value":{ "Value":44.7, "Unit":"1" } } , { "Number":16, "AD":"A", "Value":{ "Value":0, "Unit":"3" } } ], "Outputs":[ { "Number":1, "AD":"A", "Value":{ "State":0,"Value":0, "Unit":"0" } } , { "Number":2, "AD":"A", "Value":{ "State":0,"Value":0, "Unit":"0" } } , { "Number":3, "AD":"D", "Value":{ "Value":1, "Unit":"0" } } , { "Number":4, "AD":"D", "Value":{ "Value":0, "Unit":"0" } } , { "Number":5, "AD":"D", "Value":{ "Value":0, "Unit":"0" } } , { "Number":6, "AD":"A", "Value":{ "State":0,"Value":0, "Unit":"0" } } , { "Number":7, "AD":"A", "Value":{ "State":0,"Value":0, "Unit":"0" } } , { "Number":8, "AD":"D", "Value":{ "Value":0, "Unit":"0" } } , { "Number":9, "AD":"D", "Value":{ "Value":0, "Unit":"0" } } , { "Number":10, "AD":"D", "Value":{ "Value":0, "Unit":"0" } } , { "Number":11, "AD":"D", "Value":{ "Value":0, "Unit":"0" } } , { "Number":12, "AD":"D", "Value":{ "Value":0, "Unit":"0" } } , { "Number":13, "AD":"D", "Value":{ "Value":0, "Unit":"0" } } ]}, "Status":"OK", "Status code":0 }

any Idea why i don’t work ?

Hi @shufschmid !

Thats how I have done it and it works pretty well.

In my configuration.yaml I set:

rest:
  - authentication: basic
    username: "****"
    password: "****"
    scan_interval: 30
    resource: http://****:****@192.168.0.11/INCLUDE/api.cgi?jsonnode=3&jsonparam=I,O,Na,Nd
    sensor:
      - name: "cmi_og_3"
        value_template: '{{ value_json.cmi_og_3 }}'
        json_attributes:
          - "Header"
          - "Data"
          - "Status"     

Then in my templates.yaml I set:

- sensor:
    - name: "CMI OG 3 Status"
      state: '{{ states.sensor.cmi_og_3.attributes["Status"] }}'

- sensor:
    - name: "CMI OG Vorlauftemperatur Heizkreis Süden"
      state: '{{ states.sensor.cmi_og_3.attributes["Data"]["Inputs"][0]["Value"]["Value"] }}'

- sensor:
    - name: "CMI OG Vorlauftemperatur Heizkreis Norden"
      state: '{{ states.sensor.cmi_og_3.attributes["Data"]["Inputs"][1]["Value"]["Value"] }}'

- sensor:
    - name: "CMI OG Rücklauftemperatur Heizkreis"
      state: '{{ states.sensor.cmi_og_3.attributes["Data"]["Inputs"][3]["Value"]["Value"] }}'

- sensor:
    - name: "CMI OG Temperatur Aussen"
      state: '{{ states.sensor.cmi_og_3.attributes["Data"]["Inputs"][4]["Value"]["Value"] }}'

- sensor:
    - name: "CMI OG Temperatur Innen"
      state: '{{ states.sensor.cmi_og_3.attributes["Data"]["Inputs"][5]["Value"]["Value"] }}'

And that’s how it looks like in HA:

image

I Hope I could help!

The one thing I don’t know is how to turn things on or off - so how to send something via HA to the CMI. For example turn on the Heat Pump when … Maybe someday someone can help me with that.

1 Like

Firstly thanks for sharing you knowledge.

We have also a C.M.I. and UVR16x2 and I’ll tried some different approaches to get the data into HA. THe described solution in this topic seems more generic than my current approach…

I’m not sure if it’s possible to set some of the outputs of our UVR16 as i can not find a way how it can be done with the help of the C.M.I. webinterface or the cloud interface…
Does anyone know how to set a output of our UVR16 with the help of C.M.I.?

It is not possible. The CMI-JSON-API is “read only”. But I think my info has a long beard :slight_smile:

1 Like

You can controll the CMI in HomeAssistant: