diff --git a/README.md b/README.md index 11e2379..d8e0602 100644 --- a/README.md +++ b/README.md @@ -24,14 +24,51 @@ This expects a list of entities. - The number of entities that are NOT unknown, unavailable, empty, or none are counted. - If the 2 counts are the same, true is returned, else false, defaults to false. -REMEMBER!! -This always returns text, so cast to bool on the other end to be certain of the result. +### REMEMBER!! + +> This always returns text, so cast to bool on the other end to be certain of the result. +> +> Use of the - character in the return template ensures no unwanted spacing is pulled back with your answer. ### Examples +Generically, this can be dropped into many templates to be sure the result will render properly. + ```jinja -{% from 'availability_template.jinja' import avail %} -{{- avail(['sensor.qotd','sensor.qotd.attributes.entries[0]']) | bool -}} +availability: >- + {% from 'availability_template.jinja' import avail %} + {{- avail(['entity_1','entity_2') | bool -}} +``` + +Here is a full example that uses this. It will give you percent sunshine estimate based on data from sun angle and cloud coverage if you have those integrations in your config. (Found the state statement somewhere a while ago, sorry there is no attribution. I use it in my personal config.) + +```jinja +- template + - sensor: + - name: "sunlight_pct" + unique_id: 9a7586c0-0947-4b41-97e0-c0d2150bd0bb + unit_of_measurement: "%" + state: >- + {%- set elevation = state_attr('sun.sun','elevation') | float %} + {%- set cloud_coverage = states('sensor.openweathermap_cloud_coverage') | float %} + {%- set cloud_factor = (1 - (0.75 * ( cloud_coverage / 100) ** 3 )) %} + {%- set min_elevation = -6 %} + {%- set max_elevation = 60 %} + {%- set adjusted_elevation = elevation - min_elevation %} + {%- set adjusted_elevation = [adjusted_elevation,0] | max %} + {%- set adjusted_elevation = [adjusted_elevation,max_elevation - min_elevation] | min %} + {%- set adjusted_elevation = adjusted_elevation / (max_elevation - min_elevation) %} + {%- set adjusted_elevation = adjusted_elevation %} + {%- set adjusted_elevation = adjusted_elevation * 100 %} + {%- set brightness = adjusted_elevation * cloud_factor %} + {{ brightness | round }} + availability: >- + {% from 'availability_template.jinja' import avail %} + {{- avail(['sun.sun','sensor.openweathermap_cloud_coverage']) | bool -}} + icon: mdi:sun-angle + attributes: + friendly_name: "Sunlight Percentage" + ``` ### Other Info diff --git a/availability_template.jinja b/availability_template.jinja index 2b0447b..0fb6507 100644 --- a/availability_template.jinja +++ b/availability_template.jinja @@ -3,13 +3,17 @@ The number of entities are counted. The number of entities that are NOT unknown, unavailable, empty, or none are counted. If the 2 counts are the same, true is returned, else false, defaults to false. - REMEMBER!! + REMEMBER!! This always returns text, so cast to bool on the other end to be certain of the result. - SAMPLE USAGE: - {% from 'avail.jinja' import avail %} - {{- avail(['sensor.qotd','sensor.qotd.attributes.entries[0]']) | bool -}} + Use of the - character in the return template ensures no unwanted spacing is pulled back with your answer. + SAMPLE USAGE: + {% from 'availability_template.jinja' import avail %} + {{- avail(['entity_1','entity_2') | bool -}} #} - {{ ( expand(entity_list)|rejectattr('state','in',['unknown','unavailable','','none']) - |list|count == entity_list|count ) - | default (false) }} + {%- if entity_list is iterable -%} + {{- entity_list | map('states') | reject('in', ['unknown','unavailable','','none']) + | list | count == entity_list | count -}} + {%- else -%} + false + {%- endif -%} {% endmacro %}