Need help - define sensor in configuration.yaml

Hi All,

I’m relatively new to HA. I’m using HAOS on a RPI 4 8GB. I have met.no as an integration. Met.no forecasts the hourly temperature for the coming 24hrs.

I want to define two sensors: One that gives the highest temperature of the coming 24hrs, another one with the time of that highest temperature. Based on that sensor I will start my heatpump: The higher the outside temperature, the more efficient the heatpump will operate.

I use Studio Code Server to edit the code.

I’ve been hassling for days now with the code but can’t get it to work. I’m afraid that I’m making some mistakes with the indentation, or with the way I define things. I’ve tried lots of different indentation options but nothing works.

Does anybody have some hints tips?

The forecast of met.no is in weather.forecast_home_hourly.

I’ve added the following to the bottom of my configuration.yaml:

- sensor:
  - name: "Max Forecast Temperature in Next Day"
    unique_id: max_forecast_temperature_in_next_day
    state: >
        {{ state_attr('weather.forecast_home_hourly','forecast')[:] | map(attribute='temperature')|max }}
    attributes:
      state_class: measurement
      device_class: temperature
      unit_of_measurement: °C
      icon: mdi:thermometer

- sensor:
  - name: "Max Forecast Temperature Hour in Next Day"
    unique_id: max_forecast_temperature_hour_in_next_day
    state: >
        {% set max_temp = state_attr('weather.forecast_home_hourly','forecast')[:] | map(attribute='temperature')|max %}
        {% set max_index = (state_attr('weather.forecast_home_hourly','forecast')[:] | map(attribute='temperature')|list).index(max_temp) %}
        {% set warmest_hour = as_timestamp(state_attr('weather.home_hourly','forecast')[max_index]['datetime']) | timestamp_custom("%H") %}
        {{warmest_hour}}
    attributes:
      icon: mdi:hours-24

Thanks in advance for your kind answer.

You need template: at the start. I have removed the attribute: and reset some of your indentations:

template:
  - sensor:
      - name: "Max Forecast Temperature in Next Day"
        unique_id: max_forecast_temperature_in_next_day
        state: >
          {{ state_attr('weather.forecast_home_hourly','forecast')[:] | map(attribute='temperature')|max }}
        state_class: measurement
        device_class: temperature
        unit_of_measurement: °C
        icon: mdi:thermometer

  - sensor:
      - name: "Max Forecast Temperature Hour in Next Day"
        unique_id: max_forecast_temperature_hour_in_next_day
        state: >
          {% set max_temp = state_attr('weather.forecast_home_hourly','forecast')[:] | map(attribute='temperature')|max %}
          {% set max_index = (state_attr('weather.forecast_home_hourly','forecast')[:] | map(attribute='temperature')|list).index(max_temp) %}
          {% set warmest_hour = as_timestamp(state_attr('weather.home_hourly','forecast')[max_index]['datetime']) | timestamp_custom("%H") %}
          {{warmest_hour}}
        icon: mdi:hours-24

edit: here is the doc page:

1 Like