Using 'Month' in a condition

As mentioned by others, manually triggering an automation will only execute the action (the trigger and condition are skipped). Use dennis84de’s suggestion to prevent skipping the condition:
Screenshot from 2021-02-24 08-06-13

Anyway, I didn’t post just to repeat what others have already stated but to mention that you can streamline your template like this:

condition: '{{ now().month in [1, 12] }}'

It appears on one line because it employs the shorthand notation for Template Conditions. In addition it checks if the current month exists in a list of months. This is a more compact way, especially if there are several possible valid months.

3 Likes

Hey everybody. I have been a Home Assistant user for a few hours and already the friendliness in this community is overwhelming. Thank you all who replied to my question - not only solving my misunderstanding, but even providing a better solution.

2 Likes

I’m sorry to revive this but I need my covers only to close during the spring/summer months

The trace gives me this

Executed: 20 April 2022, 19:30:00
Result:

result: false
entities: []

The condition looks like this:

condition: template
value_template: 'condition: '{{ now().month in [4,5,6,7,8,9,10] }}'

Trying the new “Test” Button results in

Error occurred while testing condition

template value should be a string for dictionary value @ data[‘value_template’]. Got None

When i understand the docs right it should be:

or if you have other conditions:

  condition:
    - "{{ now().month in [4,5,6,7,8,9,10] }}"
    - condition: template
      value_template: '{{ now().day in [1,2,3] }}'
1 Like
condition: template
value_template: 'condition: '{{ now().month in [4,5,6,7,8,9,10] }}'
# remove this     ^ that shouldn't be there.

So to clarify:

condition: template
value_template: '{{ now().month in [4,5,6,7,8,9,10] }}'
2 Likes

It still says that the template value should be a string…

condition: template
value_template: '{{ now().month in [4,5,6,7,8,9,10] }}'

Have you tried quoting around

 "{{ now().month in [4,5,6,7,8,9,10] }}"

Yeah, doesn’t make a difference.

What is the other condition you have there?
Post the full yaml.

I should clarify that the OR-condition with seasons didn’t use to be there, it’s a part of me trying to get this to work at least a little bit.
All the other conditions tests green where expected (it’s not summer so that won’t turn green)

alias: Close Cover at bedtime
description: ''
trigger:
  - at: '19:30'
    platform: time
condition:
  - condition: template
    value_template: ' "{{ now().month in [4,5,6,7,8,9,10] }}"'
  - condition: state
    entity_id: input_boolean.manual_control_barnensrum
    state: 'off'
  - condition: or
    conditions:
      - condition: state
        entity_id: sensor.season
        state: spring
      - condition: state
        entity_id: sensor.season
        state: summer
action:
  - data: {}
    entity_id: cover.rullgardin_barnens_rum
    service: cover.close_cover
  - data: {}
    entity_id: cover.rullgardin_sovrummet
    service: cover.close_cover
  - service: cover.close_cover
    target:
      entity_id: cover.rullgardin_allrum
    data: {}
mode: single

You shouldn’t be using both types of quotes.

  - condition: template
    value_template: '{{ now().month in [4,5,6,7,8,9,10] }}'

That’s what it looks like when I copy it from the GUI.
Even if I have the single quote it doesn’t work. It’s been as in this post all the time.

I tested the code before posting and it worked fine for me

I created a test code (below) and it ran correctly. Here is the auromation

- id: "Test Monthly"
  alias: "Test Monthly"
  description: ''
  trigger:
    - platform: time
      at: '11:13'
  condition:
    - condition: template
      value_template: "{{ now().month in [4,5,6,7,8,9,10] }}"
    - condition: or
      conditions:
        - condition: state
          entity_id: sensor.seasons
          state: 'spring'
        - condition: state
          entity_id: sensor.seasons
          state: 'summer'
  action:
    - service: light.turn_on
      target:
        entity_id: light.study_cans_current_value
  mode: single
3 Likes

There are open issues for templates and zones in the condition tester.

This seems to just be an issue with the test button, it does not actually affect the function of automations.

1 Like

So why isn’t yesterday part of the month of april?

Executed: 20 April 2022, 19:30:00
Result:

result: false
entities: []

You keep giving us bits and pieces.
The only time you actually posted the full automation then there was a clear error.

Two have now confirmed the code works, as long as you keep posting just snippets of automations and traces then it’s very hard to see the issue.

can you go to automations and click on the timer on the right hand side.

so we can see something like this

And please post your final yaml code as well as your results from automations.

It actually triggered yesterday.

from what I can see from the history the code looks the same, but maybe the Visual interface puts some extra " ’ or something that breaked it.

Thanks for going through the troubleshooting above because he helped me implement this and work through the same “test button” error myself.

The template condition for months is working perfectly.

1 Like