Skip to content

Commit

Permalink
Template change for efficiency, error proffing
Browse files Browse the repository at this point in the history
  • Loading branch information
SirGoodenough committed Aug 26, 2023
1 parent e6d138f commit 3d5291e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
45 changes: 41 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 11 additions & 7 deletions availability_template.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}

0 comments on commit 3d5291e

Please sign in to comment.