Value Template Assistance

I have a value template someone helped me with to run an automation on the first of every month. This code is Greek to me. Can any coding guru’s help me modify this to run every three months? Maybe January 1, April 1, July 1, October 1…

What I have now…

{% set n = now() %} {{ n.day == 1 and 1 <= n.month <= 12 }}

Also, anybody know of a good reference for this? Like a “Yaml For Dummies”? Or whatever code this is? I’d like to learn more about this for the future.

Thanks,
Steven

I think it should be

{% set n = now() %} {{ n.day == 1 and n.month in [1,4,7,10] }}

It says n is the current time.

Then it said n.day (the day number of right now) is 1.
Then it says n.month (the month right now) is Jan, apr, July, oct in numbers.

2 Likes

Awesome, I’ll try this… Thanks a lot for the quick reply!

Steven

It’s Jinja2, a templating engine employing python syntax. To learn more, I suggest Home Assistant’s documentation for Templating.

The official Jinja2 reference is a bit daunting for a novice; the parts I would focus on are the Built-in Filters and Tests.

The comparison operators used in your template are from python.

1 Like

Okay, since I know this is related… I have made cards that conditionally show when the UV Index reaches a certain value. The idea is that if the UV Index reaches between 6 and 7, for example, a card will show with a sunscreen recommendation.

Sunscreen

The problem with this is that the card only shows if it’s value is precisely 6. Right now it’s 6.16 so it doesn’t show. I know this must be done with a template somehow, but I’m not sure how to go about it. I need to create an entity that returns a value based on a range (I’m thinking), and then base the condition on that entity. So if the value is between 6.00 and 6.99 the entity would show “6”. Between 7.00 and 7.99 the entity would show “7”, etc…

How would you all go about doing that?

Thanks for the suggestions…
Steven

Look at template sensors, and then use the built in round filter - something like:

template:
  - sensor:
      - name: UV Index Rounded
        unit_of_measurement: "UV"
        state: "{{ states('sensor.uv_index')|float(0)|round(0) }}"
2 Likes

Thank you, this works perfectly!

Until cards support basic features like if a condition is between value A and value B, this type of configuration variable is essential.

There is an alternative:

template:
  - binary_sensor:
      - name: UV Threshold Alert
        state: >
          {{ "on" if states('sensor.uv_index')|float(0) > 5.9 else "off" }}
        unique_id: binaryalert58ec4d9a65d5456ca33e44204b45b1fb

Now you have a binary_sensor that switches on when the UV index is 6 or above, and off when it is below. You can now use the built in Home Assistant conditional card to show the relevant cards now, based on whether the binary_sensor is on or off.

1 Like

I have all the cards built, I just created a sensor like in your first post. The rounded value from your first example is what I use as the condition now, and it works great from what I can tell. I’ll have to get a few sunny days and see how it functions.

I appreciate the help a great deal. :+1:

Okay, I tried to add a specific time to that. No dice. I tried all kinds of variants such as this.

{% set n = now() %} {{ n.day == 25 and n.time == 1125 and n.month in [9] }}

Hoping to have something run on a specific date/time each year, like at 11:25 AM on September 25th. I still can’t believe this isn’t included as a condition in a home automation platform, but I digress. I’d like to set my furnace mode based on specific dates/times each year, like turn it on every October 1st, and off come spring.

Thanks for the suggestions…
Steven

A basic template you could use as a condition would be:

{{ now() == (now().year~"-09-25 11:25:00") | as_datetime | as_local }}

There are at least two ways to trigger at a specific time on a given month and day using core components.

Time Trigger
trigger:
  - platform: time
    at: "11:25:00"
condition:
  - condition: template
    value_template: "{{ now().month == 9 and now().day == 25 }}"
Template Trigger
trigger:
  - platform: template
    value_template:  '{{ now() >= (now().year~"-09-25 11:25:00") | as_datetime | as_local }}'

Note: I tend to use >= in template triggers like this instead of == just to cover the edge case where the trigger time doesn’t register exactly, but it may not be necessary.

You could also use a Calendar trigger if you have integrated calendars or use a custom integration like Anniversaries to store the date.

1 Like

Thanks a bunch…

I put in a specific time as the trigger, and used the above as a condition as a workaround.

{% set n = now() %} {{ n.day == 25 and n.month in [9] }}

I put what you said in the template editor and when the date time ticked by it still said 'false". I’m assuming because the condition is only true for one second… That’s perfect if that’s the case. I’ll test it out and see how it works. :+1: :+1:

Thanks again!

You can compare tuples.

{{ (now().month, now().day) == (9,25) }}

1 Like

There’s a new word in my vocabulary… “Tuples”. :smiley: