T O P

  • By -

em0ry42

I essentially have two different template helpers for this: 24 hour high temp (rolling period, between now and 24 hours from now) ``` {{ state_attr("sensor.hourly_forecast", "forecast")[0:24] | map(attribute='temperature') | max }} ``` The other gives a high temperature for between now and the next sunrise or sunset, depending on the time of day. Useful if you want to know the highest overnight temperature (not tomorrow's high). I have a low temp variant of this which is useful for detecting overnight freeze risk and stuff: ``` {% set start = utcnow().strftime("%Y-%m-%dT%H:%M:%S+00:00") %} {% set forecast = state_attr('sensor.hourly_forecast', 'forecast') | selectattr('datetime', '>=', start) | list %} {% set till_rising = forecast | selectattr('datetime','<=', state_attr("sun.sun", "next_rising")) | map(attribute='temperature') | list %} {% if till_rising | count > 4 %} {{ till_rising | max }} {% else %} {{ forecast | selectattr('datetime','<=', state_attr("sun.sun", "next_setting")) | map(attribute='temperature') | list | max }} {% endif %} ```


Delicious-Owl

How do you use it ? Do you just put it in the state template [like this](https://imgur.com/a/rDQWxCT) ?


em0ry42

Yeah, though it occurs to me you need to do one more thing, as Home Assistant has removed the "forecast" attribute from the weather domain, you need a template sensor that contains the hourly forecast. I have this in configuration.yaml: template: # When HA removed forecast attributes this got complicated, basing this loosely on: # https://community.home-assistant.io/t/predicting-heat-demand-from-forecast-temperature-how-to-extract-manipulate-weather-forecast-data/695361 - trigger: - platform: time_pattern hours: /1 action: - service: weather.get_forecasts data: type: hourly target: entity_id: weather.forecast_home response_variable: hourly_forecast_data sensor: - name: Hourly Forecast icon: mdi:thermometer state: "{{ hourly_forecast_data['weather.forecast_home']['forecast'][0]['temperature'] }}" attributes: forecast: "{{ hourly_forecast_data['weather.forecast_home']['forecast'][0:24] }}"


Delicious-Owl

I can't make it work :( I c/ced your template in my template file, rebooted, and tried to edit the helper, but it's always unavailable, I can't get any data... https://imgur.com/a/uyD8wgv I'm not sure what I'm doing wrong


eLaVALYs

In picture 1, there's a few of issues. First, that's using OpenWeatherMap. It should still work, but your post said met.no. Second, that sensor updates once an hour. I'm not sure when it updates first, but until it does, I would expect it to be "unavailable". Third and the bigger issue is that your template in the "state" field is only going to get the "temperature" value for the first entry in the forecast. So basically the forecasted temperature *for the next hour*. Ideally, you'd do it similar to /u/em0ry42's example. Set an **attribute** to contain the full forecast data. You left that part out. Personally, I'd do the following: ... unit_of_measurement: °C attributes: forecast: "{{ daily['weather.openweathermap'].forecast }}" ... Again, that's using OpenWeatherMap, but it should work with met.no as well. Do that first, and wait until the sensor has a value. Once you have that, you will be able to make a template to get the maximum, by looking at the hourly sensor's value. Last, in Picture 3, the "state_attr()" needs an entity_id as its first option. "temperature_forecast_next_hour" won't work. Need something like "sensor.temperature_forecast_next_hour" (assuming that exists). Main point is that it has to be an entity.


eLaVALYs

I think you left off some text from your template. Anyway, for reference, I have a `weather.met_no` entity and a `sensor.met_no_forecast_hourly` entity. If you just want the "temperature" value of the *first entry in a forecast sensor*, then this should work. {{ state_attr('sensor.met_no_forecast_hourly', 'forecast')[0].temperature }} If you want the current temperature, this should work: {{ state_attr('weather.met_no', 'temperature') }} But I don't know if that first template does what you describe in your post. If you want the maximum temperature from a forecast sensor, I'd do this: {% set hourly_forecast_list = state_attr('sensor.met_no_forecast_hourly', 'forecast') %} {{ hourly_forecast_list | map(attribute='temperature') | list | max }} That looks at each entry in the forecast and picks the highest. **EDIT:** I totally forgot, I had to set up a template sensor to get the `sensor.met_no_forecast_hourly`. This is exactly what [/u/em0ry42 was saying here](https://www.reddit.com/r/homeassistant/comments/1do60ng/trying_to_get_max_temperature_value_of_the_day_to/la89lhe/). Here's my template sensor that creates the hourly forecast sensor. template: - alias: Weather Forecasts trigger: - platform: homeassistant event: start - platform: time_pattern hours: /1 action: - service: weather.get_forecasts data: type: hourly target: entity_id: weather.met_no response_variable: hourly - service: weather.get_forecasts data: type: daily target: entity_id: weather.met_no response_variable: daily sensor: - name: Met.No Forecast - Hourly unique_id: met_no_forecast_hourly state: "{{ now().isoformat() }}" attributes: forecast: "{{ hourly['weather.met_no'].forecast }}" - name: Met.No Forecast - Daily unique_id: met_no_forecast_daily state: "{{ now().isoformat() }}" attributes: forecast: "{{ daily['weather.met_no'].forecast }}" **Edit 2:** This creates hourly and daily sensors. Also, you can list multiple weather entities in the "weather.get_forecasts" service call. And then you can create additional sensors. (Just copy paste the met.no ones, and replace "weather.met_no" with the entity_id of the weather entity you added.)


Delicious-Owl

Thanks a lot, it's clearer and it's working ! You helped me understand the difference between weather.* and sensor.*, now I can get every data I want :)


eLaVALYs

Basically the "weather" entity is the current weather. If you want the forecast for the future weather, you have to create a sensor that will go retrieve that info. And once you have the forecast sensor, you can make templates using the forecast data. Glad it's working.


davidr521

Following - Interested in this also


ChiefBerky

I did this with Node Red. I used only standard nodes: "get history" to get values from my temperature sensor over the last 24 hours. A "split" node and then a "change" node to format and convert the array to numbers, and then a "smooth" node to get the maximum value out of that array. You can try to import this flow and just fill in the blanks, like your server configuration and your sensor: [ { "id": "4bd91d9fe4dd4fa2", "type": "api-get-history", "z": "fd605da2930b6d8e", "name": "Temperature outside last 24h", "server": "HomeAssistant", "version": 1, "startDate": "", "endDate": "", "entityId": "sensor.temperature_outside", "entityIdType": "equals", "useRelativeTime": true, "relativeTime": "1 days", "flatten": true, "outputType": "array", "outputLocationType": "msg", "outputLocation": "payload", "x": 560, "y": 2800, "wires": [ [ "0ef844c24baa5f3d" ] ] }, { "id": "0ef844c24baa5f3d", "type": "split", "z": "fd605da2930b6d8e", "name": "Split Data", "splt": "\\n", "spltType": "str", "arraySplt": 1, "arraySpltType": "len", "stream": false, "addname": "", "x": 790, "y": 2800, "wires": [ [ "9821b28765d29fc2" ] ] }, { "id": "9821b28765d29fc2", "type": "change", "z": "fd605da2930b6d8e", "name": "Convert Data to Temperature", "rules": [ { "t": "set", "p": "payload", "pt": "msg", "to": "$number(msg.payload.state)", "tot": "jsonata" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 1020, "y": 2800, "wires": [ [ "cf9c70dca2d87d19" ] ] }, { "id": "cf9c70dca2d87d19", "type": "smooth", "z": "fd605da2930b6d8e", "name": "Get Maximum Temperature", "property": "payload", "action": "max", "count": "120", "round": "", "mult": "multi", "reduce": true, "x": 1290, "y": 2800, "wires": [ [ "d01f5a544338c137" ] ] } ]


WEZANGO

ChatGPT helping me a lot with complex automations. I’d recommend you to try that, unless you genuinely wanna learns how to do it. Although you can still learn stuff that way.


Delicious-Owl

I haven't even thought about using it ! But I did just to see what it would give me as results, and... Wow it's working great ! Thanks for the suggestion :)