Forecasted precipitation sensor

How to create template sensor for forecasted precipitation (Openweathermap or Met.no)?
I’ve seen more than a couple of topics about it and all of them say to use attributes, what makes sense but I can’t find them in my integrations.
Met.no:

temperature: 10.8
dew_point: -1.2
temperature_unit: °C
humidity: 43
cloud_coverage: 19.1
uv_index: 2.2
pressure: 1022
pressure_unit: hPa
wind_bearing: 356.3
wind_gust_speed: 41.8
wind_speed: 19.4
wind_speed_unit: km/h
visibility_unit: km
precipitation_unit: mm
attribution: >-
  Weather forecast from met.no, delivered by the Norwegian Meteorological
  Institute.
friendly_name: met.no
supported_features: 3

and Openweathermap:

temperature: 8.5
apparent_temperature: 5.8
dew_point: 2.3
temperature_unit: °C
humidity: 65
cloud_coverage: 34
pressure: 1022
pressure_unit: hPa
wind_bearing: 16
wind_gust_speed: 18.54
wind_speed: 17.1
wind_speed_unit: km/h
visibility_unit: km
precipitation_unit: mm
attribution: Data provided by OpenWeatherMap
friendly_name: OpenWeatherMap
supported_features: 3

I would like to create sensor for forecasted daily precipitation and use it in my irrigation automation… How to do it?

You have to use the get_forecasts service

(see examples at bottom)

If you have more questions, feel free to ask. This is one of the harder things to do in HA.

Thanks for prompt replies.
When I was testing template in developers tools:

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

I keep receiving following error:

UndefinedError: 'hourly' is undefined

“daily” data type causes same error type.
“weather.openweathermap” is my OpenWeatherMap integration.

What am I missing here?

To be clear, the only template in what you posted is:

and in that circumstance, hourly does not exist because actions did not execute.

The only way to test that template in the template editor would be to run the weather.get_forecasts in developer tools → actions tab. Then copy the response data and set it inside the template. e.g.

{% set hourly = <copied response data> %}

Ok, so what is the correct work flow in this case?
For example:
1/ Action get_forecast at predefined periods
2/ Use above to set sensor value(?)

Do I need to do it in yaml or gui automation? I’m a bit lost here, I don’t get a logic…

The code you posted above goes inside your configuration.yaml file.

Testing it in the template editor is possible but not straight forward because you need data from a service call. The template editor does not run service calls, which is why it doesn’t see the variable and why it’s producing an error.

Thanks, I’ve got better understanding now.
The only thing which is strange that’s I created template entity with its unique id, I removed it later (completely) and that id is still visible in the system. Even more strange is that afterwards I created new template id with slightly different id and its state is visible/assigned to that deleted id…
How to sort it out?

If you used a unique id, you have to delete it in yaml and the ui. The unique id allows you to edit the entity in the ui.

Like I said, yaml is removed and in ui, delete option is greyed out…
I changed in ui old id to new id but this action duplicated in ui sensor belonging this time to sensor integration.
So, now I have 2 sensors with the same id (supposed to be impossible?), old one belonging to template integration, new (duplicated) to sensor integration… :thinking: and both have the same value/state and can’t be removed.

You have to wait for HA to realize the yaml is no longer present. It’s not a simple process because HA doesn’t know when yaml exists or not.

I have no idea if you’re referring to the entity_id or unique_id. It’s not possible to have 2 of either. The template entities under the hood append additional information to the unique_id, which means what you put in will not be what HA stores. So if you’re referring to unique_id, you will have 2 config entries at the end of the day. Unique_id’s are only unique within the integration. Separate integrations have separate information appended to each unique_id. This is done so things can remain unique in the system if the user accidentally uses the same unique_id inside 2 different integrations.

Entity_id’s will append _2 or 3 or 4 etc if the system detects the same entity_id being used.

To cut a longer story short:
Initially I had template sensor with unique_id “A”.

template:
  - trigger:
      - trigger: time_pattern
        hours: /1
    action:
      - action: weather.get_forecasts
        data:
          type: daily
        target:
          entity_id: weather.forecast_dom
        response_variable: daily_forecast
    sensor:
      - name: "Rain precipitation"
        unique_id: "A"
        state: "{{ daily_forecast['weather.forecast_dom'].forecast[0].precipitation_probability }}"
        unit_of_measurement: "%"

Removed that yaml template and sensor with that id “A” remains in gui and can’t be deleted (even with recorder’s purge).
I created second template sensor with unique_id “B”. That id is not visible in gui; instead sensor “A” shows state of sensor with unique_id “B”.
That’s why I’m a bit lost…

You’re mixing up unique_id and entity_ids.

The unique_id creates a unique config entry. Entity_id is what you as a user uses to target the entity in question.

If you create 2 config entries (which are generated from the unique_ids in yaml) that use the same entity_id, one will have the entity_id you want, the second will have the entity_id you want with an _2. This is what’s happening for you.

Go to developer tools → states page and you’ll see both entities or just one if the other doesn’t exist anymore.

EDIT: Your entity_id is derived from the name in your configuration. e.g. your entity_ids should be:

sensor.rain_precipitation
sensor.rain_precipitation_2

for A and B.

For future reference, you shouldn’t change your unique_id ever. Always just fix the sensor without touching the unique_id. This will retain your original entity_id and config entry without the headache you’ve created for yourself. If you can remember the original unique_id, I’d swap back and ignore the B configuration. Then in a couple of days when HA realizes B is no longer in the yaml, delete the b entity via the UI.

1 Like

Thanks very much for your patience. Now all makes sense.

Just for my knowledge, is it possible to use instead of unique_id, entity_id in template?

I don’t understand what you’re asking.

You don’t need to use a unique_id, but if you omit it, you can’t use the entity in the UI. That means you won’t be able to assign it to areas, change precision, update the icon, etc.