Json count

Hi all!

First of all i wanna say thank you for the lots of help here. I’m not a programmer but i can find almost everything what i need to set up HA (pws, inverter, mqtt, etc…)

Now i try to find out how many airplane is above an exact location in real time.
Opensky-network is a good choice (and HA has this integration) but i can not setup scan_interval (HA ignore this order) and my acces limit from opensky will be full soon and my ip restricted. So i read community forum for hours in the last some days and i find an almost working solution:

sensor:
# opensky
  - platform: rest
    name: repcsi500rest
    scan_interval: 2000
    resource: `https://opensky-network.org/api/states/all?lamin=45.8389&lomin=5.9962&lamax=47.8229&lomax=10.5226`
    json_attributes:
      - states

This give me lots of aircraft data in the ‘states’ attribute and them all are separated with ‘- -’ characters, so my idea is to count theese characters.

  - platform: template
    sensors:
      repcsi500db:
        friendly_name: 500km-nyi repcsi
        value_template: "{{ value_json.repcsi500rest | selectattr('states') | select('- -') | count }}"

But this do not give me back anything usable. I try dozen and dozen json settings but i have no new idea, Maybe the code abowe is a great stupidity, as i write i am not a programmer just trying to find the logic in your earlyer advices…

Try:

value_template: "{{ state_attr('sensor.repcsi500rest', 'states') | count }}"

You have already parsed the original JSON with your rest sensor. The dashes you’re seeing are the ones showing you are dealing with lists (that’s what it looks like it in YAML). The double dashes are because that API returns a list of lists. You only care about the outer list, so you just want to count that.

Hi Pieter!

Thank you for your fast answare.
I try your advise but unfortunatelly opensky banned me today, so i renew my ip but it did not help. So I rename the sensors (rest, temp) but now the rest did not appear under entities after several restart. So later i’ll try restart again HA and keep trying. Be back if i see any change.

log:
Logger: homeassistant.helpers.template_entity
Source: helpers/template_entity.py:356
First occurred: 08:40:30 (1 occurrences)
Last logged: 08:40:30
TemplateError(‘TypeError: object of type ‘NoneType’ has no len()’) while processing template ‘Template(“value_template: {{ state_attr(‘sensor.500repcsirest’, ‘states’) | count }}”)’ for attribute ‘_attr_native_value’ in entity ‘sensor.500repcsitemp’

After a little bit of waiting and some more restart… SUCCES

Thank you very much!

1 Like

Not for a long time…
Opensky become so slow i have timeout today every time

So, does anyone have idea how can i count airplanes abowe a location?