#Get forecast from Met.no

1 messages · Page 1 of 1 (latest)

tiny moth
#

Hello, Im new to HA, im hosting it using HA OS on RPi4 4GB.

I use weather integration from Met.no

It looks like this, as you can see it has daily forecasts, even hourly (attachment).

I need to get max temperature from next day (for my irrigation system), so I went to Dev Tools --> States and it contains only these attributes:

temperature: 23.4
dew_point: 12.3
temperature_unit: °C
humidity: 50
cloud_coverage: 0
uv_index: 0
pressure: 1012.1
pressure_unit: hPa
wind_bearing: 93.4
wind_speed: 1.19
wind_speed_unit: m/s
visibility_unit: km
precipitation_unit: mm
attribution: Weather forecast from met.no, delivered by the Norwegian Meteorological Institute.
friendly_name: Forecast Home
supported_features: 3

Even when I go to Dev Tools --> Template and try to insert this:

{{ state_attr('weather.forecast_home', 'get_forecasts') }}

{{ state_attr('weather.forecast_home', 'forecast') }}

{% set forecast = state_attr('weather.forecast_home', 'forecast') %}
{% if forecast %}
  {{ forecast[0].temperature }}
{% else %}
  No forecast available
{% endif %}

I get:

None

None

No forecast available

So any help please to get forecast from Met.no integration?

jade yoke
#

You have to fetch it. I have this in my configuration.yaml:

template:
  - trigger:
      - trigger: time_pattern
        hours: /1
    action:
      - action: weather.get_forecasts
        data:
          type: daily
        target:
          entity_id: weather.forecast_home
        response_variable: daily
    sensor:
      - name: Temperature forecast tomorrow
        unique_id: temperature_forecast_tomorrow
        state: "{{ daily['weather.forecast_home'].forecast[1].temperature }}"
        device_class: temperature
        unit_of_measurement: °C

And then I can easily get it as sensor.temperature_forecast_tomorrow entity.

tiny moth
jade yoke
#

Yes you will, try it out yourself

#

Why it works this way is somewhat deep. Some (maybe most) weather implementations don't provide forecast attribute but you must use weather.get_forecast service instead

#

(I think)

tiny moth
#

So I created automation get weather forecast as you can see on screenshot, but then I went to dev tools states and i dont see values, but when I go to actions I get forecast as you, but I need to "save" it somehow so my integration scripts have access to it

jade yoke
#

Maybe it's not triggered yet?

tiny moth
#

I manually started it

jade yoke
#

Also, are you sure you can set weather.* like that. I used separate sensor for some reason - it's been a while, I think I just followed the docs

jade yoke
#

oh right, I mis-read you were trying to set weather.forecast_home with new data, sorry

tiny moth
#

hmm im missing sensor

#

maybe sensor saves state?

jade yoke
#

quite possible!

#

Also, if for some reason you end up needing a icon for tomorrow's weather condition, you can use this - I spent some time gathering it:

type: custom:button-card
entity: sensor.condition_forecast_tomorrow
show_state: false
show_name: false
show_icon: true
icon: |
  [[[
    if (entity.state == "clear-day")
      return "mdi:weather-sunny";
    else if (entity.state == "sunny")
      return "mdi:weather-sunny";
    else if (entity.state == "clear")
      return "mdi:weather-sunny";
    else if (entity.state == "clear-night")
      return "mdi:weather-night";
    else if (entity.state == "rainy")
      return "mdi:weather-rainy";
    else if (entity.state == "pouring")
      return "mdi:weather-pouring";
    else if (entity.state == "snow")
      return "mdi:weather-snowy";
    else if (entity.state == "fog")
      return "mdi:weather-fog";
    else if (entity.state == "sleet")
      return "mdi:weather-partly-snowy-rainy";
    else if (entity.state == "wind")
      return "mdi:weather-windy";
    else if (entity.state == "cloudy")
      return "mdi:weather-cloudy";
    else if (entity.state == "partlycloudy")
      return "mdi:weather-partly-cloudy";
    else if (entity.state == "partly-cloudy-night")
      return "mdi:weather-night-partly-cloudy";
    else if (entity.state == "hail")
      return "mdi:weather-hail";
    else if (entity.state == "lightning")
      return "mdi:weather-lightning";
    else if (entity.state == "tstorm")
      return "mdi:weather-lightning-rainy";
    else
      return "mdi:cloud-question-outline";
  ]]]
tiny moth
#

ah thank you very much 😄

#

btw I see it has precipitation but not precipitation_probability shame

tiny moth
#

idk what im doing wrong 😄

clear lynx
#

As you have realised you need to use 'weather.get_forecasts' action. This retireves the data, you are using response_variable to give a temporary name to this data. You then use this in the sensor defintion to retrieve the value. This all looks fine since you are setting 'Template forecast next hour' from the hourly response variable.

You then appear to be trying to read the forecast data in the dev tools -> template, but this won't exist since the response_variable is not saved. Instead you should use the entity id ('sensor.temperature_forecast_next_hour') which you created as a template.

jade yoke
#

did you figure it out?

tiny moth
#

is possible to see somewhere in HA all sensors and its values? 😄

jade yoke
#

Yes, of course: Developer tools -> States

clear lynx
# tiny moth then I get sensor is undefined 😄

Why are you trying {{ sensor.temperature_forecast_tomorrow }} ? Have you defined this sensor anywhere? In the configuration.yaml you posted a screen shot off you are defining {{ sensor.temperature_forecast_next_hour }}

tiny moth
clear lynx
tiny moth
# clear lynx I'm not going to type out the whole code since you posted screen shots, but that...

this is code:

template:
  - sensor:
      - name: "Temperature forecast today"
        unique_id: temperature_forecast_today
        state: >
          {{ state_attr('weather.forecast_home', 'forecast')[0].temperature }}
        unit_of_measurement: "°C"
        device_class: temperature

      - name: "Temperature forecast tomorrow"
        unique_id: temperature_forecast_tomorrow
        state: >
          {{ state_attr('weather.forecast_home', 'forecast')[1].temperature }}
        unit_of_measurement: "°C"
        device_class: temperature
        
  - trigger:
      - trigger: time_pattern
        hours: /1
    action:
      - action: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.home
        response_variable: hourly
    sensor:
      - name: Temperature forecast next hour
        unique_id: temperature_forecast_next_hour
        state: "{{ hourly['weather.home'].forecast[0].temperature }}"
        unit_of_measurement: °C

as you can see I have unique_id "temperature_forecast_tomorrow" so why I cannot use sensor.temperature_forecast_tomorrow? 🥺

#

Okay I made changes... this is my automation:

alias: Get weather forecast
description: ""
triggers:
  - trigger: time_pattern
    hours: /1
conditions: []
actions:
  - action: weather.get_forecasts
    metadata: {}
    data:
      type: daily
    target:
      entity_id: weather.forecast_home
    response_variable: daily
mode: single

and this is my sensor script inside configurations.yaml:

template:
  - sensor:
      - name: "Temperature forecast today"
        unique_id: temperature_forecast_today
        state: "{{ daily['weather.forecast_home'].forecast[0].temperature }}"
        unit_of_measurement: "°C"
        device_class: temperature

      - name: "Temperature forecast tomorrow"
        unique_id: temperature_forecast_tomorrow
        state: "{{ daily['weather.forecast_home'].forecast[0].temperature }}"
        unit_of_measurement: "°C"
        device_class: temperature

and still cannot access sensor object inside template: {{ sensor.temperature_forecast_tomorrow }}

jade yoke
#

You only need template -> trigger, remove template -> sensor entries. Add second trigger, or a second sensor to existing trigger, double check indentation, and then restart.

clear lynx
#

you are confusing things. You do not need to create an automation as you should be using a trigger based sensor.

Since you posted your code as text, I have corrected it for you below with the following changes:

1). I added homeassistant start event to the triggers, so that it will retrieve values at startup.
2). I added the 'weather.get_forecasts' 'daily' action - this gets daily forecasts
3). I modifed the code so that the three sensors are created correctly
4). I store the forecast for today and tomorrow in attributes whilst setting the state to be the condition. This is due to size limits of the state.
5). I changed the name of the Today and Tomorrow sensors to match the fact that I'm storing the whole forecast

template:
  - trigger:
      - platform: homeassistant
        event: start
      - trigger: time_pattern
        hours: /1
    action:
      - action: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.home
        response_variable: hourly
      - action: weather.get_forecasts
        data:
          type: daily
        target:
          entity_id: weather.home
        response_variable: daily
    sensor:
      - name: Temperature forecast next hour
        unique_id: temperature_forecast_next_hour
        state: "{{ hourly['weather.home'].forecast[0].temperature }}"
        unit_of_measurement: °C

      - name: "Weather Forecast today"
        unique_id: weather_forecast_today
        state: >
          {{ "{{ daily['weather.home'].forecast[0].condition }}"
        attributes:
          forecast: >
            {{ daily[provider].forecast[0] }}

      - name: "Weather Forecast tomorrow"
        unique_id: weather_forecast_tomorrow
        state: >
          {{ "{{ daily['weather.home'].forecast[1].condition }}"
        attributes:
          forecast: >
            {{ daily[provider].forecast[1] }}
        
tiny moth
#

thank you, I changed it little bit:

template:
  - trigger:
      - platform: homeassistant
        event: start
      - trigger: time_pattern
        hours: "/1"
    action:
      - action: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.forecast_home
        response_variable: hourly
      - action: weather.get_forecasts
        data:
          type: daily
        target:
          entity_id: weather.forecast_home
        response_variable: daily
    sensor:
      - name: "Temperature forecast next hour"
        unique_id: temperature_forecast_next_hour
        state: "{{ hourly['weather.forecast_home'].forecast[0].temperature }}"
        unit_of_measurement: "°C"

      - name: "Weather Forecast today"
        unique_id: weather_forecast_today
        state: "{{ daily['weather.forecast_home'].forecast[0].condition }}"
        attributes:
          forecast: "{{ daily['weather.forecast_home'].forecast[0] }}"

      - name: "Weather Forecast tomorrow"
        unique_id: weather_forecast_tomorrow
        state: "{{ daily['weather.forecast_home'].forecast[1].condition }}"
        attributes:
          forecast: "{{ daily['weather.forecast_home'].forecast[1] }}"

but how can I access it in template / code? with {{ state_attr('sensor.weather_forecast_today', 'temperature') }} I get None

clear lynx
#
{% if state_attr('sensor.weather_forecast_today', 'forecast') is defined
  and state_attr('sensor.weather_forecast_today', 'forecast').temperature is defined
  and is_number(state_attr('sensor.weather_forecast_today', 'forecast')['temperature']) -%}
  {{ state_attr('sensor.weather_forecast_today', 'forecast')['temperature'] | int(-1) }}
{%- endif %}
#

note: I convert to int(-1) so if it doesn't exist you get -1. You may want to use float if you want to keep the decimal.

tiny moth