Template sensor with number range, help wanted please!

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 :frowning:

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 :frowning:

  - 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 :sweat:

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

Thanks @pnbruckner! You come to rescue again :sweat_smile: 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 :confused:

Oops, yeah, I forgot that part. :slight_smile: 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

Ah so thats how you do it! Finally starting to understand templating a little better! Works like a charm now!! You are incredible pnbruckner :grinning: and really from the bottom of my heart thank you for the amazing help!

3 Likes