KNMI rest sensor configuration (and preselect individual items in a huge attribute...)

For those of you interested in the KNMI (Dutch national weather service) weather alerts, Id like to share some rest sensors, I’ve been able to configure, based on the resource rss feed below.

After some chatter in DutchDomitics where this api was sourced to a complete and huge single attribute and some template sensors on that, I figured we should be able to narrow that attribute to the individual item member of that attribute we require for our homes.

Just replace the Province name to your own location:

  - resource: https://cdn.knmi.nl/knmi/xml/rss/rss_KNMIwaarschuwingen.xml
    scan_interval: 900

    sensor:

# this creates the huge single attribute sensor, which is fine, and required to check once for the attributes available, but is way too large to deal with in HA state machine
#       - name: KNMI waarschuwingen
#         value_template: >
#           {{now()}}
#         json_attributes_path: '$.rss.channel'
#         json_attributes:
#           - item

# this selects the correct Province in the list, sets that as Path and next json_attributes simply find the right attributes for us
      - name: KNMI waarschuwingen NB
        value_template: >
          {{now()}}
        json_attributes_path: "$.rss.channel.item[?(@.title=='Waarschuwingen Noord-Brabant')]"
        json_attributes:
          - title
          - description
          - link
          - guid
          - pubDate
          - author

# this creates a separate sensor for the 'main' entry in that huge list, which contains valuable in formation nation wide, like the nation wether alert, and some nice gifs.
      - name: KNMI waarschuwingen basis
        value_template: >
          {{now()}}
        json_attributes_path: "$.rss.channel.item[0]"
        json_attributes:
          - title
          - category
          - description
          - enclosure
          - link
          - guid
          - pubDate
          - author


next we can use these to create separate template sensors. Or, maybe even cooler, use it as content in a frontend card using markdown:

  - type: entities
    card_mod:
      class: class-header-margin
    title: KNMI
    entities:
      - type: custom:hui-element
        card_type: markdown
        card_mod:
          style: |
            ha-card {
              box-shadow: none;
              margin: -8px -16px;
            }
        content: >
          **Actueel:** <br>
          {{state_attr('sensor.knmi_waarschuwingen_nb','description')}}

          [KNMI]({{state_attr('sensor.knmi_waarschuwingen_nb','link')}})
      - entity: sensor.knmi_dauwpunt
        name: Dauwpunt
      - entity: sensor.knmi_gevoelstemperatuur
        name: Gevoelstemperatuur
      - entity: sensor.knmi_korte_dagverwachting
        name: Korte dagverwachting
      - entity: sensor.knmi_omschrijving
        name: Omschrijving
      - entity: sensor.knmi_relatieve_luchtvochtigheid
        name: Relatieve luchtvochtigheid
      - entity: binary_sensor.knmi_waarschuwing
        name: Waarschuwing
      - entity: sensor.knmi_wind
        name: Wind
      - entity: binary_sensor.knmi_zon
        name: Zon

Credits for the other entities in that card go to a Custom component GitHub - golles/ha-knmi: Custom component that integrates KNMI weather service in to Home Assistant.
Since that uses another service than the real resource at KNMI, I decided to check at the source itself, and not go through another proxy for those warnings.
Maybe we can even find the weather entities like that, haven’t checked just yet.

Must have a look at the Open Data portal Open Data | KNMI Dataplatform

If you’d be able to further optimize the rest sensor selector, Id be happy to know.

btw, this is the complete attribute of the core sensor without further selection:

item: 
- title: 'Code groen: Er zijn geen waarschuwingen'
  description: >-
    <p>Er zijn momenteel geen waarschuwingen van kracht.</p><br>Uitgifte:
    11/02/2023 11:13 uur LT
     <p>![](upload://h5mkbzfHNKhJWXtUnCzUbbqMSQR.gif)&nbsp;<img src="https://cdn.knmi.nl/knmi/map/current/weather/warning/waarschuwing_land_1_new.gif" width=300>&nbsp;<img src="https://cdn.knmi.nl/knmi/map/current/weather/warning/waarschuwing_land_2_new.gif" width=300></p>
  enclosure:
    '@type': image/gif
    '@url': >-
      https://cdn.knmi.nl/knmi/map/current/weather/warning/waarschuwing_land_48_new.gif
    '@length': '11552'
  link: https://www.knmi.nl/waarschuwingen
  guid: >-
    https://www.knmi.nl/waarschuwingen?Warnings_Sat%2011%20Feb%202023%2011:13:00%20+0100
  pubDate: Sat, 11 Feb 2023 11:13:00 +0100
  category:
    - '@domain': https://www.knmi.nl
      '#text': Weather
    - '@domain': https://www.knmi.nl
      '#text': Weer
  author: [email protected] (Webmaster KNMI)
- title: Waarschuwingen Waddeneilanden
  description: Code Groen. Er zijn momenteel geen waarschuwingen van kracht.
  link: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/waddeneilanden
  guid: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/waddeneilanden
  pubDate: Sat, 11 Feb 2023 11:13:00 +0100
  category:
    - '@domain': https://www.knmi.nl
      '#text': Regional Weather
    - '@domain': https://www.knmi.nl
      '#text': Regionaal Weer
  author: [email protected] (Webmaster KNMI)
- title: Waarschuwingen Groningen
  description: Code Groen. Er zijn momenteel geen waarschuwingen van kracht.
  link: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/groningen
  guid: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/groningen
  pubDate: Sat, 11 Feb 2023 11:13:00 +0100
  category:
    - '@domain': https://www.knmi.nl
      '#text': Regional Weather
    - '@domain': https://www.knmi.nl
      '#text': Regionaal Weer
  author: [email protected] (Webmaster KNMI)
- title: Waarschuwingen Friesland
  description: Code Groen. Er zijn momenteel geen waarschuwingen van kracht.
  link: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/friesland
  guid: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/friesland
  pubDate: Sat, 11 Feb 2023 11:13:00 +0100
  category:
    - '@domain': https://www.knmi.nl
      '#text': Regional Weather
    - '@domain': https://www.knmi.nl
      '#text': Regionaal Weer
  author: [email protected] (Webmaster KNMI)
- title: Waarschuwingen Drenthe
  description: Code Groen. Er zijn momenteel geen waarschuwingen van kracht.
  link: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/drenthe
  guid: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/drenthe
  pubDate: Sat, 11 Feb 2023 11:13:00 +0100
  category:
    - '@domain': https://www.knmi.nl
      '#text': Regional Weather
    - '@domain': https://www.knmi.nl
      '#text': Regionaal Weer
  author: [email protected] (Webmaster KNMI)
- title: Waarschuwingen Noord-Holland
  description: Code Groen. Er zijn momenteel geen waarschuwingen van kracht.
  link: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/noord-holland
  guid: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/noord-holland
  pubDate: Sat, 11 Feb 2023 11:13:00 +0100
  category:
    - '@domain': https://www.knmi.nl
      '#text': Regional Weather
    - '@domain': https://www.knmi.nl
      '#text': Regionaal Weer
  author: [email protected] (Webmaster KNMI)
- title: Waarschuwingen Flevoland
  description: Code Groen. Er zijn momenteel geen waarschuwingen van kracht.
  link: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/flevoland
  guid: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/flevoland
  pubDate: Sat, 11 Feb 2023 11:13:00 +0100
  category:
    - '@domain': https://www.knmi.nl
      '#text': Regional Weather
    - '@domain': https://www.knmi.nl
      '#text': Regionaal Weer
  author: [email protected] (Webmaster KNMI)
- title: Waarschuwingen Overijssel
  description: Code Groen. Er zijn momenteel geen waarschuwingen van kracht.
  link: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/overijssel
  guid: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/overijssel
  pubDate: Sat, 11 Feb 2023 11:13:00 +0100
  category:
    - '@domain': https://www.knmi.nl
      '#text': Regional Weather
    - '@domain': https://www.knmi.nl
      '#text': Regionaal Weer
  author: [email protected] (Webmaster KNMI)
- title: Waarschuwingen Gelderland
  description: Code Groen. Er zijn momenteel geen waarschuwingen van kracht.
  link: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/gelderland
  guid: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/gelderland
  pubDate: Sat, 11 Feb 2023 11:13:00 +0100
  category:
    - '@domain': https://www.knmi.nl
      '#text': Regional Weather
    - '@domain': https://www.knmi.nl
      '#text': Regionaal Weer
  author: [email protected] (Webmaster KNMI)
- title: Waarschuwingen Utrecht
  description: Code Groen. Er zijn momenteel geen waarschuwingen van kracht.
  link: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/utrecht
  guid: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/utrecht
  pubDate: Sat, 11 Feb 2023 11:13:00 +0100
  category:
    - '@domain': https://www.knmi.nl
      '#text': Regional Weather
    - '@domain': https://www.knmi.nl
      '#text': Regionaal Weer
  author: [email protected] (Webmaster KNMI)
- title: Waarschuwingen Zuid-Holland
  description: Code Groen. Er zijn momenteel geen waarschuwingen van kracht.
  link: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/zuid-holland
  guid: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/zuid-holland
  pubDate: Sat, 11 Feb 2023 11:13:00 +0100
  category:
    - '@domain': https://www.knmi.nl
      '#text': Regional Weather
    - '@domain': https://www.knmi.nl
      '#text': Regionaal Weer
  author: [email protected] (Webmaster KNMI)
- title: Waarschuwingen Zeeland
  description: Code Groen. Er zijn momenteel geen waarschuwingen van kracht.
  link: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/zeeland
  guid: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/zeeland
  pubDate: Sat, 11 Feb 2023 11:13:00 +0100
  category:
    - '@domain': https://www.knmi.nl
      '#text': Regional Weather
    - '@domain': https://www.knmi.nl
      '#text': Regionaal Weer
  author: [email protected] (Webmaster KNMI)
- title: Waarschuwingen Noord-Brabant
  description: Code Groen. Er zijn momenteel geen waarschuwingen van kracht.
  link: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/noord-brabant
  guid: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/noord-brabant
  pubDate: Sat, 11 Feb 2023 11:13:00 +0100
  category:
    - '@domain': https://www.knmi.nl
      '#text': Regional Weather
    - '@domain': https://www.knmi.nl
      '#text': Regionaal Weer
  author: [email protected] (Webmaster KNMI)
- title: Waarschuwingen Limburg
  description: Code Groen. Er zijn momenteel geen waarschuwingen van kracht.
  link: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/limburg
  guid: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/limburg
  pubDate: Sat, 11 Feb 2023 11:13:00 +0100
  category:
    - '@domain': https://www.knmi.nl
      '#text': Regional Weather
    - '@domain': https://www.knmi.nl
      '#text': Regionaal Weer
  author: [email protected] (Webmaster KNMI)
- title: Waarschuwingen Waddenzee
  description: Code Groen. Er zijn momenteel geen waarschuwingen van kracht.
  link: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/waddenzee
  guid: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/waddenzee
  pubDate: Sat, 11 Feb 2023 11:13:00 +0100
  category:
    - '@domain': https://www.knmi.nl
      '#text': Regional Weather
    - '@domain': https://www.knmi.nl
      '#text': Regionaal Weer
  author: [email protected] (Webmaster KNMI)
- title: Waarschuwingen IJsselmeergebied
  description: Code Groen. Er zijn momenteel geen waarschuwingen van kracht.
  link: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/ijsselmeergebied
  guid: https://www.knmi.nl/nederland-nu/weer/waarschuwingen/ijsselmeergebied
  pubDate: Sat, 11 Feb 2023 11:13:00 +0100
  category:
    - '@domain': https://www.knmi.nl
      '#text': Regional Weather
    - '@domain': https://www.knmi.nl
      '#text': Regionaal Weer
  author: [email protected] (Webmaster KNMI)

friendly_name: KNMI waarschuwingen
1 Like

separate post for a followup question:

consider the same resource url
https://cdn.knmi.nl/knmi/xml/rss/rss_KNMIwaarschuwingen.xml

(cant see that I Safari, but Chrome displays it nicely)

how can I select the description, (or any other attribute of this sensor)

      - name: KNMI waarschuwingen NB
        value_template: >
          {{now()}}
        json_attributes_path: "$.rss.channel.item[?(@.title=='Waarschuwingen Noord-Brabant')]"
        json_attributes:
          - title
          - description
          - link
          - guid
          - pubDate

to use in the value_template?

I did try something like {{value_json.rss.channel.item[?(@.title=='Waarschuwingen Noord-Brabant')].description}} but that errors on `unexpected char ’

same question for the ‘basis’ sensor, where I try things like:

{{value_json.rss.channel.item[0].description}}

and nothing is returned…

      - name: KNMI waarschuwingen basis
        value_template: >
          {{now()}}
        json_attributes_path: "$.rss.channel.item[0]"
        json_attributes:
          - title
          - category
          - description
          - enclosure
          - link

or, on the main sensor, there’s the first item:

item: 
- title: 'Code groen: Er zijn geen waarschuwingen'
ecetc

id like to check that for containing ‘Code groen’… and set a binary sensor on that. Code groen being safe, else being Unsafe…

btw, I did notice we Can actually use the value_template in a regular restful sensor:

sensor:
  - platform: rest
    unique_id: platform_rest_knmi_waarschuwingen
    resource: https://cdn.knmi.nl/knmi/xml/rss/rss_KNMIwaarschuwingen.xml
    name: Platform rest knmi waarschuwingen
    value_template: >
      {{'Groen' in value_json.rss.channel.item[12].description}}

returning True.

please have a look?
thx!

well, the KNMI api seems abandoned and no longer updates so the opening post sensor is no longer useful.

I did turn the Weerlive api into a better rest sensor if you like:

rest:

  - resource: !secret weerlive_rest_resource
    scan_interval: 86400

    binary_sensor:

      - unique_id: weerlive_api_response_alarm_binary_rest
        value_template: >
          {{value_json.liveweer[0].alarm}}
cant use attributes
        json_attributes_path: '$.liveweer[0]'
        json_attributes:
          - plaats
          - timestamp
          - time
          - alarmtxt
        device_class: safety

    sensor:

      - unique_id: weerlive_api_response_alarm_rest
        value_template: >
          {{value_json.liveweer[0].alarm}}
        json_attributes_path: '$.liveweer[0]'
        json_attributes:
          - plaats
          - timestamp
          - time
          - alarmtxt

      - unique_id: weerlive_api_response_actueel_rest
        value_template: >
          {{value_json.liveweer[0].samenv}}
        json_attributes_path: '$.liveweer[0]'
        json_attributes:
          - plaats
          - timestamp
          - time
          - temp
          - gtemp
          - samenv
          - lv
          - windr
          - windrgr
          - windms
          - winds
          - windk
          - windkmh
          - luchtd
          - ldmmhg
          - dauwp
          - zicht
          - verw
          - sup
          - sunder
          - image

      - unique_id: weerlive_api_response_d0_rest
        value_template: >
          {{value_json.liveweer[0].d0weer}}
        json_attributes_path: '$.liveweer[0]'
        json_attributes:
          - plaats
          - timestamp
          - time
          - d0weer
          - d0tmax
          - d0tmin
          - d0windk
          - d0windknp
          - d0windms
          - d0windkmh
          - d0windr
          - d0windrgr
          - d0neerslag
          - d0zon

      - unique_id: weerlive_api_response_d1_rest
        value_template: >
          {{value_json.liveweer[0].d1weer}}
        json_attributes_path: '$.liveweer[0]'
        json_attributes:
          - plaats
          - timestamp
          - time
          - d1tmax
          - d1tmin
          - d1windk
          - d1windknp
          - d1windms
          - d1windkmh
          - d1windr
          - d1windrgr
          - d1neerslag
          - d1zon

      - unique_id: weerlive_api_response_d2_rest
        value_template: >
          {{value_json.liveweer[0].d2weer}}
        json_attributes_path: '$.liveweer[0]'
        json_attributes:
          - plaats
          - timestamp
          - time
          - d2weer
          - d2tmax
          - d2tmin
          - d2windk
          - d2windknp
          - d2windms
          - d2windkmh
          - d2windr
          - d2windrgr
          - d2neerslag
          - d2zon

template:

  - binary_sensor:

      - unique_id: weerlive_alarm_binary
        state: >
          {{states('sensor.weerlive_alarm')}}
        attributes:
          alarm: >
            {{state_attr('sensor.weerlive_alarm','alarmtxt')}}
        device_class: safety

automation:

  - id: update_weerlive_knmi
    trigger:
      platform: time_pattern
      minutes: '/10'
    condition:
      condition: state
      entity_id:
        - binary_sensor.ha_restarted
        - binary_sensor.automation_integration_reloaded
        - binary_sensor.template_integration_reloaded
      state: 'off'
#       condition: numeric_state
#       entity_id: sensor.sun_solar_elevation
#       above: 10
    action:
      - service: homeassistant.update_entity
        target:
          entity_id:
  #           - sensor.weerlive_voorspelling_morgen
  #           - sensor.weerlive_voorspelling_vandaag
  #           - sensor.weerlive_actueel
            - sensor.weerlive_alarm

this is the same api the KNMI integration by @golles uses, so prepare to check the update frequency to remain within the api limit, or better yet, apply for a separate api key so the 2 dont bite each other