{"id":368,"date":"2017-10-30T20:47:52","date_gmt":"2017-10-30T20:47:52","guid":{"rendered":"https:\/\/www.cribbstechnologies.com\/?p=368"},"modified":"2017-10-30T20:54:19","modified_gmt":"2017-10-30T20:54:19","slug":"parsing-structured-data-in-a-sensor","status":"publish","type":"post","link":"https:\/\/www.cribbstechnologies.com\/index.php\/2017\/10\/30\/parsing-structured-data-in-a-sensor\/","title":{"rendered":"Parsing Structured Data in a Sensor"},"content":{"rendered":"<p>[et_pb_section bb_built=&#8221;1&#8243; admin_label=&#8221;section&#8221;][et_pb_row admin_label=&#8221;row&#8221;][et_pb_column type=&#8221;4_4&#8243;][et_pb_text admin_label=&#8221;Text&#8221; background_layout=&#8221;light&#8221; text_orientation=&#8221;left&#8221; use_border_color=&#8221;off&#8221; border_color=&#8221;#ffffff&#8221; border_style=&#8221;solid&#8221;]<\/p>\n<p><a href=\"http:\/\/vmwareinfo.com\">Carlo<\/a>\u00a0was posting in the Discord chat about wanting to make a sensor for Home Assistant that could use data from a service to determine if today was a day that a flag should be shown. He had found an online RESTful service that would spit back structured data in a JSON format and wasn&#8217;t sure how to proceed.<\/p>\n<p>The service had a URL that would be ever-fresh because it could use &#8220;current_year&#8221; as a parameter\u00a0http:\/\/www.webcal.fi\/cal.php?id=335&amp;format=json&amp;start_year=current_year&amp;end_year=current_year&amp;tz=America%2FNew_York<\/p>\n<p>Then it was time to parse the returned JSON.<\/p>\n<pre lang=\"yaml\">sensor:\r\n  - platform: rest\r\n    resource: http:\/\/www.webcal.fi\/cal.php?id=335&amp;format=json&amp;start_year=current_year&amp;end_year=current_year&amp;tz=America%2FNew_York\r\n    name: Flag  Day\r\n    value_template: &gt;-\r\n      {% set is_flag_day = False %}\r\n      {%- for day_val in value_json -%}\r\n        {% set now_string = now().strftime('%Y-%m-%d') %}\r\n        {%- if day_val.date == now_string and day_val.flag_day == 1-%}\r\n          {% set is_flag_day = True %}\r\n        {%- endif -%}\r\n      {% endfor %}\r\n      {{is_flag_day}}\r\n<\/pre>\n<p>The JSON returned is an array so we tell our jinja template to loop through each day and compare a formatted string of today&#8217;s date against what&#8217;s returned from the service. If it matches and the JSON object also says it&#8217;s a flag day we return true, otherwise false.<\/p>\n<p>Using my demonstration code he was able to remove a relatively large chunk of \u00a0code that had magic numbers in it and replace it with something that is <a href=\"https:\/\/github.com\/CCOSTAN\/Home-AssistantConfig\/commit\/c4bcc3fadc540a0f270e991ac02b5a40e8eaa576\">way easier to follow<\/a>.<\/p>\n<p>[\/et_pb_text][\/et_pb_column][\/et_pb_row][\/et_pb_section]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[et_pb_section bb_built=&#8221;1&#8243; admin_label=&#8221;section&#8221;][et_pb_row admin_label=&#8221;row&#8221;][et_pb_column type=&#8221;4_4&#8243;][et_pb_text admin_label=&#8221;Text&#8221; background_layout=&#8221;light&#8221; text_orientation=&#8221;left&#8221; use_border_color=&#8221;off&#8221; border_color=&#8221;#ffffff&#8221; border_style=&#8221;solid&#8221;] Carlo\u00a0was posting in the Discord chat about wanting to make a sensor for Home Assistant that could use data from a service to determine if today was a day that a flag should be shown. He had found an online RESTful service that would [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,17,15,16],"tags":[],"class_list":["post-368","post","type-post","status-publish","format-standard","hentry","category-blog","category-configuration","category-home-assistant","category-json"],"_links":{"self":[{"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/posts\/368","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/comments?post=368"}],"version-history":[{"count":1,"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/posts\/368\/revisions"}],"predecessor-version":[{"id":369,"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/posts\/368\/revisions\/369"}],"wp:attachment":[{"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/media?parent=368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/categories?post=368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/tags?post=368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}