New rest-Sensor not working properly

Hi folks,

I had a well working configuration.yaml (and I thought that I had no issues with integrations, indentation and nesting).

But now I tried to add the sensor-integration with platform:rest and the sensor just won’t work.
I think it’s because of something in the configuration.yaml, but I was not able to figure it out with the documentation (sorry I am still completely new to this).

So this is my configuration.yaml:

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

http:
 use_x_forwarded_for: true
 trusted_proxies:
   - xxx.xxx.xxx.xxx 

#logger:
#  default: warning
#  logs:
#    xknx: debug

sensor:
  - platform: rest
    name: Geosphere Forecast
    unique_id: sensor_geosphere_rain
    resource_template: >-
      https://dataset.api.hub.geosphere.at/v1/timeseries/forecast/nowcast-v1-15min-1km?lat_lon=48.10319%2C15.61224
      &start={{ now().strftime('%Y-%m-%dT%H:%M') }}
      &end={{ (now() + timedelta(hours=3)).strftime('%Y-%m-%dT%H:%M') }}
      &parameters=rr
      &output_format=geojson
    method: GET
    value_template: "{{ value_json | tojson }}"  
    json_attributes:
      - data

template:
  - sensor:
    - name: "Regenanzeige"
      unique_id: sensor_regenanzeige
      state: >
        {% set regen_an = is_state('input_boolean.regenreaktion', 'on') %}
        {% set menge = states('input_number.niederschlagsmenge') | float(0) %}
        {{ 'Regen an' if regen_an else 'Regen aus' }} – {{ menge }} mm

knx:
  light:   !include knx_light.yaml
  cover:   !include knx_cover.yaml
  sensor:  !include knx_sensor.yaml
  weather: !include knx_weather.yaml

I also use an KNX-integration, knx-files are included. just to be sure that there is nothing wrong with these files, the knx_sensor attached:

    - name: "Weather Brigthness"
      state_address: "5/1/9"
      type: illuminance
      sync_state: every 1
      
    - name: "Weather Temperature"
      state_address: "5/1/4"
      type: temperature
      sync_state: every 1
      
    - name: "Weather Wind"
      state_address: "5/1/5"
      type: wind_speed_kmh
      sync_state: every 1

Thanks in advance,
kind regards Bernhard

You have line breaks in your resource_template which are being included in the URL.

You could remove all the line breaks for a single long line, but to keep the nice sections, try this:

    resource_template: >-
      {{ "%s%s%s%s%s" %
         ("https://dataset.api.hub.geosphere.at/v1/timeseries/forecast/nowcast-v1-15min-1km?lat_lon=48.10319%2C15.61224",
          "&start=" ~ now().strftime('%Y-%m-%dT%H:%M'),
          "&end=" ~ (now() + timedelta(hours=3)).strftime('%Y-%m-%dT%H:%M'),
          "&parameters=rr",
          "&output_format=geojson") }}

Super-tiny screenshot of yours (top) vs mine (bottom) so you can see the line breaks:

However, the response is over the 255 character limit for a sensor state, so your value_template needs to return something smaller, and there is no top-level data key in the JSON response that I can see. Try:

sensor:
  - platform: rest
    name: Geosphere Forecast
    unique_id: sensor_geosphere_rain
    resource_template: >-
      {{ "%s%s%s%s%s" %
         ("https://dataset.api.hub.geosphere.at/v1/timeseries/forecast/nowcast-v1-15min-1km?lat_lon=48.10319%2C15.61224",
          "&start=" ~ now().strftime('%Y-%m-%dT%H:%M'),
          "&end=" ~ (now() + timedelta(hours=3)).strftime('%Y-%m-%dT%H:%M'),
          "&parameters=rr",
          "&output_format=geojson") }}
    value_template: "{{ now() }}"
    json_attributes:
      - reference_time
      - timestamps
      - features

…which gives this:

If you only want that data key that is deep in the JSON, you’ll need to reference where in the response it is using a JSONPath in the json_attributes_path config key:

json_attributes_path: "$.features[0].properties.parameters.rr"
json_attributes:
  - data

That gives this:

1 Like

Hey Troon,
thanks for your help, looks like that was the missing link.
I can now access the data.

Unfortunatelly the response has timestampand data in different sections.
I was trying to get a list of dictonaries in that kind of way:

[
    {timestamp0: data0},
    {timestamp1: data1},
    ...
]

Is it possible directly in the sensor or do I have to create that list in a script?

I don’t believe that’s possible in one step in a rest sensor. If you start with my first example, with the full attribute set, you then need a template sensor like this (under your existing template: header: don’t add a second one!):

template:
  - sensor:
      - name: Geosphere forecast list
        unique_id: 465e8edd-9b79-4495-81fa-92236818f8e2
        state: "{{ now() }}"
        attributes:
          list: >
            {% set timestamps = state_attr('sensor.geosphere_forecast','timestamps') %}
            {% set data = state_attr('sensor.geosphere_forecast','features')[0]['properties']['parameters']['rr']['data'] %}
            {% set ns = namespace(out=[]) %}
            {% for t, d in zip(timestamps,data) %}
              {% set ns.out = ns.out + [{t:d}] %}
            {% endfor %}
            {{ ns.out }}

Again, the output is in an attribute as they can hold real structures, unlike states which are always stored as strings and limited to 255 characters.

Thanks, I will find my way from there.
Was not aware of namespace(out=[]) %} and how to iterate to a combinded list with zip. Always something to learn :slight_smile:

1 Like