narkas
March 11, 2024, 12:59pm
1
Hey!!
I’ve been trying to get the data from weather.get_forecasts but have been unable to.
I can can sucessfully the service and it returns the data.
However if i try to use this data inside a template, it doesn’t seem that the response variable is being populated.
This is my template:
template:
- trigger:
- platform: time_pattern
hours: /1
action:
- service: weather.get_forecasts
data:
type: hourly
target:
entity_id: weather.home
response_variable: hourly
sensor:
- name: Temperature forecast next hour
unique_id: temperature_forecast_next_hour
state: "{{ hourly['weather.home'].forecast[0].temperature }}"
unit_of_measurement: °C
To which i’m getting UndefinedError: ‘hourly’ is undefined
Is there something wrong in my script?
Thanks
petro
(Petro)
March 11, 2024, 1:09pm
2
Are you testing this in the template editor? If yes, the error is correct, hourly is not defined because the only template in that is {{ hourly['weather.home'].forecast[0].temperature }}
. The rest is yaml which is not jinja and yaml does not resolve in the template editor.
Templates are indicated by lines that start and end with {% %} or {{ }}. Everything else is yaml. The language contained inside {% %} and {{ }} is Jinja, it is not yaml.
Examples of a template:
{% set x = 4 %}
{{ x }}
Example of a template inside yaml
some_field: >
{% set x = 4 %}
{{ x }}
explanation:
some_field: > # THIS IS YAML
{% set x = 4 %} #THIS IS JINJA
{{ x }} # THIS IS JINJA
The character > in the some_field line is yaml. This mean the next set of indented lines are p…
narkas
March 11, 2024, 3:32pm
3
Yeah i was testing in the template editor.
I wasn’t aware than only the jinja would get evaluated.
Tested just the jinja there and added it to my automation and it worked.
Thanks!
@narkas , Could you please show how you solved the above problem in the template?
thanks
Troon
(Troon)
July 8, 2024, 9:54am
5
What’s the real question here? There isn’t a “problem” as such. What are you trying to achieve?