this is super weird
I’m clearly targeting hourly:
- name: Weatherman Data
state: "OK"
attributes:
weather_condition_0: >
{% set cond0 = hourly["weather.tomorrow_io_home_hourly"].forecast[0].condition %}
{% set next_setting = as_timestamp(state_attr('sun.sun', 'next_setting')) %}
{% set next_rising = as_timestamp(state_attr('sun.sun', 'next_rising')) %}
{% set cond0_time = as_timestamp(hourly["weather.tomorrow_io_home_hourly"].forecast[0].datetime) %}
{% if ( cond0_time < next_rising and next_rising < next_setting ) or ( cond0_time > next_setting and cond0_time < next_rising ) %}
{% if cond0 == 'sunny' or 'clear-night' %} night {% elif cond0 == 'partlycloudy' %} night-partly-cloudy {% else %} {{ cond0 }} {% endif %}
{% else %}
{{ cond0 }}
{% endif %}
weather_temperature_0: >
{{ hourly["weather.tomorrow_io_home_hourly"].forecast[0].temperature | round }}
weather_timestamp_0: >
{{ as_timestamp(hourly["weather.tomorrow_io_home_hourly"].forecast[0].datetime) | timestamp_custom('%I')
Edwin_D
(Edwin D.)
April 19, 2024, 8:39am
24
It is weird indeed. I do not see anything wrong with your code. What if you swap the actions and do hourly last and dayly first? If that changes things (it shouldn’t) then it would point to a bug where the second service calls internally replaces the data from the first call.
swapping the order didnt change anything. But neither did commenting out the daily service, then the hourly and then both altogether.
And now I’m not receiving anything at all which leads me to believe the attributes were somehow pulled elsewhere from a wonky file config or something.
Will update if I figure it out!
petro
(Petro)
April 19, 2024, 11:07am
26
Do yourself a favor and reduce your overhead by using variables. Also, You don’t need to get the daily information every hour. Separate that out.
e.g.
- trigger:
- platform: time_pattern
hours: /1
action:
- variables:
entity: weather.tomorrow_io_home_hourly
- service: weather.get_forecasts
data:
type: hourly
target:
entity_id: "{{ entity }}"
response_variable: hourly
- variables:
mapped:
sunny: night
clear-night: night
partlycloudy: night-partly-cloudy
info: >
{%- set ns = namespace(items=[]) %}
{%- set sunrise = state_attr('sun.sun', 'next_setting') %}
{%- set sunset = state_attr('sun.sun', 'next_rising') %}
{%- for hour in hourly[entity] if loop.index < 4 %}
{%- set cond = hour.condition %}
{%- set t = hour.datetime | as_datetime | as_local %}
{%- set tod = t.strftime('%I%p') %}
{%- set temp = hour.temperature | round %}
{%- set ret = mapped.get(cond, cond) if (t < sunrise and sunrise < sunset) or (sunset < t < sunrise) else cond %}
{%- set ns.items = ns.items + [dict(cond=ret, tod=tod, temp=temp)] %}
{%- endfor %}
{{ ns.items }}
sensor:
- name: Weatherman Data
device_class: timestamp
state: "{{ now() }}"
attributes:
weather_condition_0: "{{ info[0].cond }}"
weather_temperature_0: "{{ info[0].tod }}"
weather_timestamp_0: "{{ info[0].temp }}"
weather_condition_1: "{{ info[1].cond }}"
weather_temperature_1: "{{ info[1].tod }}"
weather_timestamp_1: "{{ info[1].temp }}"
weather_condition_2: "{{ info[2].cond }}"
weather_temperature_2: "{{ info[2].tod }}"
weather_timestamp_2: "{{ info[2].temp }}"
weather_condition_3: "{{ info[3].cond }}"
weather_temperature_3: "{{ info[3].tod }}"
weather_timestamp_3: "{{ info[3].temp }}"
You can also turn this into a script to debug the output in the scripts trace.
get_weather_info:
sequence:
- variables:
entity: weather.tomorrow_io_home_hourly
- service: weather.get_forecasts
data:
type: hourly
target:
entity_id: "{{ entity }}"
response_variable: hourly
- variables:
mapped:
sunny: night
clear-night: night
partlycloudy: night-partly-cloudy
info: >
{%- set ns = namespace(items=[]) %}
{%- set sunrise = state_attr('sun.sun', 'next_setting') %}
{%- set sunset = state_attr('sun.sun', 'next_rising') %}
{%- for hour in hourly[entity] if loop.index < 4 %}
{%- set cond = hour.condition %}
{%- set t = hour.datetime | as_datetime | as_local %}
{%- set tod = t.strftime('%I%p') %}
{%- set temp = hour.temperature | round %}
{%- set ret = mapped.get(cond, cond) if (t < sunrise and sunrise < sunset) or (sunset < t < sunrise) else cond %}
{%- set ns.items = ns.items + [dict(cond=ret, tod=tod, temp=temp)] %}
{%- endfor %}
{{ ns.items }}
- stop: End the script
response_variable: info
Then using it in your template entity…
- trigger:
- platform: time_pattern
hours: /1
action:
- service: script.get_weather_info
response_variable: info
sensor:
- name: Weatherman Data
device_class: timestamp
state: "{{ now() }}"
attributes:
weather_condition_0: "{{ info[0].cond }}"
weather_temperature_0: "{{ info[0].tod }}"
weather_timestamp_0: "{{ info[0].temp }}"
weather_condition_1: "{{ info[1].cond }}"
weather_temperature_1: "{{ info[1].tod }}"
weather_timestamp_1: "{{ info[1].temp }}"
weather_condition_2: "{{ info[2].cond }}"
weather_temperature_2: "{{ info[2].tod }}"
weather_timestamp_2: "{{ info[2].temp }}"
weather_condition_3: "{{ info[3].cond }}"
weather_temperature_3: "{{ info[3].tod }}"
weather_timestamp_3: "{{ info[3].temp }}"
1 Like
Thanks Petro. Is this a solution to my current issue? For now I’m just looking for this code to work, however shabby it might be than to learn new things like using variables and creating scripts. I will come back to that though.
Edit: As for separating daily and hourly out, ofc that makes sense but here I am making a comparison of daily temps to hourly and if hourly goes higher than daily high or lower than daily low then that is adjusted since it would be odd if an hourly low temp is lower than the daily. I believe I wouldnt be able to read from the response variable in a different block
I realise my current issue is using this trigger in my templates.yaml file where a trigger should be in automations?
configuration.yaml:
edit: maybe not but it must be how the files are set up for my weatherman data to now be empty
Triggers can be used in both automations and templates.
1 Like
petro
(Petro)
April 19, 2024, 8:21pm
30
You can copy/paste the code I wrote and it’ll do what you want. It’s a direct replacement for what you’ve shown.
1 Like
creating a new file with only
- trigger:
- platform: time_pattern
minutes: /1
action:
- variables:
entity: weather.tomorrow_io_home_hourly
- service: weather.get_forecasts
data:
type: hourly
target:
entity_id: "{{ entity }}"
response_variable: hourly
- variables:
mapped:
sunny: night
clear-night: night
partlycloudy: night-partly-cloudy
info: >
{%- set ns = namespace(items=[]) %}
{%- set sunrise = state_attr('sun.sun', 'next_setting') %}
{%- set sunset = state_attr('sun.sun', 'next_rising') %}
{%- for hour in hourly[entity] if loop.index < 4 %}
{%- set cond = hour.condition %}
{%- set t = hour.datetime | as_datetime | as_local %}
{%- set tod = t.strftime('%I%p') %}
{%- set temp = hour.temperature | round %}
{%- set ret = mapped.get(cond, cond) if (t < sunrise and sunrise < sunset) or (sunset < t < sunrise) else cond %}
{%- set ns.items = ns.items + [dict(cond=ret, tod=tod, temp=temp)] %}
{%- endfor %}
{{ ns.items }}
sensor:
- name: Weatherman Data
device_class: timestamp
state: "{{ now() }}"
attributes:
weather_condition_0: "{{ info[0].cond }}"
weather_temperature_0: "{{ info[0].tod }}"
weather_timestamp_0: "{{ info[0].temp }}"
weather_condition_1: "{{ info[1].cond }}"
weather_temperature_1: "{{ info[1].tod }}"
weather_timestamp_1: "{{ info[1].temp }}"
weather_condition_2: "{{ info[2].cond }}"
weather_temperature_2: "{{ info[2].tod }}"
weather_timestamp_2: "{{ info[2].temp }}"
weather_condition_3: "{{ info[3].cond }}"
weather_temperature_3: "{{ info[3].tod }}"
weather_timestamp_3: "{{ info[3].temp }}"
and I’m still not being returned any data (attributes)
petro
(Petro)
April 19, 2024, 9:22pm
33
Make the script I said above and view the trace