Another if then else question

Hi - struggling with a more complex value_template. Mrs S wants to know the colour of th enext waste collection. Even weeks = black, odd weeks = blue. I am happy with the formulas to determine this, and have just hardcoded a single check for now - my issue is - if ‘even’ then show the word ‘BLUE’ else show the word ‘BLACK’ In the example below, i have used ‘46’ instead of isEven ( as that isn’t the problem ). How do i format a simple ‘if…then…else’ and return strings?

First post - so please forgive / correct any etiquette errors

sensor 1: 
  - platform: template
    sensors:
      next_bin_colour:
         friendly_name: "Next Bin Colour"
         value_template: "{{% if now().strftime('%U') != '45' %} BLUE {% else %} BLACK' {% endif %}}"

Is it bad form to answer your own question? I just kept messing with the format. Will leave it here as a basic example. Thanks folks

      next_bin_colour:
         friendly_name: "Next Bin Colour 2"
         value_template: >
             {% if now().strftime('%U') != '45' %}
               BLUE 
             {% else %} 
               BLACK
             {% endif %}
1 Like

I don’t think it’s bad form to do what you did, you experimented with what you had and obtained an implementation that worked for you.
Well done !

What I consider bad form is when someone asks a question, a member replies giving the format, structure and all the constructs (95% of it) and the OP fills in the blank entities and then says ‘they’ solved it.

You, persevered and made some effort, that’s good in my book. :+1: :smile:

Thanks Mutt. Already hit my next problem, but will hack around considerably longer this time before posting - half the fun is in the discovery.

Cheers

1 Like

Tediore, That’s brilliant ! - I’m gonna steal that and say I made it :rofl:

@Malcolm_Smart, great sentiment but though you only joined 1 hour ago you have (self) mastered a basic template - so I have no idea where you are on the HA learning curve (it’s steep). Play a bit, but don’t be afraid to post, I for one will try to assist depending on time of day :smiley: (many others too no doubt)
Cheers

1 Like

Thanks again. Been on HA for about a week, using HASSIO on a pi. Have tasmota running, Mqtt, various things ( been a developer for years ) but really struggling with the value_template syntax - and what functions are avialable and what aren’t. Seems to be a hybrid python / proprietary language. Anyway - if you’re bored and can help - i need to try and get ‘mod 2’ or ‘% 2’ working to determine even / odd. it’s a pointless template really, but it is getting me up that curve, albeit slowly :slight_smile: Similar stuff like this, i can write my classes / methods, and reference them in the template - but it seems i have to do all the work in the template itself.

value_template: >
        {% if (int(now().strftime('%U')) % 2 == 0) %}   //if week num is even
          BLUE 
        {% else %} 
          BLACK
         {% endif %}

I need to go to bed - sorted this one. Learned two things - one - casting is not intuitive, and 2 - there is a template dev tool under developers tool - oh so much easier. Thanks for the nice welcome.

     {% set week = now().strftime('%U') %}
     {% set weekEven = (week | int) % 2  %}

The “|” filter operator takes precedence over all other operands except brackets :smile:

So this is just the same as : -

{% set weekEven = week | int % 2 %}

Note this template will not update on its own, you will need to give an entity that will update eg sensor.date

it’s called “Jinja”

Here is the general documentation:

https://jinja.palletsprojects.com/en/master/

But here is the template specific documentation which you will find way more useful:

https://jinja.palletsprojects.com/en/2.10.x/templates/

Yup - i sat staring at a dashboard waiting for it to change over a simulated dateroll. I just set up a trigger in node-red to update the state at 2am each morning. Really getting into this now.

Thanks for being them Mutt - will definitely need you around when I start hooking more items together. I’m looking at more ‘real automation’ and not just turning on off lights from my phone.

NodeRed is used by some here so you can get help on that should you need it.
I’ve never needed it and you can easily do what you want without it.
I have a completely vanilla installation and want to find what the limits are on that before expanding into other areas (I’ve not found them yet).
Post your full original automation and I’ll show you.
Yaml is pervasive throughout HA and master that and it pays dividends.
Regardless of my opinion the choice is yours and you can explore any options
Good luck

You are right about the phone thing, most people start by turning their phone/tablet/pc into a remote control for their house.
Petro (I’m sure you’ll run into him, another really good guy) believes that touching your phone is admitting defeat - write a new automation.

was going to post if i needed Node-Red or not. a few months ago i started with node-red and it was suggested i hook it up to HA - but i do really like HA - and if you think it’s all possible here, i’d rather not over complicate. I will leave the small routines i already have in Node-red and use them as a way to learn how to implement in HA.

Don’t get me wrong, and I don’t want to offend anyone for whom nodered serves a purpose, if you like drag and drop interfaces (I think it’s a stop gap for people who can’t envisage structures (:rofl: half joking here))
I don’t think that’s you though, solving a template question by yourself in less than 33 minutes

Seriously, post your code and we’ll ‘gild that lilly’ :crazy_face:

1 Like