Environment Canada integration - 2021.11 onwards

Hi, There will be a change in early 2024 to get the forecasts. Weather.xxxx entities will no longer provide forecasts automatically, you must request them. Here’s how I do it.

template: 
          
  - trigger:
      - platform: time_pattern
        hours: /1
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.shawinigan
        response_variable: previsionshoraires
    sensor:
      - name: Prévisions météorologiques horaires
        unique_id: previsions_meteorologiques_horaires
        state: "{{ now().isoformat() }}"
        icon: mdi:hours-24
        attributes:
          forecast: "{{ previsionshoraires.forecast }}"

This will create a sensor under the name sensor.previsions_meteorologiques_horaires which will contain the forecasts.

Then you can get the forecasts like this.

template:
  - sensor:

    - name: 'def_previsions_temperature_h0'
      unique_id: 'def_previsions_temperature_h0'
      unit_of_measurement: "°C"
      state: "{{ state_attr('sensor.previsions_meteorologiques_horaires', 'forecast')[0].temperature }}"
      
    - name: 'def_previsions_temperature_h1'
      unique_id: 'def_previsions_temperature_h1'
      unit_of_measurement: "°C"
      state: "{{ state_attr('sensor.previsions_meteorologiques_horaires', 'forecast')[1].temperature }}"
  
    - name: 'def_previsions_temperature_h2'
      unique_id: 'def_previsions_temperature_h2'
      unit_of_measurement: "°C"
      state: "{{ state_attr('sensor.previsions_meteorologiques_horaires', 'forecast')[2].temperature }}"
  
    - name: 'def_previsions_temperature_h3'
      unique_id: 'def_previsions_temperature_h3'
      unit_of_measurement: "°C"
      state: "{{ state_attr('sensor.previsions_meteorologiques_horaires', 'forecast')[3].temperature }}"
  
    - name: 'def_previsions_temperature_h4'
      unique_id: 'def_previsions_temperature_h4'
      unit_of_measurement: "°C"
      state: "{{ state_attr('sensor.previsions_meteorologiques_horaires', 'forecast')[4].temperature }}"

    - name: 'def_previsions_temperature_h5'
      unique_id: 'def_previsions_temperature_h5'
      unit_of_measurement: "°C"
      state: "{{ state_attr('sensor.previsions_meteorologiques_horaires', 'forecast')[5].temperature }}"
      

Tried to extract the hourly weather forecast with your code above @ValMarDav. I could not make it work. I believe there is a difference in the data contained depending on the weather station I use (Sherbrooke).

Here is what I believe is the entity from Environment Canada that contains the best forecast information.

I placed the following code into the “template” section of my configuration.yaml.

  • trigger:
    • platform: time_pattern
      hours: /1
      action:
    • service: weather.get_forecasts
      data:
      type: hourly
      target:
      entity_id: weather.sherbrooke_previsions
      response_variable: previsionshoraires
      sensor:
    • name: Prévisions météorologiques horaires
      unique_id: previsions_meteorologiques_horaires
      state: “{{ now().isoformat() }}”
      icon: mdi:hours-24
      attributes:
      forecast: “{{ previsionshoraires.forecast }}”

It creates the entity but it does not contain the forecast, see below:

I believe the issue lies within the data that is available from Environment Canada. I don’t see the hourly forecast, so I can’t extract it.

Does someone have a clue how to get the hourly temperature forecast out?

Hi, What I published 6 days ago, no longer works because they have again changed the response of the service in the publication of core 2023.12. Now the result contains the name of the Weather entity because you can specify several in the same query. So to read the forecasts, you must specify which weather entity you want to read from. Here is the new way to create the template.


template:

# Obtenir les pévisions horaires
          
  - trigger:
      - platform: time_pattern
        hours: /1
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.shawinigan
        response_variable: previsionshoraires
    sensor:
      - name: Prévisions météorologiques horaires
        unique_id: previsions_meteorologiques_horaires
        state: "{{ now().isoformat() }}"
        icon: mdi:hours-24
        attributes:
          forecast: "{{ previsionshoraires['weather.shawinigan'].forecast }}" 

1 Like

Hello,

It does work!

Thank you/merci.

My gosh, if they change the way the data is provided so often, how are you keeping track of this?

Does the integration provide a way to extract the full warning details:

As it only seem to provide the title/summary:

I don’t mind polling the data manually/externally and create a sensor but just want to check if someone figured out a way to achieve this before reinventing the wheel.

Thx!

I don’t think this is built in. I did notice that there was an RSS feed link provide by environment Canada on the bottom of the warning (not shown in your picture). I am not sure if the HA RSS feed reader would create a sensor for this. I will try at my next opportunity.

Thanks, will try with the RSS feed. Will report back if positive.
Cheers

The integration used to pull the full alert data from the ECCC website using HTML scraping, since it’s not included in the XML data. However, HA took a hard stance against HTML scraping so I had to remove this functionality. I haven’t looked at this in a while, so if anyone has any suggestions I’m all ears!

I appreciate that scraping is no longer a part of the integration, but scraping is still an available option with HA. I am aware that EC could change their website at any time and thus break this scraping…But do you have any details on how we could scrape the website ourselves based on the Scrape integration that exists? No warranty is expected or implied by anything you can provide.

The code I was using to get the HTML is here:

This was quite a while ago, but I don’t think they’ve changed the site much.

I cannot figure out how to turn on hourly forecasts again, I just had a SD card die and I am rebuilding. Hourly forecasts, were those lost when scraping was banned?

Is it possible to put the full version of Environment Canada addon into HACS with all it’s webscraping glory?

@michaeldavie, I may have mentioned this before but would it be possible to have the summary added to an attribute so that it is not truncated by the 255 character limit?

The limit is imposed by Home Assistant. The max length of a field is 255.

This limit does not exist for sensor attributes, so the idea would be to move the summary to an attribute field to overcome the 255 character limit.