MQTT Publish Weather Minimum and Maximum Temperature

Hi guys,

I want to send via mqtt.publish automation the minimum and maximum temperature for today, and week.

I tried using the code mentioned in the thread by @vingerha: Extracting values from weather.forecast_home

It does show the maximum but not the minimum temperature

The user @Troon said this no longer applies and I should open a new post

Here is my automation YAML code:

service: mqtt.publish
data:
  topic: homeassistant/sensor/IntelkaSW-8e8c-3/datetime
  payload: |-
    {
      "datetime": "{{ now().timestamp() | timestamp_custom("%Y-%d-%mT%H:%M:%S%z") }}",
      "weather": "{{ states('weather.forecast_home') }}",
      "temperature": "{{ state_attr('weather.forecast_home','temperature') }}",
      "humidity": "{{ state_attr('weather.forecast_home','humidity') }}",
      "max_temperature": "{{ state_attr('weather.forecast_home','forecast') | selectattr('datetime', 'search', now().timestamp() | timestamp_custom('%Y-%m-%d')) | map(attribute='temperature') | max }}",
      "min_temperature": "{{ state_attr('weather.forecast_home','forecast') | selectattr('datetime', 'search', now().timestamp() | timestamp_custom('%Y-%m-%d')) | map(attribute='temperature') | min }}"
    }

Output:

homeassistant/sensor/IntelkaSW-8e8c-3
{
  "datetime": "2024-26-11T10:50:00+0100",
  "weather": "rainy",
  "temperature": "7.9",
  "humidity": "77",
  "max_temperature": 8.3
  "min_temperature": 8.3
}

Please let me know why minimum is not working and what should I do to fix this in my automation YAML code.
Thanks and regards
Andree

I said that the old code in that thread no longer applies, as the method of getting forecast data has changed.

Please put this into Developer Tools / Template and paste the full result here:

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

Hi @Troon,

I updated my HomeAssistant to the latest version:

  • Core - 2024.11.3
  • Supervisor - 2024.11.4
  • Frontend - 20241106.2

This made my code not work at all now. (All information coming from forecast)

After that I went to Developer Tools and pasted the command you shared and got null

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

Result

Result type: dict

null

This template listens for the following state changed events:

  • Entity: weather.forecast_home

Thanks in advance and appreciate your help
Kindly, please advise.
Andree

Take a look at the first example here:

That should create a template sensor that contains the forecast data from your weather provider. You can then use that to work out the lowest temperatures.

Hi @Troon ,

I tried the code mentioned in the developer tools templates section, my entity is weather.forecast_home

Here the adjusted code:

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

but got as result:

UndefinedError: 'hourly' is undefined

Not sure if I’m creating the template in the correct place. What am I missing?

Kindly, please advise
Thanks and regards
Andree

You’re not. The template editor is merely for trying out Jinja templates.

This is YAML configuration and needs to be in configuration.yaml. I realise it starts with template: but this code is not a template in itself. Only the bit in {{...}} is a template.

Please add a couple of lines to it, so you get this at the end:

    sensor:
      - name: Temperature forecast next hour
        unique_id: temperature_forecast_next_hour
        state: "{{ hourly['weather.forecast_home'].forecast[0].temperature }}"
        unit_of_measurement: °C
        attributes:
          forecast: ""{{ hourly['weather.forecast_home'].forecast }}"

That way, the sensor will have the current temperature as its state, and the whole forecast in an attribute.

Hi @Troon ,

Okay, I moved this change to my configuration.yaml file and added the lines you mentioned at the bottom. Here’s the updated code snippet

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

I left it running for a couple of hours (since it runs once an hour) and when I looked at the Developer Tools → States → Entity : sensor.temperature_forecast_next_hour
The State shows as unknown and attributes as unit_of_measurement: °C friendly_name: Temperature forecast next hour

But cannot see any of the attributes for forecasts. Not sure what may be causing issues now.

Kindly, please advise
Thanks and regards
Andree

Do you have a weather provider set up on your system? This feels like you’re just guessing what to do without understanding what’s happening.

You must set up a weather integration first, which is what the action: is calling. Here are your options:

Then replace weather.forecast_home with the actual entity ID of your weather entity.