DRY = Don’t repeat yourself. Well, you can see how much repetition is going on here! Looking to simplify. Is there a way to use variables in YAML? I was thinking about using PHP to generate my configuration.yaml? Any suggestions appreciated!
rest:
- resource: https://cmg-prod.apigee.net/v1/xapi/tvschedules/tvguide/9100006621/web?&duration=480
method: GET
sensor:
- name: TV Guide NBC
value_template: 'OK'
json_attributes_path: "$.data.items[2]"
json_attributes: &tv_guide_json_attributes
- channel
- programSchedules
- name: TV Guide FOX
value_template: 'OK'
json_attributes_path: "$.data.items[3]"
json_attributes: *tv_guide_json_attributes
- name: TV Guide ABC
value_template: 'OK'
json_attributes_path: "$.data.items[1]"
json_attributes: *tv_guide_json_attributes
- name: TV Guide PBS Create
value_template: 'OK'
json_attributes_path: "$.data.items[178]"
json_attributes: *tv_guide_json_attributes
template:
- sensor:
name: PBS Create Shows
unique_id: pbs-create-shows
state: 'OK'
attributes:
titles: >-
[ {%- for i in range(0,((states.sensor.tv_guide_pbs_create.attributes.programSchedules | to_json(ensure_ascii=False)) | from_json()) | count) -%}
'{{ ((states.sensor.tv_guide_pbs_create.attributes.programSchedules | to_json(ensure_ascii=False)) | from_json())[i]['title'] | replace("'","\\'") }}'
{%- if not loop.last %},{% endif -%}
{% endfor -%} ]
start_times: >-
[ {%- for i in range(0,((states.sensor.tv_guide_pbs_create.attributes.programSchedules | to_json(ensure_ascii=False)) | from_json()) | count) -%}
{{ ((states.sensor.tv_guide_pbs_create.attributes.programSchedules | to_json(ensure_ascii=False)) | from_json())[i]['startTime'] }}
{%- if not loop.last %},{% endif -%}
{% endfor -%} ]
end_times: >-
[ {%- for i in range(0,((states.sensor.tv_guide_pbs_create.attributes.programSchedules | to_json(ensure_ascii=False)) | from_json()) | count) -%}
{{ ((states.sensor.tv_guide_pbs_create.attributes.programSchedules | to_json(ensure_ascii=False)) | from_json())[i]['endTime'] }}
{%- if not loop.last %},{% endif -%}
{% endfor -%} ]
- sensor:
- name: FOX Shows
unique_id: fox-shows
state: 'OK'
attributes:
titles: >-
[ {%- for i in range(0,((states.sensor.tv_guide_nbc.attributes.programSchedules | to_json(ensure_ascii=False)) | from_json()) | count) -%}
'{{ ((states.sensor.tv_guide_fox.attributes.programSchedules | to_json(ensure_ascii=False)) | from_json())[i]['title'] | replace("'","\\'") }}'
{%- if not loop.last %},{% endif -%}
{% endfor -%} ]
start_times: >-
[ {%- for i in range(0,((states.sensor.tv_guide_fox.attributes.programSchedules | to_json(ensure_ascii=False)) | from_json()) | count) -%}
{{ ((states.sensor.tv_guide_fox.attributes.programSchedules | to_json(ensure_ascii=False)) | from_json())[i]['startTime'] }}
{%- if not loop.last %},{% endif -%}
{% endfor -%} ]
end_times: >-
[ {%- for i in range(0,((states.sensor.tv_guide_fox.attributes.programSchedules | to_json(ensure_ascii=False)) | from_json()) | count) -%}
{{ ((states.sensor.tv_guide_fox.attributes.programSchedules | to_json(ensure_ascii=False)) | from_json())[i]['endTime'] }}
{%- if not loop.last %},{% endif -%}
{% endfor -%} ]
- sensor:
- name: NBC Shows
unique_id: nbc-shows
state: 'OK'
attributes:
titles: >-
[ {%- for i in range(0,((states.sensor.tv_guide_nbc.attributes.programSchedules | to_json(ensure_ascii=False)) | from_json()) | count) -%}
'{{ ((states.sensor.tv_guide_nbc.attributes.programSchedules | to_json(ensure_ascii=False)) | from_json())[i]['title'] | replace("'","\\'") }}'
{%- if not loop.last %},{% endif -%}
{% endfor -%} ]
start_times: >-
[ {%- for i in range(0,((states.sensor.tv_guide_nbc.attributes.programSchedules | to_json(ensure_ascii=False)) | from_json()) | count) -%}
{{ ((states.sensor.tv_guide_nbc.attributes.programSchedules | to_json(ensure_ascii=False)) | from_json())[i]['startTime'] }}
{%- if not loop.last %},{% endif -%}
{% endfor -%} ]
end_times: >-
[ {%- for i in range(0,((states.sensor.tv_guide_nbc.attributes.programSchedules | to_json(ensure_ascii=False)) | from_json()) | count) -%}
{{ ((states.sensor.tv_guide_nbc.attributes.programSchedules | to_json(ensure_ascii=False)) | from_json())[i]['endTime'] }}
{%- if not loop.last %},{% endif -%}
{% endfor -%} ]
- sensor:
- name: ABC Shows
unique_id: abc-shows
state: 'OK'
attributes:
titles: >-
[ {%- for i in range(0,((states.sensor.tv_guide_abc.attributes.programSchedules | to_json(ensure_ascii=False)) | from_json()) | count) -%}
'{{ ((states.sensor.tv_guide_abc.attributes.programSchedules | to_json(ensure_ascii=False)) | from_json())[i]['title'] | replace("'","\\'") }}'
{%- if not loop.last %},{% endif -%}
{% endfor -%} ]
start_times: >-
[ {%- for i in range(0,((states.sensor.tv_guide_abc.attributes.programSchedules | to_json(ensure_ascii=False)) | from_json()) | count) -%}
{{ ((states.sensor.tv_guide_abc.attributes.programSchedules | to_json(ensure_ascii=False)) | from_json())[i]['startTime'] }}
{%- if not loop.last %},{% endif -%}
{% endfor -%} ]
end_times: >-
[ {%- for i in range(0,((states.sensor.tv_guide_abc.attributes.programSchedules | to_json(ensure_ascii=False)) | from_json()) | count) -%}
{{ ((states.sensor.tv_guide_abc.attributes.programSchedules | to_json(ensure_ascii=False)) | from_json())[i]['endTime'] }}
{%- if not loop.last %},{% endif -%}
{% endfor -%} ]
- sensor:
- name: NBC Show Announcement
unique_id: nbc-show-announcement
state: >-
{% for title in states.sensor.nbc_shows.attributes.titles %}
{%- set good_start_time = states.sensor.nbc_shows.attributes.start_times[loop.index0] < as_timestamp(now().replace(hour=22, minute=0, second=0)) | round(0) %}
{%- if 'dateline' in title | lower and good_start_time -%}
{{ title }} is on NBC at {{ states.sensor.nbc_shows.attributes.start_times[loop.index0] | timestamp_custom("%-I:%M %p") }}.
{%- if not loop.last -%}
{{ ' ' }}
{%- endif %}
{%- endif -%}
{% endfor -%}
- sensor:
- name: FOX Show Announcement
unique_id: fox-show-announcement
state: >-
{% for title in states.sensor.fox_shows.attributes.titles %}
{%- set good_start_time = states.sensor.fox_shows.attributes.start_times[loop.index0] < as_timestamp(now().replace(hour=22, minute=0, second=0)) | round(0) %}
{%- if 'masterchef' in title | lower and good_start_time -%}
{{ title }} is on FOX at {{ states.sensor.fox_shows.attributes.start_times[loop.index0] | timestamp_custom("%-I:%M %p") }}.
{%- if not loop.last -%}
{{ ' ' }}
{%- endif %}
{%- endif -%}
{% endfor -%}
- sensor:
- name: PBS Create Show Announcement
unique_id: pbs-create-show-announcement
state: >-
{% for title in states.sensor.pbs_create_shows.attributes.titles %}
{%- set good_start_time = states.sensor.pbs_create_shows.attributes.start_times[loop.index0] < as_timestamp(now().replace(hour=22, minute=0, second=0)) | round(0) %}
{%- if 'belton' in title | lower and good_start_time -%}
{{ title }} is on PBS Create at {{ states.sensor.pbs_create_shows.attributes.start_times[loop.index0] | timestamp_custom("%-I:%M %p") }}.
{%- if not loop.last -%}
{{ ' ' }}
{%- endif %}
{%- endif -%}
{% endfor -%}
- sensor:
- name: ABC Show Announcement
unique_id: abc-show-announcement
state: >-
{% for title in states.sensor.abc_shows.attributes.titles %}
{%- set good_start_time = states.sensor.abc_shows.attributes.start_times[loop.index0] < as_timestamp(now().replace(hour=22, minute=0, second=0)) | round(0) %}
{%- if 'shark tank' in title | lower and good_start_time %}
{{ title }} is on ABC at {{ states.sensor.abc_shows.attributes.start_times[loop.index0] | timestamp_custom("%-I:%M %p") }}.
{%- if not loop.last -%}
{{ ' ' }}
{%- endif %}
{%- endif -%}
{% endfor -%}