I am trying to setup a sensor using a REST_Command based upon upcoming vacation events in our calendar. The API is for Visual Crossing (they have a free 14 day weather forecast). The funny thing is that the daily sensor is working well, but not the hourly one.
The vacation events in our calendar have existing sensors with the number of vacation days as their state and an event attribute with a list of date(s). I use some macros to obtain the the vacation dates, and set standard icons and conditions, etc.
A link to the API data (substitute your own key):
https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/syracuse%20fairgrounds%20ny?unitGroup=us&key=MY_API_KEY&contentType=json
A sample of the data returned:
content:
queryCost: 1
latitude: 43.0499
longitude: -76.2405
resolvedAddress: Fairmount, Syracuse, NY, United States
address: syracuse fairgrounds ny
timezone: America/New_York
tzoffset: -4
description: >-
Similar temperatures continuing with a chance of rain Tuesday, Wednesday &
Saturday.
days:
- datetime: "2024-04-20"
datetimeEpoch: 1713585600
tempmax: 54.6
tempmin: 36.9
temp: 47.3
feelslikemax: 54.6
feelslikemin: 29.2
feelslike: 44
dew: 33.3
humidity: 59.3
precip: 0.039
precipprob: 22
precipcover: 4.17
preciptype:
- rain
snow: 0
snowdepth: 0
windgust: 37.3
windspeed: 21.1
winddir: 269.2
pressure: 1014.8
cloudcover: 78.9
visibility: 9.9
solarradiation: 174.2
solarenergy: 15.1
uvindex: 7
severerisk: 10
sunrise: "06:14:09"
sunriseEpoch: 1713608049
sunset: "19:54:10"
sunsetEpoch: 1713657250
moonphase: 0.39
conditions: Partially cloudy
description: Partly cloudy throughout the day.
icon: partly-cloudy-day
stations:
- KITH
- KSYR
- KFZY
- 0534W
source: comb
hours:
- datetime: "00:00:00"
datetimeEpoch: 1713585600
temp: 52.9
feelslike: 52.9
humidity: 74.64
dew: 45
precip: 0
precipprob: 0
snow: 0
snowdepth: 0
preciptype: null
windgust: 11.4
windspeed: 6.9
winddir: 260
pressure: 1014
visibility: 9.9
cloudcover: 89.3
solarradiation: 0
solarenergy: 0
uvindex: 0
severerisk: 10
conditions: Partially cloudy
icon: partly-cloudy-night
stations:
- KITH
- KSYR
- KFZY
- 0534W
source: obs
- datetime: "01:00:00"
datetimeEpoch: 1713589200
temp: 51.9
feelslike: 51.9
humidity: 76.98
dew: 45
precip: 0
precipprob: 0
snow: 0
snowdepth: 0
preciptype: null
windgust: 12.8
windspeed: 5.6
winddir: 257
pressure: 1014
visibility: 9.9
cloudcover: 98.3
solarradiation: 0
solarenergy: 0
uvindex: 0
severerisk: 10
conditions: Overcast
icon: cloudy
stations:
- KITH
- KSYR
- KFZY
- 0534W
source: obs
- datetime: "02:00:00"
datetimeEpoch: 1713592800
temp: 51.7
feelslike: 51.7
humidity: 75.16
dew: 44.1
precip: 0
precipprob: 0
snow: 0
snowdepth: 0
preciptype: null
windgust: 11.4
windspeed: 9.2
winddir: 246
pressure: 1013.9
visibility: 9.9
cloudcover: 100
solarradiation: 0
solarenergy: 0
uvindex: 0
severerisk: 10
conditions: Overcast
icon: cloudy
stations:
- KITH
- KSYR
- KFZY
- 0534W
source: obs
- datetime: "03:00:00"
datetimeEpoch: 1713596400
temp: 51.5
feelslike: 51.5
humidity: 66.4
dew: 40.6
precip: 0
precipprob: 0
snow: 0
snowdepth: 0
preciptype: null
windgust: 13.9
windspeed: 12.8
winddir: 276
pressure: 1013.2
visibility: 9.9
cloudcover: 99.6
solarradiation: 0
solarenergy: 0
uvindex: 0
severerisk: 10
conditions: Overcast
icon: cloudy
stations:
- KITH
- KSYR
- KFZY
- 0534W
source: obs
- datetime: "04:00:00"
datetimeEpoch: 1713600000
temp: 48.4
feelslike: 43.1
humidity: 69.84
dew: 39
precip: 0
precipprob: 0
snow: 0
snowdepth: 0
preciptype: null
windgust: 13.9
windspeed: 13
winddir: 311
pressure: 1013.8
visibility: 9.9
cloudcover: 98.7
solarradiation: 0
solarenergy: 0
uvindex: 0
severerisk: 10
conditions: Overcast
icon: cloudy
stations:
- KITH
- KSYR
- KFZY
- 0534W
source: obs
- datetime: "05:00:00"
datetimeEpoch: 1713603600
temp: 45.7
feelslike: 42.9
humidity: 68.24
dew: 35.8
precip: 0
precipprob: 0
snow: 0
snowdepth: 0
preciptype: null
windgust: 11.4
windspeed: 5.4
winddir: 315
pressure: 1015.2
visibility: 9.9
cloudcover: 98.3
solarradiation: 0
solarenergy: 0
uvindex: 0
severerisk: 10
conditions: Overcast
icon: cloudy
stations:
- KITH
- KSYR
- KFZY
- 0534W
source: obs
- datetime: "06:00:00"
datetimeEpoch: 1713607200
temp: 44.1
feelslike: 41.8
humidity: 72.46
dew: 35.8
precip: 0
precipprob: 0
snow: 0
snowdepth: 0
preciptype: null
windgust: 10.3
windspeed: 4.3
winddir: 273
pressure: 1015.1
visibility: 9.9
cloudcover: 90.4
solarradiation: 0
solarenergy: 0
uvindex: 0
severerisk: 10
conditions: Overcast
icon: cloudy
stations:
- KITH
- KSYR
- KFZY
- 0534W
source: obs
- datetime: "07:00:00"
datetimeEpoch: 1713610800
temp: 44.7
feelslike: 44.7
humidity: 66.37
dew: 34.1
precip: 0
precipprob: 0
snow: 0
snowdepth: 0
preciptype: null
windgust: 6.9
windspeed: 0.1
winddir: 358
pressure: 1014.7
visibility: 9.9
cloudcover: 81.3
solarradiation: 53
solarenergy: 0.2
uvindex: 1
severerisk: 10
conditions: Partially cloudy
icon: partly-cloudy-day
stations:
- KITH
- KSYR
- KFZY
- 0534W
source: obs
- datetime: "08:00:00"
datetimeEpoch: 1713614400
temp: 45.9
feelslike: 43.6
humidity: 63.7
dew: 34.2
precip: 0
precipprob: 0
snow: 0
snowdepth: 0
preciptype: null
windgust: 8.1
windspeed: 4.7
winddir: 174
pressure: 1014.5
visibility: 9.9
cloudcover: 79
solarradiation: 203
solarenergy: 0.7
uvindex: 2
severerisk: 10
conditions: Partially cloudy
icon: partly-cloudy-day
stations:
- KITH
- KSYR
- KFZY
- 0534W
source: obs
- datetime: "09:00:00"
datetimeEpoch: 1713618000
temp: 48.6
feelslike: 45.4
humidity: 55.61
dew: 33.4
precip: 0
precipprob: 0
snow: 0
snowdepth: 0
preciptype: null
windgust: 13.9
windspeed: 7.1
winddir: 197
pressure: 1014
visibility: 9.9
cloudcover: 79
solarradiation: 240
solarenergy: 0.9
uvindex: 2
severerisk: 10
conditions: Partially cloudy
icon: partly-cloudy-day
stations:
- KITH
- KSYR
- KFZY
- 0534W
source: obs
- datetime: "10:00:00"
datetimeEpoch: 1713621600
temp: 51.9
feelslike: 51.9
humidity: 50.26
dew: 33.9
precip: 0
precipprob: 0
snow: 0
snowdepth: 0
preciptype: null
windgust: 20.8
windspeed: 8.9
winddir: 221
pressure: 1013.5
visibility: 9.9
cloudcover: 88
solarradiation: 465
solarenergy: 1.7
uvindex: 5
severerisk: 10
conditions: Partially cloudy
icon: partly-cloudy-day
stations:
- KITH
- KSYR
- KFZY
- 0534W
source: obs
- datetime: "11:00:00"
datetimeEpoch: 1713625200
temp: 54
feelslike: 54
humidity: 41.31
dew: 31
precip: 0
precipprob: 0
snow: 0
snowdepth: 0
preciptype: null
windgust: 21.1
windspeed: 12.9
winddir: 230
pressure: 1012.6
visibility: 9.9
cloudcover: 88
solarradiation: 540
solarenergy: 1.9
uvindex: 5
severerisk: 10
conditions: Partially cloudy
icon: partly-cloudy-day
stations:
- KITH
- KSYR
- KFZY
- 0534W
source: obs
- datetime: "12:00:00"
datetimeEpoch: 1713628800
temp: 54.6
feelslike: 54.6
humidity: 38.48
dew: 29.8
precip: 0
precipprob: 0
snow: 0
snowdepth: 0
preciptype: null
windgust: 24.4
windspeed: 16.8
winddir: 234
pressure: 1012.2
visibility: 9.9
cloudcover: 89.3
solarradiation: 166
solarenergy: 0.6
uvindex: 2
severerisk: 10
conditions: Partially cloudy
icon: partly-cloudy-day
stations:
- KITH
- KSYR
- KFZY
- 0534W
source: obs
- datetime: "13:00:00"
datetimeEpoch: 1713632400
temp: 50.5
feelslike: 50.5
humidity: 51.05
dew: 33
precip: 0
precipprob: 0
snow: 0
snowdepth: 0
preciptype: null
windgust: 37.3
windspeed: 20.3
winddir: 307
pressure: 1012.9
visibility: 9.9
cloudcover: 88.4
solarradiation: 229
solarenergy: 0.8
uvindex: 2
severerisk: 10
conditions: Partially cloudy
icon: partly-cloudy-day
stations:
- KITH
- KSYR
- KFZY
- 0534W
source: obs
- datetime: "14:00:00"
datetimeEpoch: 1713636000
temp: 50.2
feelslike: 50.2
humidity: 51.11
dew: 32.8
precip: 0
precipprob: 0
snow: 0
snowdepth: 0
preciptype: null
windgust: 35.1
windspeed: 21.1
winddir: 274
pressure: 1014.1
visibility: 9.9
cloudcover: 88.4
solarradiation: 725
solarenergy: 2.6
uvindex: 7
severerisk: 10
conditions: Partially cloudy
icon: partly-cloudy-day
stations:
- KITH
- KSYR
- KFZY
- 0534W
source: obs
- datetime: "15:00:00"
datetimeEpoch: 1713639600
temp: 47.4
feelslike: 40.5
humidity: 46.64
dew: 28
precip: 0
precipprob: 0
snow: 0
snowdepth: 0
preciptype: null
windgust: 32.9
windspeed: 18.6
winddir: 272
pressure: 1015
visibility: 9.9
cloudcover: 88.4
solarradiation: 669
solarenergy: 2.4
uvindex: 7
severerisk: 10
conditions: Partially cloudy
icon: partly-cloudy-day
stations:
- KITH
- KSYR
- KFZY
- 0534W
source: obs
- datetime: "16:00:00"
datetimeEpoch: 1713643200
temp: 48.4
feelslike: 42.5
humidity: 46.04
dew: 28.6
precip: 0
precipprob: 0
snow: 0
snowdepth: 0
preciptype: null
windgust: 28.7
windspeed: 15.3
winddir: 282
pressure: 1015.1
visibility: 9.9
cloudcover: 86.7
solarradiation: 383
solarenergy: 1.4
uvindex: 4
severerisk: 10
conditions: Partially cloudy
icon: partly-cloudy-day
stations:
- KITH
- KSYR
- KFZY
- 0534W
source: obs
- datetime: "17:00:00"
datetimeEpoch: 1713646800
temp: 48.3
feelslike: 41.2
humidity: 44.15
dew: 27.4
precip: 0
precipprob: 0
snow: 0
snowdepth: 0
preciptype: null
windgust: 29.8
windspeed: 20.7
winddir: 271
pressure: 1015.4
visibility: 9.9
cloudcover: 84.3
solarradiation: 240
solarenergy: 0.9
uvindex: 2
severerisk: 10
conditions: Partially cloudy
icon: partly-cloudy-day
stations:
- KITH
- KSYR
- KFZY
- 0534W
source: obs
- datetime: "18:00:00"
datetimeEpoch: 1713650400
temp: 45.8
feelslike: 39.1
humidity: 54.61
dew: 30.4
precip: 0
precipprob: 0
snow: 0
snowdepth: 0
preciptype: null
windgust: 26.1
windspeed: 15.8
winddir: 270
pressure: 1016.3
visibility: 9.9
cloudcover: 79
solarradiation: 173
solarenergy: 0.6
uvindex: 2
severerisk: 10
conditions: Partially cloudy
icon: partly-cloudy-day
stations:
- KITH
- KSYR
- KFZY
- 0534W
source: obs
- datetime: "19:00:00"
datetimeEpoch: 1713654000
temp: 45.6
feelslike: 38.8
humidity: 46.69
dew: 26.4
precip: 0
precipprob: 0
snow: 0
snowdepth: 0
preciptype: null
windgust: 26.1
windspeed: 16
winddir: 272
pressure: 1016.3
visibility: 9.9
cloudcover: 44.6
solarradiation: 52
solarenergy: 0.2
uvindex: 1
severerisk: 10
conditions: Partially cloudy
icon: partly-cloudy-day
stations:
- KITH
- KSYR
- KFZY
- 0534W
source: obs
- datetime: "20:00:00"
datetimeEpoch: 1713657600
temp: 40
feelslike: 32.2
humidity: 61.78
dew: 27.9
precip: 0.039
precipprob: 22
snow: 0
snowdepth: 0
preciptype:
- rain
windgust: 24.2
windspeed: 13.9
winddir: 280
pressure: 1017
visibility: 10.1
cloudcover: 44
solarradiation: 42
solarenergy: 0.2
uvindex: 0
severerisk: 10
conditions: Partially cloudy
icon: partly-cloudy-night
stations: null
source: fcst
- datetime: "21:00:00"
datetimeEpoch: 1713661200
temp: 38.9
feelslike: 31.7
humidity: 64.43
dew: 27.9
precip: 0
precipprob: 14
snow: 0
snowdepth: 0
preciptype: null
windgust: 19.5
windspeed: 11.4
winddir: 280
pressure: 1018
visibility: 10.1
cloudcover: 40
solarradiation: 0
solarenergy: 0
uvindex: 0
severerisk: 10
conditions: Partially cloudy
icon: partly-cloudy-night
stations: null
source: fcst
- datetime: "22:00:00"
datetimeEpoch: 1713664800
temp: 38
feelslike: 30.6
humidity: 66.74
dew: 27.9
precip: 0
precipprob: 11
snow: 0
snowdepth: 0
preciptype: null
windgust: 19.5
windspeed: 11.4
winddir: 280
pressure: 1018
visibility: 10.1
cloudcover: 37
solarradiation: 0
solarenergy: 0
uvindex: 0
severerisk: 10
conditions: Partially cloudy
icon: partly-cloudy-night
stations: null
source: fcst
- datetime: "23:00:00"
datetimeEpoch: 1713668400
temp: 36.9
feelslike: 29.2
humidity: 69.64
dew: 27.9
precip: 0
precipprob: 4
snow: 0
snowdepth: 0
preciptype: null
windgust: 18.3
windspeed: 11.4
winddir: 270
pressure: 1017
visibility: 10.1
cloudcover: 33
solarradiation: 0
solarenergy: 0
uvindex: 0
severerisk: 10
conditions: Partially cloudy
icon: partly-cloudy-night
stations: null
source: fcst
And the template sensor:
- trigger:
- platform: template
value_template: "{{ states('sensor.vacation1_event') >= '1' }}"
- platform: time_pattern
hours: /23
- platform: homeassistant
event: start
- platform: event
event_type: event_template_reloaded
action:
- service: rest_command.weather_vc_vacation
response_variable: daily_forecast
- variables:
weather_day: >
{{ daily_forecast['content']['days'] }}
sensor:
- name: "Template VC Vacation1 Daily"
unique_id: template_vc_vacation1_daily
state: "{{ now().isoformat() }}"
availability: >-
"{{ states('sensor.vacation1_event') >= '1' }}"
attributes:
forecast: >
{%- from 'events.jinja' import get_event_dates -%}
{%- from 'weather.jinja' import weather_icon, wind_direction, get_dew, weather_condition %}
{%- set ns = namespace (results=[], evt_weather=[]) -%}
{%- set event_name = 'Vacation' -%}
{%- set event_data = get_event_dates(event_name) | from_json -%}
{%- set event_num = event_data[0].num | int-%}
{%- set event_dtes = event_data[0].events -%}
{%- for day in weather_day -%}
{%- set icon = weather_icon((day.conditions|lower)) | trim -%}
{%- set condition = weather_condition(day.conditions) | trim -%}
{%- set temp = day.temp | round -%}
{%- set mintemp = day.tempmin | round -%}
{%- set apptemp = day.feelslikemax| round -%}
{%- set wind = wind_direction(day.windspeed, day.winddir) | trim -%}
{%- set dte = day.datetimeEpoch | timestamp_custom('%Y-%m-%d') + 'T00:00:00Z' -%}
{%- set preciptype = "" -%}
{%- set preciptype = day.preciptype[0] if day.preciptype is defined else "" -%}
{%- set new_dt = day.datetimeEpoch | timestamp_local -%}
{%- set ns.results = ns.results + [{'date': day.datetime, 'datetime': new_dt, 'temperature': temp, 'templow': mintemp,
'apparent_temperature': apptemp, 'humidity': day.humidity, 'condition': condition, 'ha_icon': icon,
'precipitation': preciptype, 'precipitation_probability': day.precipprob, 'precipitation_type': preciptype, 'snow': day.snow,
'wind_gust_speed': day.windgust, 'wind_speed': day.windspeed, 'wind_bearing': day.winddir , 'snowdepth': day.snowdepth,
'pressure': day.pressure, 'uv_index': day.uvindex, 'dew_point': day.dew, 'wind': wind, 'description': day.description,
'visibility': day.visibility, 'cloud_coverage': day.cloudcover }] -%}
{%- endfor -%}
{% if event_num <= 1 %}
{%- set ns.evt_weather = ns.results | selectattr('date', 'eq', event_dtes[0]) | list -%}
{%- elif event_num > 1 %}
{% for evt_dte in event_dtes %}
{%- set tmp_data = (ns.results | selectattr('date', 'eq', evt_dte) | list) -%}
{%- set ns.evt_weather = ns.evt_weather + tmp_data -%}
{% endfor %}
{%- else -%}
{%- set ns.evt_weather = '' -%}
{%- endif %}
{{ ns.evt_weather }}
- name: "Template VC Vacation1 Hourly"
unique_id: template_vc_vacation1_hourly
state: "{{ now().isoformat() }}"
availability: >-
"{{ states('sensor.vacation1_event') >= '1' }}"
attributes:
forecast: >
{%- from 'events.jinja' import get_event_dates -%}
{%- from 'weather.jinja' import weather_icon, wind_direction, get_dew, weather_condition %}
{%- set ns = namespace (results=[], evt_weather=[]) -%}
{%- set event_name = 'Vacation' -%}
{%- set event_data = get_event_dates(event_name) | from_json -%}
{%- set event_num = event_data[0].num | int-%}
{%- set event_dtes = event_data[0].events -%}
{%- set curr_time = ((utcnow() | as_timestamp | timestamp_utc)[:-19] + ':00:00-00:00')
| as_timestamp %}
{%- set start_time = '08:00:00' %}
{%- set end_time = '17:00:00' %}
{%- for day in daily_forecast['content']['days'] -%}
{%- for hr in day.hours -%}
{%- set chk_dt = hr.datetimeEpoch -%}
{%- set new_dt = hr.datetimeEpoch | timestamp_utc -%}
{%- set dte = day.datetimeEpoch | timestamp_custom('%Y-%m-%d') -%}
{% if (chk_dt >= curr_time and hr.datetime >= start_time and hr.datetime <= end_time) and dte in event_dtes -%}
{%- set icon = weather_icon((hr.conditions|lower)) | trim -%}
{%- set condition = weather_condition(hr.conditions) | trim -%}
{%- set temp = hr.temp | round -%}
{%- set mintemp = day.tempmin | round -%}
{%- set apptemp = hr.feelslike| round -%}
{%- set wind = wind_direction(hr.windspeed, hr.winddir) | trim -%}
{%- set preciptype = "" -%}
{%- set preciptype = hr.preciptype[0] if hr.preciptype is defined else "" -%}
{%- set ns.results = ns.results + [{'date': day.datetime, 'datetime': hr.datetime, 'temperature': temp, 'templow': mintemp,
'apparent_temperature': apptemp, 'humidity': hr.humidity, 'condition': condition, 'ha_icon': icon,
'precipitation': hr.precip, 'precipitation_probability': hr.precipprob, 'precipitation_type': preciptype, 'snow': hr.snow,
'wind_gust_speed': hr.windgust, 'wind_speed': hr.windspeed, 'wind_bearing': hr.winddir , 'snowdepth': hr.snowdepth,
'pressure': hr.pressure, 'uv_index': hr.uvindex, 'dew_point': hr.dew, 'wind': wind, 'description': day.description,
'visibility': hr.visibility, 'cloud_coverage': hr.cloudcover }] -%}
{% endif -%}
{%- endfor -%}
{%- endfor -%}
{% if event_num <= 1 %}
{%- set ns.evt_weather = ns.results | selectattr('date', 'eq', event_dtes[0]) | list -%}
{%- elif event_num > 1 %}
{% for event_dte in event_dtes %}
{%- set tmp_data = (ns.results | selectattr('date', 'eq', event_dte) | list) -%}
{%- set ns.evt_weather = ns.evt_weather + tmp_data -%}
{% endfor %}
{%- else -%}
{%- set ns.evt_weather = '' -%}
{%- endif %}
{{ ns.evt_weather }}
This works in Developer tools - template, but I suspect I made a mistake in the YAML. Where am I going wrong?