Commandline sensor with Json response and multiple attriubtes

Hey guys,

Situation: I have a heating system that talks with the cloud portal they provide. There are no API/Rest/… services provided; only an ugly UI. I wrote a python script with mechanicalsoup to access this portal; read some values and return them as json. This result looks like:

{"Heizung": {"Aussentemperatur_Aktuell": "4,2 \u00b0C", "Aussentemperatur_Minimum": "2,5 \u00b0C", "Aussentemperatur_Maximum": "17 \u00b0C", "Warmwassertemperatur_Aktuell": "55,5 \u00b0C", "Vorlauftemperatur_Aktuell_HZK1": "29,7 \u00b0C", "Istleistung_Aktuell_WE0": "0 %", "Kesseltemperatur_Aktuell_WE0": "45,1 \u00b0C", "Anlagendruck_VPT_Aktuell_WE0": "1,9 bar", "Weichentemperatur_Aktuell": "53,2 \u00b0C"}}

My main goal now would be to integrate these values into Home Assistant and for that I created the following configuration entry:

sensor
  - platform: command_line
    name: heating
    value_template: '{{ value_json["Heizung"] }}'
    json_attributes:
      - Aussentemperatur_Aktuell
      - Aussentemperatur_Minimum
      - Aussentemperatur_Maximum
      - Warmwassertemperatur_Aktuell
      - Vorlauftemperatur_Aktuell_HZK1
      - Istleistung_Aktuell_WE0
      - Kesseltemperatur_Aktuell_WE0
      - Anlagendruck_VPT_Aktuell_WE0
      - Weichentemperatur_Aktuell
    command: 'python3 /home/homeassistant/.homeassistant/scripts/wem_status.py'
    scan_interval: 600

The script is executed, returns the json but what I get in Home assistant is:

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity_platform.py", line 408, in _async_add_entity
    await entity.async_update_ha_state()
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity.py", line 275, in async_update_ha_state
    self._async_write_ha_state()
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity.py", line 394, in _async_write_ha_state
    self.entity_id, state, attr, self.force_update, self._context
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/core.py", line 988, in async_set
    state = State(entity_id, new_state, attributes, last_changed, None, context)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/core.py", line 733, in __init__
    ).format(entity_id)
homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity id: sensor.heating. State max length is 255 characters.

I hope that someone could point me into the right direction.

Thanks!
Dirk

Put a dummy state in the sensor. Only the attributes can store more than 255 characters. See

didn’t get you. I dont want to store moren than 255 chars. I just want to store each value of the json into a separate attribute of the sensor…

got it working with your tipp! this was the solution:

  - platform: command_line
    name: heating
    json_attributes:
      - Aussentemperatur_Aktuell
      - Aussentemperatur_Minimum
      - Aussentemperatur_Maximum
      - Warmwassertemperatur_Aktuell
      - Vorlauftemperatur_Aktuell_HZK1
      - Istleistung_Aktuell_WE0
      - Kesseltemperatur_Aktuell_WE0
      - Anlagendruck_VPT_Aktuell_WE0
      - Weichentemperatur_Aktuell
    value_template: "{{ value_json['Aussentemperatur_Aktuell'] }}"
    command: 'python3 /home/homeassistant/.homeassistant/scripts/wem_status.py'
    scan_interval: 600

with this json:

{"Aussentemperatur_Aktuell": "4,7 \u00b0C", "Aussentemperatur_Minimum": "2,5 \u00b0C", "Aussentemperatur_Maximum": "17 \u00b0C", "Warmwassertemperatur_Aktuell": "50 \u00b0C", "Vorlauftemperatur_Aktuell_HZK1": "29,3 \u00b0C", "Istleistung_Aktuell_WE0": "0 %", "Kesseltemperatur_Aktuell_WE0": "26,1 \u00b0C", "Anlagendruck_VPT_Aktuell_WE0": "1,89 bar", "Weichentemperatur_Aktuell": "42,7 \u00b0C"}

This:

value_template: '{{ value_json["Heizung"] }}'

Is trying to store this:

{"Aussentemperatur_Aktuell": "4,2 \u00b0C", "Aussentemperatur_Minimum": "2,5 \u00b0C", "Aussentemperatur_Maximum": "17 \u00b0C", "Warmwassertemperatur_Aktuell": "55,5 \u00b0C", "Vorlauftemperatur_Aktuell_HZK1": "29,7 \u00b0C", "Istleistung_Aktuell_WE0": "0 %", "Kesseltemperatur_Aktuell_WE0": "45,1 \u00b0C", "Anlagendruck_VPT_Aktuell_WE0": "1,9 bar", "Weichentemperatur_Aktuell": "53,2 \u00b0C"}

Which is more than 255 characters. Which a state can’t contain.

Don’t do it. Put something less than that in the value_template (one of the sensor values perhaps). Then use the restful sensor attributes - which can store more than 255 characters.

exactly did that; its running already :slight_smile: thanks!

1 Like

I will be grateful if you attach a script.

thank you in advance

what exactly are you trying to solve? do you want to access a Weishaupt heating system? If yes go this direction: Weishaupt integration - #15 by dm82m

Otherwise it makes no sense to ask for the wem_status.py script as this one is just accessing a Weishaupt heating and extracting the data in JSON forma that is then handled by the sensor description above

Your original .json would have worked with this code:

sensor
  - platform: command_line
    name: heating
    value_template: "{{ value_json['Heizung'][] }}"
    json_attributes:
      - Heizung
    command: 'python3 /home/homeassistant/.homeassistant/scripts/wem_status.py'
    scan_interval: 600
2 Likes

I want to extract Sensor data from this Json

{
    "body": {
        "header": {
            "access_no": 19,
            "identification": "0x00, 0x34, 0x54, 0x12",
            "manufacturer": "SEN",
            "medium": "0x4",
            "sign": "0x0, 0x0",
            "status": "0x0",
            "type": "0x72",
            "version": "0x19"
        },
        "records": [
            {
                "function": "FunctionType.INSTANTANEOUS_VALUE",
                "type": "VIFUnit.ENERGY_WH",
                "unit": "MeasureUnit.WH",
                "value": 17891000
            },
            {
                "function": "FunctionType.INSTANTANEOUS_VALUE",
                "type": "VIFUnit.VOLUME",
                "unit": "MeasureUnit.M3",
                "value": 2337.1040000000002692104317247867584228515625
            },
            {
                "function": "FunctionType.INSTANTANEOUS_VALUE",
                "type": "VIFUnit.VOLUME_FLOW",
                "unit": "MeasureUnit.M3_H",
                "value": 0.001000000000000000020816681711721685132943093776702880859375
            },
            {
                "function": "FunctionType.INSTANTANEOUS_VALUE",
                "type": "VIFUnit.POWER_W",
                "unit": "MeasureUnit.W",
                "value": 12
            },
            {
                "function": "FunctionType.INSTANTANEOUS_VALUE",
                "type": "VIFUnit.FLOW_TEMPERATURE",
                "unit": "MeasureUnit.C",
                "value": 39.400000000000005684341886080801486968994140625
            },
            {
                "function": "FunctionType.INSTANTANEOUS_VALUE",
                "type": "VIFUnit.RETURN_TEMPERATURE",
                "unit": "MeasureUnit.C",
                "value": 24.900000000000002131628207280300557613372802734375
            },
            {
                "function": "FunctionType.INSTANTANEOUS_VALUE",
                "type": "VIFUnit.TEMPERATURE_DIFFERENCE",
                "unit": "MeasureUnit.K",
                "value": 14.506000000000000227373675443232059478759765625
            },
            {
                "function": "FunctionType.INSTANTANEOUS_VALUE",
                "type": "VIFUnit.FABRICATION_NO",
                "unit": "MeasureUnit.NONE",
                "value": 345412
            },
            {
                "function": "FunctionType.INSTANTANEOUS_VALUE",
                "type": "VIFUnitExt.CUSTOMER_LOCATION",
                "unit": "MeasureUnit.NONE",
                "value": 345412
            },
            {
                "function": "FunctionType.MORE_RECORDS_FOLLOW",
                "type": "None",
                "unit": "None",
                "value": null
            }
        ]
    },
    "head": {
        "a": "0x0",
        "c": "0x8",
        "crc": "0x29",
        "length": "0x42",
        "start": "0x68",
        "stop": "0x16"
    }
}

but with your code same I got all from Body in one atribute

ive made it work somehow

- platform: command_line
    name: pollucom_energy_wh
    unique_id: "Pollucom WH"
    value_template: '{{ (value_json.body.records[0].value)}}'  
    unit_of_measurement: "Wh"
    json_attributes:
      - body
    command: ssh [email protected] -o StrictHostKeyChecking=no -i /config/id_rsa "sudo python /home/pi/pollucom.py"

but if I want to get more values, I copy the entire block and change the value_template, that triggers the command more often as it should I think