Lovelace conditional card, based on time

Hi is it possible to show a Lovelace Card only during specific times?
I found this in the documentation https://www.home-assistant.io/lovelace/conditional/

but that only considers state or state_not.

Basically i want to display a card only during evening time.

You could still use the Conditional card mixed with and automation script where the on state is for night time and the off state for day time. Now, the only problem at the moment is that the Conditional card is broken. It should be fixed on 0.81.4.

Yes it’s possible but you’ll need to make a template sensor that has a specific state based on time.

sensor:
  - platform: time_date
    display_options:
      - 'time'
binary_sensor:
  - platform: template
    sensors:
      display_card:
        friendly_name: Display card between 8 and noon.
        entity_id: sensor.time
        value_template: >
           {% set ct = now().hour + now().minute/60 + now().second/3600 %}
           {{ 8 <= ct <= 12 }}

then in lovelace

- type: conditional
  conditions:
    - entity: binary_sensor.display_card
      state: "on"
  card:
    type: entities
    entities:
      - device_tracker.demo_paulus
      - cover.kitchen_window
      - group.kitchen
      - lock.kitchen_door
      - light.bed_light
7 Likes

Thank you very much!, That worked!

1 Like

@petro : thanks for sharing. i try to do the same with low battery level:

binary_sensor:
  - platform: template
    sensors:
      low_battery:
        friendly_name: low battery
        value_template: >-
          {{ is_state('sensor.battery_level' < 10 %)
             or is_state('sensor.battery_level_2' < 10 %)
             or is_state('sensor.hue_outdoor_motion_sensor_1_battery' < 10 %) }}

Independently of the batterylevel, the state of this binary sensor is always ‘off’. What i do wrong?

Thanks

you can’t use is_state to compare numbers

          {{ states('sensor.battery_level') | int < 10 
             or states('sensor.battery_level_2') | int < 10 
             or states('sensor.hue_outdoor_motion_sensor_1_battery') | int < 10 }}
1 Like

Thank you for your answer. Works great.
I’ve already read some of your posts. You are a great added value for the community!

1 Like

No problem, glad to help.

Although I agree that using a template is very flexible. But for a simple binary sensor with the time of day you can simply do like this

binary_sensor:
  - platform: tod
    name: Late Morning
    after: '10:00'
    before: '12:00'
2 Likes

tod didn’t exist when this thread was created

1 Like

Hi Petro, is this also possible to do with days/months? I want to hide the “christmas tree” plug if its not December/half January.

2 Likes

Hi Petro, sorry, this mathematical christmas season sensor is too hard for me to figure out. Can you please help me to understand how to convert this to a binairy sensor? I have tried:

  - platform: template
    sensors:
      christmas_season:
        friendly_name: Christmas Season (1 dec - 15 jan)
        value_template: >-
          {%- set month, week, day = 11, 4, 3 %}
          {%- set today = now().date() %}
          {%- set temp = today.replace(month=month, day=1) %}
          {%- set adjust = (day - temp.weekday()) % 7 %}
          {%- set temp = temp + timedelta(days=adjust) %}
          {%- set thanksgiving = temp + timedelta(weeks = week - 1) %}
          {%- set jan15th = temp.replace(month=1, day=15) %}

But it stayed off after 1 dec… :frowning:

Can you give me a hint or maybe please point out where it could be wrong?

I am now using this and that seems to work well:

- platform: template
    sensors:
      christmas_time_2:
        friendly_name: Christmas Season (1 dec - 15 jan) - 2
        value_template: >
          {% set today = states('sensor.date').split('-') %}
          {% set month = today[1]|int %}
          {% set day = today[2]|int %}
          {{ month == 12 and day >= 1 or month == 1 and day <= 10 }}

It’s already a binary sensor… you literally just need to copy and paste it into the template section.

you didn’t copy the template correctly… you’re missing the last line. As for the template itself, what does the first line of the template say? set month, week, day… so if you want December first, what month is that? and what week would the first day be in?

What line?

You have to copy the code from the link, not the snippit thats showing in the preview. Common man.

S rr , I d d not unders nd th t I ne d d to c py a l th c nt nt of an th r l nk. Ne t t me I will do be er.

I think you will be a good teacher! You remind me of a a professor when I was young.

@petro don’t forget that I am an overenthusiastic HA user. I do know a lot (I think) but I have major problems with the format of ymal/json coding, especially template formatting. Templates are my weak point, and I appreciate all your help (again - think I am only saying this to you since you a lecturing me as if I am at school).

Thanks for making me feel young again.

I observed problems with this.
Prefer not using it.

@petro has some slick templating in the github repository, especially this one based on the Thanksgiving Holiday!

For anyone that needs a simple date condition for the Christmas Holiday, this is what I came up with to have a simple trigger for dates between 11/1 and 1/15.

Thank you @petro for the references.

template:
  - binary_sensor:
    - unique_id: christmas_season
      name: Christmas Season
      state: >
        {%- set today = now().date() %}
        {%- set nov1st  = today.replace(month=11, day=1) %}
        {%- set jan15th = today.replace(month=1, day=15) %}
        {{ today <= jan15th or today >= nov1st }}
      attributes:
        template: seasons