Package doesn't get the correct states

Hi all,

I write a package with the following code:

homeassistant:

  customize:
    sensor.meteonetwork:
      friendly_name: Stazione Meteonetwork
      icon: mdi:cloud-braces   

sensor:
  - platform: rest
    name: meteonetwork
    json_attributes:
      - observation_time_local
      - station_code
      - place
      - region_name
      - country
      - latitude
      - longitude
      - altitude
      - temperature
      - smlp
      - rh
      - wind_speed
      - wind_direction
      - rain_rate
      - dew_point
      - current_tmin
      - current_tmax
      - daily_rain
    resource: https://api.meteonetwork.it/v3/data-realtime/laz290
    value_template: "{{ value_json[0].area }}"
    verify_ssl: false    
    scan_interval: 300
    headers:
      Authorization: !secret meteonetwork_token

  - platform: template
    sensors:
      meteonetwork_last_update:
        friendly_name: "Ultimo aggiornamento"
        value_template: "{{ state_attr('sensor.meteonetwork', 'observation_time_local') }}"
        icon_template: mdi:clock-outline
      meteonetwork_temp:
        friendly_name: "Temperatura"
        value_template: "{{ state_attr('sensor.meteonetwork', 'temperature') | float | round(1) }}"
        unit_of_measurement: "°C"
      meteonetwork_rh:
        friendly_name: "Umidita relativa"
        value_template: "{{ state_attr('sensor.meteonetwork', 'rh') | int }}"
        unit_of_measurement: "%"
        icon_template: mdi:water-percent
      meteonetwork_press:
        friendly_name: "Pressione"
        value_template: "{{ state_attr('sensor.meteonetwork', 'smlp') | float | round(1) }}"
        unit_of_measurement: "hPa"
        icon_template: mdi:gauge
      meteonetwork_wind:
        friendly_name: "Vento"
        value_template: "{{ state_attr('sensor.meteonetwork', 'wind_speed') | int }} ({{state_attr('sensor.meteonetwork', 'wind_direction') }})"
        unit_of_measurement: "Km/h"
        icon_template: mdi:weather-windy
      meteonetwork_rain:
        friendly_name: "Pioggia"
        value_template: "{{ state_attr('sensor.meteonetwork', 'rain_rate') | int }}"
        unit_of_measurement: "mm/h"
        icon_template: mdi:weather-pouring
      meteonetwork_dew_point:
        friendly_name: "Temperatura rugiada"
        value_template: "{{ state_attr('sensor.meteonetwork', 'dew_point') | float | round(1) }}"
        unit_of_measurement: "°C"
        icon_template: mdi:water

automation:
  # Notifica su app di pioggia
  - alias: "Meteo__Notifica_pioggia"
    trigger:
      platform: numeric_state
      entity_id: sensor.meteonetwork_rain
      above: 1
      for: '00:10:00'
    condition: []
    action:
       service: notify.mobile_app
       data_template:
         title: "Meteo"
         message: "Pioggia persistente a {{ trigger.to_state.state }} mm/h"   
  # Notifica su app di vento forte
  - alias: "Meteo__Notifica_vento"
    trigger:
      platform: numeric_state
      entity_id: sensor.meteonetwork_wind
      above: 1
      for: '00:10:00'
    condition: []
    action:
       service: notify.mobile_app_oneplus
       data_template:
         title: "Meteo"
         message: "Vento forte a {{ trigger.to_state.state }} kmh" 
         

All the sensor states have “unknown” state, but if I run the following command:

curl --location --request GET 'https://api.meteonetwork.it/v3/data-realtime/laz290' \
--header 'Authorization: Bearer 1234|questoeilmiotokenunicoperaccedereameteonetworkapigratuitamente'

I get the right information from network station called “laz290” and yet I correctly included the package into configuration.yaml

I don’t know what I miss to get the sensors populated.

This does not make sense in combination with what you have for json_attributes — what is the response from the resource? At a guess you don’t need the [0], which would suggest the response is a list, but thejson_attributes says it’s a dict.

I assume you’ve removed private information from the token — the curl request doesn’t work for me as written.

Sorry, what is a dict?

Absolutely yes, it is totally invented. It’s not the real token I use.

EDIT
anyway you’re right, in the log I have the error:

Template variable error: dict object has no element 0 when rendering '{{ value_json[0].area }}'

Deleting “[0]” this error disappears. But I still don’t have sensors populated

https://jinja.palletsprojects.com/en/latest/templates/#literals

Until you post the JSON response we cannot help.

I figured out where the issue was.
In the secrets.yaml file I had to put:

meteonetwork_token: Bearer 12345|kkkkkYgyyyyy

rather than:

meteonetwork_token: 12345|kkkkkYgyyyyy

1 Like

If running the curl command for a meteostation, I get the following response:

{"error":true,"status_code":204,"message":"No results found or the weather station license does not allow the distribution of weather data"}#

About you, could I do something different in my package or there’s nothing to do for that station?