Stuck with template and get_forecasts

Hi all,

I hope to be able to get some help in here. I use the met.no nowcast integration available on HACS. GitHub - toringer/home-assistant-metnowcast: Met.no Nowcast component for Home Assistant

When using the new weather get_forecasts I get a response back that contains datetime objects and I am having trouble figuring out how to handle them in templates.

To test I have set up a service test in the developer tools:

  service: weather.get_forecasts
    target:
      entity_id: weather.met_no_nowcast_neshamn
    data:
      type: hourly
    response_variable: wf

This returns the response (truncated below) which is exactly what I would like:

    weather.met_no_nowcast_neshamn:
      forecast:
        - temperature: 10.8
          precipitation: 0
          relative_humidity: 68
          wind_bearing: 153.4
          wind_speed: 8.28
          wind_speed_of_gust: 6
          datetime: "2024-04-09T08:45:00+00:00"
          condition: cloudy
        - temperature: null
          precipitation: 0
          datetime: "2024-04-09T08:50:00+00:00"
        - temperature: null
          precipitation: 0
          datetime: "2024-04-09T08:55:00+00:00"
        - temperature: null
          precipitation: 0
          datetime: "2024-04-09T09:00:00+00:00"

However when I run the same service from a template I get a response that looks like this containing datetime objects and not fully stringified json:

    forecast: >-
      [{'temperature': 10.7, 'precipitation': 0.0, 'relative_humidity': 66.2,
      'wind_bearing': 156.4, 'wind_speed': 6.48, 'wind_speed_of_gust': 5.3,
      'datetime': datetime.datetime(2024, 4, 9, 9, 35,
      tzinfo=datetime.timezone.utc), 'condition': 'cloudy'}, {'temperature': None,
      'precipitation': 0.0, 'datetime': datetime.datetime(2024, 4, 9, 9, 40,
      tzinfo=datetime.timezone.utc)}, {'temperature': None, 'precipitation': 0.0,
      'datetime': datetime.datetime(2024, 4, 9, 9, 45,
      tzinfo=datetime.timezone.utc)}, {'temperature': None, 'precipitation': 0.0,
      'datetime': datetime.datetime(2024, 4, 9, 9, 50,
      tzinfo=datetime.timezone.utc)}, {'temperature': None, 'precipitation': 0.0,
      'datetime': datetime.datetime(2024, 4, 9, 9, 55,
      tzinfo=datetime.timezone.utc)}, {'temperature': None, 'precipitation': 0.0,

Any ideas how I can convert this attribute to something more manageable? At the moment I cannot call from_json because the datetime objects are there.

Somehow the developer tools was able to correctly convert the response to something sane.

Not sure what you mean: you can’t run a service from a template.

Have a look at the examples here:

The custom weather integration you using is reporting dates as datetime objects instead of datetime strings.

Contact the custom integration’s author and report the problem.

This is the template that I am using (in configuration.yaml):

    template:
      - trigger:
          - platform: time_pattern
            minutes: /5
        action:
          - service: weather.get_forecasts
            target:
              entity_id: weather.home
            data:
              type: hourly
            response_variable: weather_home_var
        sensor:
          - name: weather_home
            unique_id: weather_home
            state: "{{ now().isoformat() }}"
            attributes:
              forecast: "{{ weather_home_var['weather.home'].forecast  }}"

The nowcast weather service returns a response that contains datetime objects. That is not necessarily a bug as home assistant manages perfectly well in the developer console (see example above).

I need to do something else to {{ weather_home_var['weather.home'].forecast }} before it can be piped to from_json, I cant figure out what.

you can do…

              forecast: >
                [{%- for n in weather_home_var['weather.home'].forecast  %}
                   {
                   {%- for k, v in n.items() %}
                     {%- if v is datetime %}
                     "{{ k }}": "{{ v.isoformat() }}",
                     {%- elif v is string %}
                     "{{ k }}": "{{ v }}",
                     {%- else %}
                     "{{ k }}": {{ v }},
                     {%- endif %}
                   {%- endfor %}
                   },
                 {%- endfor %}]
1 Like

Brilliant, thanks @petro

Here’s proof :slight_smile:

image