Map JSON value to String in REST Sensor

Hi,

this is my first encounter with HA after becoming a bit frustrated with OpenHab :wink: My current “problem” is mapping the value I get out of a REST Sensor to a string. Background is a leakage protection device from a company named Syr. It has an API that responds to HTTP get requests with a JSON answer. So far so good, I get the correct values displayed in my default dashboard

# REST Sensor
sensor:
  - platform: rest
    resource: http://syr.mydomain.com:5333/safe-tec/get/ALA
    value_template: "{{value_json['getALA'] }}"

The value for getALA can be eight different values, each described with two characters. For example, FF is the code for “no alarm”, while A4 is the code for “Max flow alarm” and so on. Is there a way to map the values directly in this sensor or do I have to create a template for this? If it is the latter, do I always have to create two entries for such a device/entity?

Thanks & best regards,
Klayman

# REST Sensor
sensor:
  - platform: rest
    resource: http://syr.mydomain.com:5333/safe-tec/get/ALA
    value_template: >
      {% set codes =
        { "FF": "No alarm",
          "A4": "Max flow alarm",
          "B2": "Something else",
          "D1": "Etc"
        } %}
      {{ codes.get(value_json['getALA'], 'unknown') }}

codes is a Jinja2 variable whose value is a dictionary. The dictionary’s keys correspond to the received hex values and the dictionary’s values are the associated code descriptions. You’ll need to expand the dictionary to contain the eight possible codes that can be received.

This line basically uses the received getALA value to find a matching key and report the associated value. If it fails to find a matching key, it reports the supplied default value unknown.

1 Like

Thank you. Worked like a charm. I have another question :wink: : The device has many different variables I can read out. They can either be be obtained individually or together with http://syr.mydomain.com:5333/safe-tec/get/all and will be returned as follows:

{"getSRN":"212511451","getVER":"Safe-Tech V3.56","getTYP":"140","getCNO":"RDGY638Y418Y4ZZ","getMAC":"0c:73:eb:51:4b:c5","getAB":"1","getPRF":"1","getPRN":"2","getPN1":"Home","getPV1":"150","getPT1":"30","getPF1":"3500","getPM1":"1","getPR1":"0","getPB1":"1","getPA1":"1","getPW1":"1","getPN2":"Away","getPV2":"30","getPT2":"30","getPF2":"3500","getPM2":"1","getPR2":"0","getPB2":"1","getPA2":"1","getPW2":"0","getPN3":"Profile 3","getPV3":"300","getPT3":"60","getPF3":"3500","getPM3":"1","getPR3":"0","getPB3":"1","getPA3":"0","getPW3":"0","getPN4":"Profile 4","getPV4":"300","getPT4":"60","getPF4":"3500","getPM4":"1","getPR4":"0","getPB4":"1","getPA4":"0","getPW4":"0","getPN5":"Profile 5","getPV5":"300","getPT5":"60","getPF5":"3500","getPM5":"1","getPR5":"0","getPB5":"1","getPA5":"0","getPW5":"0","getPN6":"Profile 6","getPV6":"300","getPT6":"60","getPF6":"3500","getPM6":"1","getPR6":"0","getPB6":"1","getPA6":"0","getPW6":"0","getPN7":"Profile 7","getPV7":"300","getPT7":"60","getPF7":"3500","getPM7":"1","getPR7":"0","getPA7":"0","getPB7":"1","getPW7":"0","getPN8":"Profile 8","getPV8":"300","getPT8":"60","getPF8":"3500","getPM8":"1","getPR8":"0","getPB8":"1","getPA8":"0","getPW8":"0","getLWT":"90","getTMP":"0","getVLV":"20","getCEL":"145","getBAR":"4800 mbar","getFLO":"0","getNPS":"5929","getALA":"FF","getALM":"Alarms:->A4 A3 AE A4 A3 AE A4 A3","getDMA":"1","getAVO":"0mL","getVOL":"Vol[L]14776","getLTV":"1","get71":"ERROR: ADM","getBAT":"9,42","getNET":"12,49","getBUZ":"1","getDBD":"10","getDBT":"15","getDST":"180","getDCM":"3","getDOM":"60","getDPL":"10","getDTC":"3","getDRP":"1","getWFC":"Pretty Fly for a WiFi","getWFS":"2","getWFR":"32","getRTC":"1681654816","getIDS":"1","getTMZ":"4","getFSL":[],"getCND":"874","getCNL":"0","getCNF":"20","getSLP":"0","getSLE":"0","getSLV":"168","getSLT":"1800","getSLF":"4600","getWAD":"1","getAPT":"3600","getWIP":"192.168.20.215","getWGW":"192.168.20.1","getSFV":"0"}

Is there a way I can map each of them to an individual sensor/binary sensor in HA? Of course I don’t need all of them, but about 8-10 would be nice…

Kind regards,
Klayman

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

Yes. Refer to the example in the REST integration.