Needs some help parsing JSON

Hello,

I’m trying to parse some JSON data collected using the rest sensor platform however I’m unable to get the correct JSON data. Below the JSON data and the entry in the configuration.yaml.
The sensor is shown in the developer tools but the value is unknown. So I needs some help here as the documentation can’t help me any further. I’m only interested in the “samenv” value.

Thanks Rob

The JSON data retreived:

{
  "liveweer": [
    {
      "plaats": "Elsloo",
      "temp": "20.7",
      "gtemp": "18.1",
      "samenv": "Zwaar bewolkt",
      "lv": "46",
      "windr": "ZW",
      "windms": "5",
      "winds": "3",
      "windk": "9.7",
      "windkmh": "18",
      "luchtd": "1016.4",
      "ldmmhg": "762",
      "dauwp": "8",
      "zicht": "35",
      "verw": "Enkele buien, in het westen en zuiden tijdelijk droger",
      "sup": "06:37",
      "sunder": "20:40",
      "image": "bewolkt",
      "d0weer": "bewolkt",
      "d0tmax": "21",
      "d0tmin": "13",
      "d0windk": "3",
      "d0windknp": "8",
      "d0windms": "4",
      "d0windkmh": "15",
      "d0windr": "ZW",
      "d0neerslag": "4",
      "d0zon": "19",
      "d1weer": "regen",
      "d1tmax": "20",
      "d1tmin": "13",
      "d1windk": "3",
      "d1windknp": "8",
      "d1windms": "4",
      "d1windkmh": "15",
      "d1windr": "Z",
      "d1neerslag": "90",
      "d1zon": "20",
      "d2weer": "regen",
      "d2tmax": "19",
      "d2tmin": "16",
      "d2windk": "4",
      "d2windknp": "12",
      "d2windms": "6",
      "d2windkmh": "22",
      "d2windr": "W",
      "d2neerslag": "80",
      "d2zon": "20",
      "alarm": "0"
    }
  ]
}

The configuration.yaml part:

# Get weather state from weer.nl for home location
sensor:
  - platform: rest
    name: weernlsamenv
    resource: http://weerlive.nl/api/json-data-10min.php?key=c9a1ebc79d&locatie=50.949,5.765
    json_attributes_path: "$.[0]"
    value_template: '{{ value_json[0].samenv }}'

Please properly format your code blocks in the post. The point 11 in the FAQ describe how to do this.

Thanks for your reply, updated the code block.

Could you also do it for the json? That way we can put it into a json beautifier to make it readable

Try:

# Get weather state from weer.nl for home location
sensor:
  - platform: rest
    name: weernlsamenv
    resource: http://weerlive.nl/api/json-data-10min.php?key=c9a1ebc88d&locatie=50.9491,5.7655
    value_template: '{{ value_json.liveweer[0].samenv }}'

The state is still set to ‘unknown’. I will further investigate next weekend but good tips are as always very welcome.
When using https://jsonpath.com with the JSONPath Syntax “$.[0].samenv” I get the correct
value " Geheel bewolkt". The hard part for me is how to get this working in rest sensor platform.

Thanks for you reply, updated the json part to make it more readable. Good tips for future posts!

What @mirekmal wrote should work. When I go to the resource

http://weerlive.nl/api/json-data-10min.php?key=c9a1ebc88d&locatie=50.9491,5.7655

I get

Dagelijkse limiet van 300 verzoeken overstegen, neem een pro-abonnement voor 500.000 dataverzoeken (slechts €45 per maand) bij https://meteoserver.nl

Can you verify the resource is working for you? I believe that says soemthing about a daily limit in dutch. Anyways, you’ll definitely want to set your scan_interval to 300 or more seconds.

Yes there is a limit on the daily api calls, completely forgot about it.
Added a scan_interval to the sensor to prevent exceeding the daily limit.
But most important, I found the correct command line to retrieve the value using the JSONPath Online Evaluator (https://jsonpath.com) by enabling the option ’ Output paths’ .
Below the working code.

# Get weather state from weer.nl for home location
sensor:
  - platform: rest
    name: weernl
    resource: http://weerlive.nl/api/json-data-10min.php?key=1234567&locatie=50.1454,5.4567
    value_template: "{{value_json['liveweer'][0]['samenv']}}"
    scan_interval: 600

syntactically, that’s identical to what @mirekmal wrote.

You’re using the getitem method []

value_json['liveweer'][0]['samenv']

and this is the dot notation that does the same thing

value_json.liveweer[0].samenv

the dot notation is not allowed on that jsonpath.com site that you are using but is allowed in templates inside HA.

Thanks for the explanation!
One more question, is there a way to retrieve the last 3 historical values of this sensor?

What are you trying to do with the last 3? There are ways, but knowing the endgoal chooses the path.

The goal is to compare if the last 3 values all have a specific value like “onbewolkt” (sunny) and if true trigger an automation that will lower my sun screens,

That’s going to be a pain in the ass because it’s non-numerical. Try this template sensor:

- platform: template
  sensors:
    last_3_conditions:
      value_template:
        {% set next = states('sensor.weernl') %}
        {% set previous = states('sensor.last_3_conditions') %}
        {% set previous = previous.split(',') | list %}
        {% set new = previous + [ next ] %}
        {% if new | length < 3 %}
          {{ new | join(',') }}
        {% else %}
          {{ new[-3:] | join(',') }}
        {% endif %}

Then if you want to make sure that a value is in that sensor, inside a template do:

{{ 'xxxx' in states('sensor.last_3_conditions') }}

Thanks, will get in to this. Let you know the results!