ashscott
(Ash)
December 12, 2018, 5:00pm
1
I’m still getting my head around templates and can’t yet get this to pass the config check.
I want a slider to be able to change the elevation offset.
What am I missing here?
- alias: Set dark boolean on
trigger:
platform: numeric_state
entity_id: sun.sun
value_template: '{{ state.attributes.elevation }}'
below: "{{ states('input_number.sunset_elevation_offset') | float }}"
action:
- service: input_boolean.turn_on
entity_id: input_boolean.dark
- service: notify.prowl
data_template:
title: "Is it dark?"
message: "It is!"
ashscott
(Ash)
December 12, 2018, 7:44pm
2
Trying it below throws up an ‘expected float’ error.
Is what I’m attempting even possible or do I need to take an alternate approach?
- alias: Set dark boolean on
trigger:
platform: numeric_state
entity_id: sun.sun
value_template: '{{ state.attributes.elevation }}'
below: "{{ (state.input_number.sunset_elevation_offset) | float }}"
action:
- service: input_boolean.turn_on
entity_id: input_boolean.dark
- service: notify.prowl
data_template:
title: "Is it dark?"
message: "It is!"
ashscott
(Ash)
December 12, 2018, 8:24pm
3
I’ve made some progress but I’m now at the edge of my capability.
When using the template checker I now get this result:
Which is an improvement, but I still get the ‘expected float’ error.
I’d really appreciate some help.
123
(Taras)
December 12, 2018, 9:06pm
4
Either of these ought to work.
below: "{{ states.input_number.sunset_elevation_offset.state | float }}"
below: "{{ states('input_number.sunset_elevation_offset') | float }}"
Also, try either one without the | float
just to see if it’s necessary (I suspect it’s not, but I may be wrong).
ashscott
(Ash)
December 12, 2018, 9:28pm
5
Thank you so much for your help.
I tried both of those options, both with and without the | float
and still get the exact same error.
When I run the first one through the template checker I get:
Error rendering template: UndefinedError: 'None' has no attribute 'state'
The second one checks out OK in the template checker but still gives me the same config error.
I’ve substituted the template for a value and the config is OK, so it’s definitely a template config problem.
Any other suggestions to try?
123
(Taras)
December 12, 2018, 9:46pm
6
For completeness, can you post the configuration for this entity: input_number.sunset_elevation_offset
ashscott
(Ash)
December 12, 2018, 9:54pm
7
sunset_elevation_offset:
name: Sunset Elevation Offset
icon: mdi:timer
min: -10.0
max: 10.0
step: 1.0
123
(Taras)
December 12, 2018, 10:08pm
8
Thanks, so the initial value is the default one, namely zero.
ashscott:
When I run the first one through the template checker I get:
Error rendering template: UndefinedError: 'None' has no attribute 'state'
The second one checks out OK in the template checker but still gives me the same config error.
These results are puzzling. The first error message is exactly what you’d expect if the input_number entity did not exist (but it does).
The second one works in the Template Editor but you say it fails in the actual automation. Weird.
Sorry, that’s all I got (for now).
EDIT
There’s an extra “)” in the first template I posted that I’ve since removed. Be sure to get rid of it.
finity
December 13, 2018, 12:41am
9
Have you checked the states page to make sure the entity exists and what the state of the entity is?
ashscott
(Ash)
December 13, 2018, 7:27am
10
Thanks for your time. Yes, the entity exists and has an initial value after restart of -10.0.
ashscott
(Ash)
December 13, 2018, 7:33am
11
Yes, I caught that and changed it already.
ashscott
(Ash)
December 13, 2018, 10:25am
12
I’ve also checked that the value changes with the number slider.
I have a feeling this has something to do with float or int but really don’t know where to look.
ashscott
(Ash)
December 13, 2018, 11:56am
13
It would seem that what I’m attempting is not currently possible.
So, I’ve decided to use a binary sensor to achieve the same:
- platform: template
sensors:
lights_on_elevation:
value_template: >-
{{ (states.sun.sun.attributes.elevation | float) > (states.input_number.sunset_elevation_offset.state | float) }}
Thank you for all of the help.
123
(Taras)
December 13, 2018, 1:33pm
14
I’ve come to the same conclusion. It seems like below:
is not designed to handle templates.
petro
(Petro)
December 13, 2018, 1:39pm
15
It’s possible… just different. You need to evaluate your output in the value_template and assign it hard coded values. Then your below: attribute simply is in the middle of your 2 hardcoded values. Is this a work around? Yes, but it will work.
trigger:
platform: numeric_state
entity_id: sun.sun
value_template: "{{ 0 if state.attributes.elevation < states('input_number.sunset_elevation_offset') | float else 10 }}"
below: 5
EDIT: You could have also just used a value_template trigger. Either is fine.
trigger:
platform: template
value_template: "{{ state_attr('sun.sun','elevation') | float < states('input_number.sunset_elevation_offset') | float }}"
you beat me as I was templating this:
{{ 0 if state_attr('sun.sun','elevation') <
states('input_number.sunset_elevation_offset') | float else 10 }}
still, since I have also worked around this specific format for the below:, please let me ask if the state.attributes.elevation, or any other state in any other entity, can be called this was, if one first states the entity_id on its own? Not a very usual construction but very useful indeed!
1 Like
petro
(Petro)
December 13, 2018, 1:50pm
17
Mariusthvdb:
still, since I have also worked around this specific format for the below:, please let me ask if the state.attributes.elevation, or any other state in any other entity, can be called this was, if one first states the entity_id on its own?
state.anything
is only possible because it treats the entity_id: whatever
as the state
object. This is only doable inside numerical_state trigger and condition. It may be doable in other areas but they will be very specific, i.e. tied to an entity id during the setup.
1 Like
ashscott
(Ash)
December 13, 2018, 1:53pm
18
It was 40 years ago I used to program C and am just getting back to coding with this. I keep looking for variables to use, but of course, HA doesn’t currently support them.
petro
(Petro)
December 13, 2018, 1:55pm
19
They do support variables in certain context. They don’t support global variables. If you want global variables, you can always make a python script that stores states inside sensors.
ashscott
(Ash)
December 13, 2018, 2:02pm
20
That’s beyond me right now!