And I’m receiving this error (the value is what I’m seeking but why don’t it apply it to the “lamp1”? Invalid service data for script.turn_on: expected dict for dictionary value @ data['variables']. Got "brightness: '41'"
Hope you can help me, thank you in advance. Cheers!
the scope of your jinja varaibles is wrong. You’re tyring to create variables in the variables section, then use the bri variable in the brightness section. They need to be encompassed in a single section.
This should work:
- alias: 'Cool automation name here'
trigger:
platform: time
minutes: '/1'
seconds: 00
condition:
condition: and
conditions:
- condition: numeric_state
entity_id: sensor.room1_lux
below: '14000'
action:
- service: script.turn_on
entity_id: script.controllightwithlux
data_template:
variables:
brightness: >
{%- set lux = states.sensor.room1_lux.state | int -%}
{%- set perc = (14000 - lux) / (14000 / 100) | int -%}
{{ ((76.5 * perc) / 100) | int }}
So I have to ask, why are you triggering this automation once every minute, when it really only needs to run whenever the state of sensor.room1_lux changes (and it’s below 14000)? Why not this:
- alias: Blah
trigger:
platform: numeric_state
entity_id: sensor.room1_lux
below: 14000
condition:
# Put your other conditions here...
action:
# Run your actions...
This will trigger whenever the state or any attribute of sensor.room1_lux changes. Yes, I know what the docs say about the numeric_state trigger, but they’re wrong; see documentation PR 6054. I’ve looked at the code, and even tested it - it simply does not just fire once when the state “crosses a threshold.”
I had trouble in the past having the numeric_state to trigger my automation
I want my script to refresh every minute in order to follow the decrease in lux from the light sensor - unless this could be done by adding some line directly in the script?
That’s my point. When the lux value decreases, it will generate a state_changed event for that entity, which will cause the numeric_state trigger to fire (assuming the value is below 14000.) No need to fire every minute if the value isn’t changing.
I’d be more than happy to help if you try the numeric_state trigger again and it for some reason doesn’t work the way you expect. I currently use it in five automations and I’ve had no problems with it.
So what you’re saying is that for every update my light sensor triggers a value below the threshold it would update the script with the new brightness? I will give it a go ASAP.
And regarding the numeric_state trigger I’ve implemented in the automation now and it already seems to work - but I’ll let you know it it gives me trouble.
Petro, it seems that you are quite skilled in these types of automations. If I want to have a fixed brightness when the LUX reaches below a certain threshold how do I do it - and/or where am I wrong?
variables:
brightness: >-
{% if states.sensor.room1_lux.state | int <= 5000 %}
40
{% elif states.sensor.room1_lux.state | int < 5000 %}
{%- set lux = states.sensor.room1_lux.state | int -%}
{%- set perc = (13500 - lux) / (13500 / 100) | int -%}
{{ ((30 * perc) / 100) | int }}
{% endif %}
Yes. Even if the state (i.e., the lux level) doesn’t change, but just an attribute of the sensor.room1_lux entity changes, it will also trigger the automation (as long as the state, interpreted as a numeric_value, is less than the below value of 14000.)
I just verified with one of my automations. It’s triggered using a numeric_state trigger where the entity is estimated outdoor illuminance (using a custom sensor platform I wrote.) The illuminance entity updated three times, even though the state didn’t change. (It has an attribute which provides the weather condition that goes into the estimation, and it changed to rain, to cloudy, and back to rain, and the automation triggered each time.)
FYI, if you really wanted the automation to trigger only when the state changed (and its new value is below 14000), that could be done using a template trigger.
You are close, you just need an else instead of elif.
And you could reorganize it so you only get the state once. EDIT: Not even sure if this makes a difference time wise because we are just accessing the object. But it does make it nice to edit in the future.
variables:
brightness: >-
{%- set lux = states.sensor.room1_lux.state | int -%}
{% if lux <= 5000 %}
40
{% else %}
{%- set perc = (13500 - lux) / (13500 / 100) | int -%}
{{ ((30 * perc) / 100) | int }}
{% endif %}
please let me ask why you run this with the script you posted above? And not from within the automation itself? Seems that would be simpler. Unless you call the script from elsewhere too of course.
something like:
- service: light.turn_on
entity_id: light.lamp1
data_template:
transition: 5
brightness: >
{%- set lux = states('sensor.room1_lux') | int -%}
{% if lux <= 5000 %}
40
{% else %}
{%- set perc = (13500 - lux) / (13500 / 100) | int -%}
{{ ((30 * perc) / 100) | int }}
{% endif %}