Newbee Help in debugging

I am new to home automation having installed it yesterday. Maybe my first effort is a bridge too far given my level of understanding. I want to do a REST GET and send the JSON to my MQTT broker. I think the code works to get the JSON but the broker does not get what I expected. Just in case my error is obvious to the most casual observer I will post it here. I put this code in in configuration.yaml

default_config:
frontend:
  themes: !include_dir_merge_named themes
logger:
  default: info

rest_command:
  get_report_1:
    url: "http://192.168.1.15:32000/Driver/Uranus/Report?DriverUniqueKey=606e7eca-4f45-4956-8faa-e3d0a4b694bd"
    method: "get"

automation:
  - alias: "Retrieve and Publish Report 1 to MQTT"
    trigger:
      - platform: time_pattern
        seconds: 10 # Trigger every 10 seconds
    action:
      - service: rest_command.get_report_1
      - service: mqtt.publish
        data_template:
          topic: "report_1"
          payload_template: "{{ states('rest_command.get_report_1') }}"
          # Assuming 'rest_command.get_report_1' returns a JSON string
      - service: logger.log
        data:
          message: "Response: {{ states('rest_command.get_report_1') }}"
      - service: python_script.parse_json_data
        # Replace 'python_script.parse_json_data' with the correct service to process JSON data

python_script:
  parse_json_data:
    sequence:
      - service: mqtt.publish
        data_template:
          topic: "parsed_data"
          payload_template: "{{ states('rest_command.get_report_1') | from_json | json_query('data') }}" 

and my rest call returns this:

{"status":"success","code":202,"message":"Report.","data":{"uniqueKey":"606e7eca-4f45-4956-8faa-e3d0a4b694bd","name":"Uranus","message":{"temperature":{"temperature":15.4,"messageType":"EnvironmentTemperature"},"relativeHumidity":{"percentage":18,"messageType":"Humidity"},"dewPoint":{"temperature":-8.8,"messageType":"EnvironmentTemperature"},"absolutePressure":{"hPa":823.21,"messageType":"Pressure"},"relativePressure":{"hPa":1020.83,"messageType":"Pressure"},"barometricAltitude":{"meters":1781,"messageType":"Elevation"},"skyQuality":{"mpsas":19.7,"messageType":"SkyQuality"},"nelm":{"vMag":5.29,"messageType":"NakedEyeLimitMagnitude"},"illuminance":{"lux":0.0086,"messageType":"Illuminance"},"temperatureDifference":{"temperature":35.9,"messageType":"EnvironmentTemperature"},"cloudCoverage":{"percentage":12,"messageType":"CloudCoverage"},"skyTemperature":{"temperature":-14.6,"messageType":"EnvironmentTemperature"},"isGpsFixed":true,"dateTime":"2023-11-07T04:40:37","latitude":{"dms":{"degree":35,"minute":11,"second":18.13,"standardFormat":"+35°11′18.13″","messageType":"DMS"},"dd":{"decimalDegree":35.18837,"messageType":"DD"},"messageType":"Latitude"},"longitude":{"dms":{"degree":-106,"minute":30,"second":54,"standardFormat":"-106°30′54.00″","messageType":"DMS"},"dd":{"decimalDegree":-106.515,"messageType":"DD"},"messageType":"Longitude"},"totalSatellites":9,"altitude":{"meters":1781,"messageType":"Elevation"},"messageType":"UranusAggregateReport"}}}

but my broker only gets messageType=UranusAggregate Report report_1= unknown

I would like to see what is going on but I can’t figure out how to debug this. Is there any guidance on how to debug these automations?

Thanks
Kurt

Use a rest sensor rather than a rest command. Your rest command does not store the reply. A sensor will.

However, how many characters are in that rest sensor reply?

The maximum for any state is 255 characters. So check you are not going to exceed that. If you are you will have to store the result in an attribute (not limited to 255 characters).

This is totally wrong:

payload_template: "{{ states('rest_command.get_report_1') }}"

If you use a rest sensor it will be:

payload_template: "{{ states('sensor.your_rest_sensor_object_id_here') }}"

Your trigger actually triggers every minute, when the seconds == 10. Using “/10” would be every ten seconds. However rather than polling needlessly, you could trigger only when the sensor gets a new value:

automation:
  - alias: "Retrieve and Publish Report 1 to MQTT"
    trigger:
      - platform: state
        entity_id: sensor.your_rest_sensor_object_id_here
        not_to:
          - unavailable 
          - unknown 
    action: etc...

Finally I have no idea what you are attempting here but it is not how you use Python scripts.

python_script:
  parse_json_data:
    sequence:
      - service: mqtt.publish
        data_template:
          topic: "parsed_data"
          payload_template: "{{ states('rest_command.get_report_1') | from_json | json_query('data') }}" 

See:

If you’re relying on an AI to build your yaml, you should stop…

2 Likes

Thanks for the quick reply. I didn’t get the expected notification so it took me awhile to realize that you had replied. I will explore your idea.

Kurt

I think I can guess how you know this. I really wanted to get results and not have to spend days trying to understand yaml. Given tom’s input I now have this code that is working

default_config:
frontend:
  themes: !include_dir_merge_named themes
logger:
  default: debug

sensor:
  - platform: rest
    name: uranus_report
    resource: http://192.168.1.15:32000/Driver/Uranus/Report?DriverUniqueKey=606e7eca-4f45-4956-8faa-e3d0a4b694bd
    value_template: "{{ value_json.data }}"
    json_attributes:
      - data
    scan_interval:
      seconds: 10

automation:
  - alias: "Retrieve and Publish Report 1 to MQTT"
    trigger:
      - platform: state
        entity_id: sensor.uranus_report
    action:
      - service: mqtt.publish
        data_template:
          topic: "report_1"
          payload_template: "{{ state_attr('sensor.uranus_report', 'data') }}"

I can understand that for those of you that have been coding for a lifetime that having an AI write some code probably leaves a bad taste. But I have found it helpful. I am very amazed by it as it is very knowledgeable but not very good at coding. For me I code a little C++ and I think Python is a snake. I would like to learn Python but there is only so much time in a lifetime.

Kurt

Well, that’s the thing. None of this is python. This is all a configuration. It’s a yaml file. If you understand data structures, you can pick up yaml very quickly. As for the AI, it’s just simply wrong in most cases for HA. It makes assumptions and makes things up. It’s easy to tell when someone uses it because it’s 100% convincing yaml that is no where close to being correct. If you realy want to learn this, take 10 minutes to understand yaml. The HA side will come as time goes on. Use the automation editor to learn the HA side of yaml.

Beginners shouldn’t use it, because beginners can’t spot the obvious and sometime subtle mistakes it makes. It’s as simple as that (for now at least).

Basic Python is one of the easiest languages to learn. My wife with no programming or analytical background learnt basic Python within days.

Tom I thought I understood your reply but I guess I didn’t . The payload I am receiving is about 1500 characters long. I found where I can see the logs and the log error is:
homeassistant.exceptions.InvalidStateError: Invalid state with length 1527. State max length is 255 characters.

I tried a simple sensor:

sensor:
  - platform: rest
    name: uranus_report
    resource: http://192.168.1.15:32000/Driver/Uranus/Report?DriverUniqueKey=606e7eca-4f45-4956-8faa-e3d0a4b694bd
    scan_interval:
      seconds: 10

I can see the the REST sensor is running every 10 seconds and is getting the correct JSON.
My MQTT trigger looks like this:

  alias: "Publish Uranus Report to MQTT"
  trigger:
    - platform: state
      entity_id: sensor.uranus_report
  action:
    - service: mqtt.publish
      data_template:
        topic: "uranus_report_topic"
        payload_template: "{{ state_attr('sensor.uranus_report', 'data') | tojson }}" 

but it doesn’t get that far.

Kurt

You can’t put the whole rest result in your state. You can only put it in your attributes assuming the attributes can support your endpoints json. Please post the contents of your JSON, then we can help you with your rest sensor.

EDIT: Nevermind I see you posted it above.

This should be your rest sensor.

sensor:
  - platform: rest
    name: uranus_report
    resource: http://192.168.1.15:32000/Driver/Uranus/Report?DriverUniqueKey=606e7eca-4f45-4956-8faa-e3d0a4b694bd
    value_template: >
      {{ value_json.status }}
    json_attributes:
    - code
    - message
    - data
    scan_interval:
      seconds: 10