I’m new to scripts, but as i’m coming to a point in my hass.io adventure where i need the “same” function more places, I figured saving some time would do me good.
My challange is that, in the below script i want to turn the light on, but only if no override of the light is present. The override always start with ‘input_boolean.override_’.
I pass ‘override’,‘light’ and ‘transition’ from my automation as shown below.
- alias: Gang - Skru op hvis tændt og bevægelse
trigger:
# Some triggers
condition:
condition: and
conditions:
# I wan't these two conditions to be part of my script, the entity_id changes based on light location.
- condition: state
entity_id: input_boolean.override_gang
state: 'off'
# This is fine as the entity_id does not change.
- condition: state
entity_id: sensor.indoor_lights_threshold
state: 'on'
action:
- service: script.light_brightness
data:
override: 'gang'
light: 'group.gang_paneler'
transition: 1
My script is below, i tried some different solutions on getting the ‘override’ variable from the automation and into the condition. This is where i’m at.
Basically i only want the light to turn on if input_boolean.override_gang is off.
light_brightness_up:
alias: 'Skru lysstyrke op'
sequence:
# First try was this, but i'm understanding that entity_id cannot be a variable.
- condition: state
entity_id: "{{ 'input_boolean.' ~ override }}"
state: 'off'
# This is where i'm at right now.
- condition: template
value_template: "{{ 'input_boolean.' ~ override == 'off' }}"
# The below code is working fine.
- service: light.turn_on
data_template:
entity_id: "{{ light }}"
brightness: >
# Some brightness template..
transition: "{{ transition }}"
The error is the following…
Error loading /config/configuration.yaml: while parsing a block mapping in “/config/includes/scripts/script-light.yaml”, line 6, column 7 expected , but found ‘’ in “/config/includes/scripts/script-light.yaml”, line 7, column 29
Gold! I will try the solutions tomorrow! Much appreciated!
Cheers!
EDIT: The solutions from @pnbruckner and @petro are both awesome, but for my use i’m gonna go with having the full condition in the script as this will always control light, and there is always an input_boolean, and it will always be called input_boolean.override_ ending with the location of the override.
Thank you both!! I’m always learning cool new stuff in this community!
The final solution is as follows:
Automation:
- alias: Gang - Skru op hvis tændt og bevægelse
trigger:
- platform: state
entity_id: binary_sensor.gang_motion
to: 'on'
- platform: state
entity_id: sensor.status_familie
from: 'not_home'
to: 'home'
action:
- service: script.light_brightness_up
data:
override: 'gang'
light: 'group.gang_paneler'
transition: 1
Script:
light_brightness_up:
alias: 'Skru lysstyrke op'
sequence:
- condition:
condition: and
conditions:
- condition: template
value_template: "{{ is_state('input_boolean.override_' ~ override, 'off') }}"
- condition: state
entity_id: sensor.indoor_lights_threshold
state: 'on'
- service: light.turn_on
data_template:
entity_id: "{{ light }}"
brightness: >
{%- if ('sun.sun', 'above_horizon') and now().hour >= 09 and now().hour < 11 %}
50
{%- elif ('sun.sun', 'above_horizon') and now().hour >= 11 and now().hour < 13 %}
160
{%- elif ('sun.sun', 'above_horizon') and now().hour >= 13 and now().hour < 16 %}
250
{%- elif ('sun.sun', 'above_horizon') and now().hour >= 16 and now().hour < 18 %}
160
{%- elif ('sun.sun', 'above_horizon') and now().hour >= 18 and now().hour < 21 %}
70
{%- else %}
1
{% endif %}
transition: "{{ transition }}"
Your if statements are wrong because you are missing the is_state call. The brightness isn’t grabbing the states correctly. Also, you can simplify it a bit and reduce your state calls:
Well. It’s working fine.
But as always, you provide a more elegant solution.
I’m working on a template sensor which consumes an external light level, so depending on how much sun/light there is outside, it will scale it from 1 to 250 and then i want to use that value instead of the hard-coded ones above.
the least you should do is fix this portion of your current if statements because that will always resolve to true because you are missing the is_state:
{%- if ('sun.sun', 'above_horizon') ...
change to
{%- if is_state('sun.sun', 'above_horizon')
So, while your code works, it will not work when the sun starts to dip below the horizon at a different time of the year.
That is of course true. Either way, the code is replaced with the one you provided.
I will report back when i get the time to do the dynamic brightness.
Maybe i can explain what i’m doing. It’s maybe a bit confusing that i’m calling it “override”.
First i have some input_booleans, like this:
override_entre: # roughly translates to entrance
override_gang: # translates to hallway
override_stue: # translates to livingroom
Then i have an automation that calls the script:
action:
- service: script.light_brightness_up 'script name
data:
override: 'gang' # my override variable, this could as well be "entre" or "stue".
light: 'group.gang_paneler' # my lightsource variable, or any light source in the rooms "entre" or "stue".
transition: 1 # my transition variable, any number that i want the transition to be.
Then i have the script:
#This is where i use the override variable, it's basically looking at the state of input_boolean.override_gang because "gang" is coming from the variable named override.
- condition: template
value_template: "{{ is_state('input_boolean.override_' ~ override, 'off') }}"
#This is where the "light" and "transition" variables gets used, what it does is it takes 'group.gang_paneler' and puts it in entity_id and the transition value and puts it in transition.
- service: light.turn_on
data_template:
entity_id: "{{ light }}"
brightness: >
{% if is_state('sun.sun', 'above_horizon') %}
{%- if 9 <= now().hour < 11 %}50
{%- elif 11 <= now().hour < 13 %}160
{%- elif 13 <= now().hour < 16 %}250
{%- elif 16 <= now().hour < 18 %}160
{%- elif 18 <= now().hour < 21 %}70
{%- else %}1{% endif %}
{% else %}1{% endif %}
transition: "{{ transition }}"
I hope this sheds some light on it for you, @Mariusthvdb.
You’re mixing up 2 examples. One example was using the True/False that he could pass via ‘override’ variable, the other example was using a string name that he could pass via ‘override’ variable.