Automation During Date Range

Is there a way to start an automation between January 8th and December 9th, then in different months and days?
thank you

Is correct?

  condition:
  - condition: template
    value_template: >
      {% set n = now() %}
      {{ 12 <= n.month <= 1 and n.day == 8 }}

That certainly is not correct. The number of the month cannot be both greater than or equal to 12 at the same time as being less than or equal to 1.

It’s not exactly clear what you want. Do you want the automation to be able to run on January 8th, and December 9th, or any day between them (i.e., after January 8th but before December 9th)?

Or do you want the opposite (i.e., on December 9th, and January 8th, or any day between them – after December 9th but before January 8th)?

automation should work between the days from 8 December to 8 January of the following year, every day, and another that should work on 9 January to 7 December of the same year for each day. thank you

Here’s one way you could do this for the first automation:

condition:
  condition: template
  value_template: >
    {% set n = now() %}
    {{ n.month == 12 and n.day >=8
       or n.month == 1 and n.day <= 8 }}

And for the second:

condition:
  condition: template
  value_template: >
    {% set n = now() %}
    {{ not (n.month == 12 and n.day >=8
            or n.month == 1 and n.day <= 8) }}
7 Likes

Is this for all the day between range?

1 Like

Tom, I googled date range as I was looking for a solution for my Christmas Lights!!! Thanks for your post

I actually like Phil’s solution better and am using it now.

- id: xmas_lights_on
  alias: 'Xmas Lights On'
  trigger:
    platform: numeric_state
    entity_id: sensor.outside_light_level
    below: 660
  condition:
  - condition: sun
    after: sunrise # Active 4 hours after sunrise (prevents false triggering at dawn)
    after_offset: "+04:00:00"
  - condition: template # Only between December 1 and January 6.
    value_template: >
      {% set n = now() %}
      {{ n.month == 12 or ( n.month == 1 and ( 1 <= n.day <= 5 )) }}
  action:
    service: switch.turn_on
    entity_id: switch.xmas_tree_lights

- id: xmas_lights_off
  alias: 'Xmas Lights Off'
  trigger:
  - platform: time
    at: '02:00:00'
  - platform: state
    entity_id: binary_sensor.dark_outside
    to: 'off'
  condition:
    condition: state
    entity_id: switch.xmas_tree_lights # only if the lights are on
    state: 'on'
  action:
    service: switch.turn_off
    entity_id: switch.xmas_tree_lights
3 Likes

Thanks Phil, used your condition template and it works a treat

1 Like

A simpler concept, but same principle…

I’m trying to turn on a Christmas tree at 5AM and turn it off at 10PM. A couple options…?

  1. Use the switch.toggle option, and somehow make sure no one messes with it manually and gets it out of sequence…

  2. Use two separate automations one that triggers on at 5AM, one that triggers off at 10PM

  3. (probably a smarter way!)

i thought i saw that i could use a condition template similar to what you are doing above, but i dont know how to ‘action’ it to do an on when the condition is met and an off when not.

thoughts? thanks

Do option 2. It’s simple and easy and not much configuration.

Using two automations is definitely simpler, and you get the added benefit of being able to individually turn them on or off if you like. But, for completeness, you can do it in one automation:

trigger:
  - platform: time
    at: 05:00:00
  - platform: time
    at: 22:00:00
action:
  - service_template: >
      switch.turn_{{ 'on' if trigger.now.hour == 5 else 'off' }}
    entity_id: switch.SWITCH
2 Likes

Very nicely done. Im going to have to look closer at template syntax in the future. Is it right that the template stuff is tasmota driven? Whereas all the code around it is home assistant specific?

What language does tasmota talk vs HA?

Really appreciate the help!!

Phils Template is pure Home Assistant. Tasmota has ‘rules’ you can use however I only use Home Assistant stuff with all mine.

Tasmota communicates with HA via MQTT.

1 Like

Ok. Thanks for that quick response Dave. Ive just seen some template type stuff in a field on the tasmota flashed sonoff basics, which made me think it was tasmota scripting vs HA.

Are home assistant templates python, perl, bash, or something else? Or just their own special home grown flavor?

The template stuff for Tasmota is entered in Tasmota to set the device type on a once-off basis. Nothing to do with HA templates… completely different function.

Home Assistant generally uses Jinja templates but some addons/custom-cards use Java. You can also use scripts (I have used bash and Python but I assume you could use Perl too - the trick is to make sure the used commands are supported by the Home Assistant shell)

I see in configuration | other in the sonoff/tasmota there is is this below, under template, but template isn’t active…

{“NAME”:“Generic”,“GPIO”:[255,255,255,255,255,255,255,255,255,255,255,255,255],“FLAG”:15,“BASE”:18}

what is this stuff for?

Also, what scripting language is HA most like? Python/Perl/bash?

thanks.

The template sets the GPIO Port outputs. 255=user and that is a Generic sonoff and not active as per what you say. If you set the module type, it will overide that template.
See here:
Here is my nodemcu:


It has these GPIO Settings:

I configured it using this template:

{"NAME":"NodeMCU","GPIO":[0,0,0,0,6,5,0,0,0,0,0,0,0],"FLAG":0,"BASE":18}

On the Template page you can click on the dropdown menu for each GPIO to see what the options are for that pin.

As well as the preconfigured modules, there is a site you can download templates for pretty much any device or you can roll your own as I have done here.

HA uses Python scripting.

As @DavidFW1960 said, HA templates are written in Jinja2. But note that you can use a lot of Python within Jinja2 templates as well.

HA has two main scripting choices. HA’s native Scripts and fairly restricted Python Scripts. HA also lets you call Shell commands, so…

honestly… this is french to me. GPIO’s are the places i can solder wires on the device i’m using, correct? when you define GPIO4 and 5 above, what kind of data/function is actually moving through these pins? how did you know what an SDA and an SCL are? (yeah. pretty noob!).

Also, why is Generic (18) used? on my sonoff-basic there is an option for that… but i’m on generic (18) as well instead of that. mine are all 255?

it works in HA, but i’d just like to know the why… (why are those two red?) i have a lot of why’s!

Well you see SCL and SDA - those pins enable me to connect i2c devices and they are connected physically to those GPIO’s (See the temp/pressure/humidity/illuminance)

Yours is set to Generic (18) as the module type because that is the default and you have not selected the correct module type. 255=user is the default!
No idea why 9/10 are red.
If you go to configure>configure module you can select the correct module type and it won’t say generic anymore.