noam76
(Noam)
February 28, 2019, 4:34pm
1
Hi, I copy paste this part of code I have in my sensor, it’s for turn on/off switch by time from slider number.
(the automation is working)
who can explain me what do this code line by line ?
and where can I learn the syntax of code syntax with simple example?
- platform: template
sensors:
timer_on_hour:
value_template: '{{ states.input_number.timer_on_hours.state | int }}'
timer_on_min:
value_template: '{{ states.input_number.timer_on_min.state | int }}'
timer_on_clock_start:
value_template: >-
{{ states.sensor.timer_on_hour.state }}:
{%- if states.sensor.timer_on_min.state|length == 1 -%}
0
{%- endif -%}
{{ states.sensor.timer_on_min.state }}
Formatting issues aside…
Line 1 defines that what follows is the configuration of some template sensors.
Line 2 is the key that contains the list of the template sensors you want to create.
Line 3 is the key defining the first template sensor
Line 4 is the template that defines the ‘reading’ for the first sensor - in this case the reading will mirror the value of an input number entity
Line 5 is the key defining the second template sensor
Line 6 is the template that defines the ‘reading’ for the second sensor - same as line 4 but mirrors a different entity
Line 7 is the key defining the third template sensor
Line 8 defines that the ‘reading’ for the third sensor is generated from the multiline template below
Line 9 prints the value of the first template sensor followed by a colon
Line 10 asks whether the value of the second sensor’s length is 1
Line 11 prints a zero if line 10s condition is true
Line 12 ends the condition from the two lines above
Line 13 prints the value of the second sensor
noam76
(Noam)
February 28, 2019, 10:46pm
3
Thanks
The line 10- checking the length to know if is inside a number, If it’s egual to 1, it assign zero to the value?
someting like :
if (lenght of my variable ==1 )
my variable=0
it’s correct ?
It adds a zero to the output string if the physical length of that sensor reading is only one character, so that if the time is 03:05 you get that as the output, rather than 03:5