Using 'Month' in a condition

I want an automation to run on specific months. It seems you cannot do this simply in HA (what an oversight!). I’ve got this far but got stuck. I’ve created a sensor to identify the current month;

  • platform: template
    sensors:
    current_month:
    value_template: “{{now().month}}”
    friendly_name: ‘Current Month’

And I’m now trying to use this as a part of a condition in an automation;
- condition: numeric_state
entity_id: sensor.current_month
state:
- 1
- 2
- 3
- 4
- 10
- 11
- 12

However, this doesn’t work. What am I doing wrong?

Just in case anyone gets stuck here. I solved this by using a template. Here’s the condition which works.

- condition: template
  value_template: '{{ now().month >= 10 or now().month <= 4 }}'
16 Likes

Sorry for reviving an old thread. I tried to use this condition on the Christmas lights. I want them to trigger in December and January, and I put this in as a condition:

condition: template
value_template: '{{ now().month == 1 or now().month == 12 }}'

I am using the UI but editing the condition as YAML. When I hit “Execute”, the lights still turn on, even though now is February. What’s wrong?

1 Like

Not sure if months are 0-based (january = 0) or 1-based (january = 1).

And that will Execute the actions, that is the purpose of the button.
If you want to trigger the automation then edit the trigger to something you can control.
Like an input_boolean for example.

Like already said the conditions are ignored in that case.
When you trigger the automation from the dev-tolls you can set “skip_condition” to “false”.

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