Hi!
I’m trying to create my first blueprint, and can’t figure out how to make a calculation based on the sensor illumination. I come from a Homey environment where I used a similar calculation, and now I want to transform it to HA.
It works if I set the brightness_pct
to like 50, but my calculation doesn’t work. I’m not sure how to do this, or if it’s even possible? Is this possible? I tried with both wait_template
and to make the calculation within the brightness_pct
settings, but none of them works. What am I missing here?
blueprint:
name: Set dim level on selected light source based on illumination value when motion is triggered
description:
"Dims light based on motion detection and illumination source. You
can choose to dim the light up when the illumination goes up, or dim the light
down when the illumination goes up. The dim level will be set by ten (10) steps
at a time.\n\n
Required entities:\n
- Motion sensor (single sensor or group)\n
- Illuminance sensor\n
- Light source\n
- Max illumination value\n
- Max dim level\n\n
Optional entities:\n
- Turn off wait time\n
- Blocking entity\n
- Run before time\n
- Run after time\n\n
Optional features:\n
- You can define a blocking entity, which blocks the automation from running when
this entity's state is on.\n
- Time limits can be defined to limit the time before and/or after the automation id triggered.\n
- You can use the Turn off wait time input to turn off the light automatically."
domain: automation
input:
motion_sensor:
name: Motion Sensor
description: This sensor will trigger the turning on of the target light source below.
selector:
entity: {}
illuminance_sensor:
name: Illuminance sensor
description: This sensor that will be used to determine the illumination.
default:
selector:
entity:
domain: sensor
device_class: illuminance
multiple: false
target_entity:
name: Target entity
description: The light, switch or scene to turn on when the automation is triggered.
selector:
entity: {}
max_lum_level:
name: Maximal value of the illumination sensor
description: Max illumination value when light should be at it's maximum dim value stated below
default: 50
selector:
number:
min: 0
max: 500
mode: slider
step: 1
max_dim_level:
name: Maximal dim level
description: This is the maximum dim value for the light source in percent
default: 50
selector:
number:
min: 0
max: 100
mode: slider
step: 1
dim_state:
name: Reverse or parallel dim value depending on illumination.
description:
"**Reverse**: The light will *dim down* when illumination goes up.\n
**Parallel**: The light will *dim up* when the illumination goes up."
default: Reverse
selector:
select:
options:
- Reverse
- Parallel
multiple: false
custom_value: false
sunstate_condition:
name: When to trigger automation, based on the elevation of the sun.
description: Use 'Always' when you don't want to add a condition for this.
default: Always
selector:
select:
options:
- Always
- Below horizon
- Above horizon
multiple: false
custom_value: false
no_motion_wait:
name: Turn off wait time (minutes)
description: "*(Optional)*\n
Time in minutes to leave the target entity on after last motion
is detected. If not used entity will not auto turn off."
default:
selector:
number:
min: 0
max: 300
mode: slider
step: 1
duration_time:
name: Transition duration (seconds)
description: "*(Optional)*\n
Number of seconds it takes to dim from current dim level to the next"
default:
selector:
number:
min: 0
max: 60
mode: slider
step: 1
blocker_entity:
name: Blocking entity
description: "*(Optional)*\n
If this entity's state is on, it will prevent the automation from running.
This can be sleep mode or away mode, or even when a light source already is on."
default:
selector:
entity: {}
time_limit_before:
name: Only run before time.
description: "*(Optional)*\n
Automation will only run when time is <u>*earlier*</u> than this input_datetime value."
default:
selector:
entity:
domain: input_datetime
multiple: false
time_limit_after:
name: Only run after time.
description: "*(Optional)*\n
Automation will only run when time is <u>*dim up*</u> than this input_datetime value."
default:
selector:
entity:
domain: input_datetime
multiple: false
mode: restart
max_exceeded: silent
variables:
target_entity: !input target_entity
illuminance_currently: !input illuminance_sensor
max_lum_level: !input max_lum_level
max_dim_level: !input max_dim_level
dim_state: !input dim_state
no_motion_wait: !input no_motion_wait
blocker_entity: !input blocker_entity
duration_time: !input duration_time
sun_condition: !input sunstate_condition
time_limit_before: !input time_limit_before
time_limit_after: !input time_limit_after
dimlevel_pct: 50
dimlevel: 50
entity_domain: "{{ states[target_entity].domain }}"
trigger:
platform: state
entity_id: !input motion_sensor
to: "on"
condition:
- condition: template
alias: Check for illuminance conditions
value_template: >
{% set illuminance_defined = illuminance_currently != none %}
{% set target_entity_domain_supports_on_state_check = entity_domain != 'scene' and entity_domain != 'script' %}
{% set target_entity_is_scene = entity_domain == 'scene' %}
{% set scene_target_entities = state_attr(target_entity, 'entity_id') %}
{% set singular_scene_entity_is_on = scene_target_entities is not none and scene_target_entities | count == 1 and scene_target_entities | first == 'on' %}
{{
( target_entity_domain_supports_on_state_check and states(target_entity) == 'on') or
( target_entity_domain_supports_on_state_check and states(target_entity) == 'off' and not illuminance_defined) or
( target_entity_domain_supports_on_state_check and states(target_entity) == 'off' and illuminance_defined) or
( not target_entity_domain_supports_on_state_check and illuminance_defined) or
( not target_entity_domain_supports_on_state_check and not illuminance_defined) or
( target_entity_is_scene and singular_scene_entity_is_on)
}}
- condition: template
alias: Check for blocker entity
value_template: "{{ (blocker_entity == none) or (states(blocker_entity) == 'off') }}"
- condition: template
alias: "Check for time limits"
value_template: >
{% set current_time = now().strftime("%H:%M") %}
{% if time_limit_before == none and time_limit_after == none %}
true
{% endif %}
{% if time_limit_before != none and time_limit_after == none %}
{% set current_time_is_before_limit = current_time < states(time_limit_before) %}
{{ current_time_is_before_limit }}
{% elif time_limit_before == none and time_limit_after != none %}
{% set current_time_is_after_limit = current_time > states(time_limit_after) %}
{{ current_time_is_after_limit }}
{% endif %}
{% if time_limit_before != none and time_limit_after != none %}
{% set before_limit_is_tomorrow = states(time_limit_before) < states(time_limit_after) %}
{% set current_time_is_before_limit = current_time < states(time_limit_before) %}
{% set current_time_is_after_limit = current_time > states(time_limit_after) %}
{% set time_window_spans_midnight = states(time_limit_after) > states(time_limit_before) %}
{% if time_window_spans_midnight != none and time_window_spans_midnight and before_limit_is_tomorrow %}
{{ current_time_is_after_limit or current_time_is_before_limit }}
{% elif time_window_spans_midnight != none and not time_window_spans_midnight %}
{{ current_time_is_before_limit and current_time_is_after_limit }}
{% endif %}
{% endif %}
- condition: template
alias: "Check for sun state"
value_template: >
{# Prevent breaking when no sun condition is set in existing automations #}
{% if not sun_condition is defined %}
{% set sun_condition = 'Always' %}
{% endif %}
{# Get current sun state #}
{% set current_sun_condition = states['sun.sun'].state %}
{# Checks for current sun state #}
{% set sun_state_is_below_horizon = current_sun_condition == 'below_horizon' %}
{% set sun_state_is_above_horizon = current_sun_condition == 'above_horizon' %}
{# Check to see if a condition for sunstate is set by user #}
{% set no_sun_condition = sun_condition is defined and sun_condition == 'Always' %}
{# Check to see if the user wants automation to trigger below horizon #}
{% set sun_condition_is_below_horizon = sun_condition == 'Below horizon' %}
{# Check to see if the user wants automation to trigger above horizon #}
{% set sun_condition_is_above_horizon = sun_condition == 'Above horizon' %}
{# Checks to see if sunstate matches up with users condition #}
{% set sun_condition_below_horizon_match = sun_state_is_below_horizon and sun_condition_is_below_horizon %}
{% set sun_condition_above_horizon_match = sun_state_is_above_horizon and sun_condition_is_above_horizon %}
{% set sun_condition_matching = sun_condition_below_horizon_match or sun_condition_above_horizon_match or no_sun_condition %}
{{ sun_condition_matching }}
action:
# - wait_template: >
# {% if not dim_state is defined %}
# {% set dim_state = 'Reverse' %}
# {% endif %}
# {% set lum = states(illuminance_currently)|int(0) %}
# {% set maxlight = (states(max_dim_level)|int / 100)|float %}
# {% set percent = lum / states(max_lum_level)|int %}
# {% if dim_state == 'Parallel' %}
# {% set dimlevel = [maxlight, [maxlight, percent * maxlight ]|min | round(1)]|min * 255 %}
# {% set dimlevel_pct = [maxlight, [maxlight, percent * maxlight ]|min | round(1)]|min * 100 %}
# {% elif dim_state == 'Reverse' %}
# {% set percentRev = [0, (1 - percent)|float]|max %}
# {% set dimlevel = [0, (states(max_dim_level)|int * percentRev)|float ]|max * 255 %}
# {% set dimlevel_pct = [0, (states(max_dim_level)|int * percentRev)|float ]|max * 100 %}
# {% endif %}
# continue_on_timeout: true
- service: homeassistant.turn_on
entity_id: !input target_entity
data:
transition: "{{ duration_time }}"
brightness_pct: 50
# brightness_pct: >
# {% if not dim_state is defined %}
# {% set dim_state = 'Reverse' %}
# {% endif %}
# {% set lum = states(illuminance_currently)|int(0) %}
# {% set maxlight = (states(max_dim_level)|int(100) / 100)|float %}
# {% set percent = lum / states(max_lum_level)|int(100) %}
# {% if dim_state == 'Parallel' %}
# {% set dimlevel_pct = [maxlight, [maxlight, percent * maxlight ]|min | round(1)]|min * 100 %}
# {% elif dim_state == 'Reverse' %}
# {% set percentRev = [0, (1 - percent)|float]|max %}
# {% set dimlevel_pct = [0, (states(max_dim_level)|int(100) * percentRev)|float ]|max * 100 %}
# {% endif %}
# "{{ dimlevel_pct }}"
- wait_for_trigger:
platform: state
entity_id: !input motion_sensor
from: "on"
to: "off"
- choose:
- alias: "Motion wait not defined"
conditions:
- condition: template
value_template: "{{ no_motion_wait == none }}"
sequence:
- service: homeassistant.turn_off
entity_id: !input target_entity
- alias: "Motion wait defined"
conditions:
- condition: template
value_template: "{{ no_motion_wait != none }}"
sequence:
- delay:
minutes: !input no_motion_wait
- service: homeassistant.turn_off
entity_id: !input target_entity