Met office - trying to create a sensor for current max daily temperature

I’ve got the Met Office integration working fine and want to create a sensor to examines the current daily max temperature but struggling.

I’ve created the below sensor on my template.yaml file and it creates the sensor but no data just unavailable.

- sensor:
  - name: "Met Office Daily High Temperature"
    unique_id: '66b8c2a0-b401-4d6e-8f04-81d6efbff8120'
    unit_of_measurement: "°C"
    state: "{{ state_attr('weather.met_office_cheltenham_racecourse', 'forecast')[0]['temperature'] }}"
    attributes:
      friendly_name: "Daily High Temperature"      

Any help would be appreciated as I’m not a confident coder.

The forecast attribute was removed from the state object of weather entities about 18 month ago, there is no way to access it directly with a state template. You will need to follow the example in the docs showing how to create a template sensor using the action weather.get_forecasts.

1 Like

@123 so I’ve followed that example and replaced weather.home with weather.met_office_cheltenham_racecourse

Currently the new sensor shows as unknown

I just tested it on my system and it reported the maximum forecasted temperature for my area.

Odd,

I tried this approach and it’s working now

- trigger:
  - platform: state
    entity_id: weather.met_office_cheltenham_racecourse
  action:
    - variables:
        w: weather.met_office_cheltenham_racecourse
    - service: weather.get_forecasts
      data:
        type: daily
      target:
        entity_id: '{{ w }}'
      response_variable: r
  sensor:
    - name: 'Met Office Daily Max Temperature '
      unique_id: 'met_office_daily_max_temperature'
      unit_of_measurement: '°C'
      device_class: temperature
      state: "{{ r[w].forecast[0].temperature }}"

I thought you had asked for the maximum forecasted temperature. Your modified version, of my example’s template, does not do that.

EDIT:

I see you changed the value of type from hourly to daily.

Very odd indeed because the example I offered does work.

BTW, the reason my template excludes strings (using reject('string')) is because there are rare occasions when met.no doesn’t report a numeric value for temperature.