Getting the most likely weather forecast for the next 4 hours

I have been struggling all day to achieve a simple thing, several AIs could not help me fix it.

I am trying to create a sensor that shows me the most likely weather forecast for the next 4 hours. I just want to grab the forecasts and determine which element appears most often.

So for example:
[‘sunny’, ‘sunny’, ‘sunny’, ‘rainy’] should result in ‘sunny’
[‘sunny’, ‘sunny’, ‘rainy’, ‘rainy’] should result in ‘inconclusive’
I think this transports my point, if one condition occurs more often than all the others, that one wins, otherwise its inconclusive.

I was successful in extracting this array with 4 strings, but was not able to implement the remaining functionality. Any help is appreciated. Here is how I did it:

template:
  - trigger:
      - platform: state
        entity_id: weather.forecast_home
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.forecast_home
        response_variable: hourly
    sensor:
      - name: Local Weather Forecast Hourly
        unique_id: local_weather_forecast_hourly
        state: "{{ now() }}"
        attributes:
          forecast: "{{ hourly['weather.forecast_home'].forecast }}"
  - sensor:
    - name: "Most Likely Weather Next 4 Hours"
      unique_id: most_likely_weather_next_4_hours
      state: >
          {% set forecast_list = state_attr('sensor.local_weather_forecast_hourly', 'forecast') | default([]) %}
          {% set conditions = forecast_list | map(attribute='condition') | list %}
          {% if conditions | length > 4 %}
            {% set conditions = conditions[:4] %}
          {% endif %}
          {% set condition_counts = {} %}
          {% for condition in conditions %}
            {% set condition_counts = condition_counts | merge({ condition: (condition_counts[condition] | default(0)) + 1 }) %}
          {% endfor %}
          {% set most_likely_condition = condition_counts | dictsort(true, 'value') | first %}
          {{ most_likely_condition[0] }}

This fails: TemplateAssertionError: No filter named ‘merge’. All other attempts to create this dictionary also failed.

There is no filter named merge in jinja or HA template extensions that I can find, not sure where you go that from. Closest thing I could find is merge_response() documented here, and it does not look like it can be used as a filter.

{% set forecast_list = state_attr('sensor.local_weather_forecast_hourly', 'forecast') | default([]) %}
{% set conditions = (forecast_list | map(attribute='condition') | list)[:4] %}
{%- set u_cond = conditions | unique | list %}
{%- set u_count = u_cond | count %}
{%- if u_count == 4 %}
  inconclusive
{%- elif u_count == 1 %}
  {{ u_cond[0] }}
{%- else %}
  {%- set ns = namespace(d=[]) %}
  {%- for c in u_cond %}
    {%- set ns.d = ns.d + [(c, conditions|select('eq', c)| list | count)] %}
  {%- endfor %}
  {% set c_comp = (ns.d)|map(attribute=1)|unique|list|count %}
  {{ (ns.d | max(attribute=1))[0] if c_comp != 1 else "inconclusive"}}
{%- endif %}

Thank you, that works perfectly! Did not know I needed the namespace construct to work with the dictionary inside a loop