fillwe
(Fillwe)
July 24, 2018, 6:31pm
1
Hello everyone, I have tried to figure out template sensors for several hours now and tried to find examples that match my needs but with no luck
Right now I have two sensors:
sensor.weeknumber displays current week in numbers
sensor.evenweek displays true if week number is even, if not it displays false
I would like to create a template sensor with friendly name ”Is it garbage emptying week?” with a Yes or No answer:
If sensor.evenweek says true OR sensor.weeknumber shows a number between 25 to 34 it should show Yes. In other cases it should show No.
Im halfway there I think, but have no idea how to proceed
- platform: template
sensors:
garbagepickupweek:
friendly_name: Is it garbage emptying week?
value_template: >-
{% if is_state("sensor.evenweek", "true") %}
Yes
{% else %}
No
{% endif %}
Any help would be much appreciated
value_template
needs to line up exactly under friendly_name
. That’s the problem.
Having said that, here’s a bit of a more compact solution:
- platform: template
sensors:
garbagepickupweek:
friendly_name: Is it garbage emptying week?
value_template: "{{ 'Yes' if is_state('sensor.evenweek', 'true') else 'No' }}"
1 Like
fillwe
(Fillwe)
July 24, 2018, 7:11pm
3
Thanks @pnbruckner ! You come to rescue again What I’m having problem with is how to make it also check sensor.weeknumber for a number between 25 to 34.
In short the end product should be: If sensor.evenweek = true OR sensor.weeknumber = number between 25 to 34 I want the sensor to display Yes
Oops, yeah, I forgot that part. So back to your original, plus:
- platform: template
sensors:
garbagepickupweek:
friendly_name: Is it garbage emptying week?
value_template: >
{% set wk = states('sensor.weeknumber')|int %}
{% if is_state('sensor.evenweek', 'true') or wk >= 25 and wk <= 34 %}
Yes
{% else %}
No
{% endif %}
2 Likes
fillwe
(Fillwe)
July 25, 2018, 1:06am
5
Ah so thats how you do it! Finally starting to understand templating a little better! Works like a charm now!! You are incredible pnbruckner and really from the bottom of my heart thank you for the amazing help!
3 Likes