Type: button only in a certain period of time

I have the following code to show an action button:

type: custom:state-switch
entity: user
default: default
states:
  Ich:
    type: button
    icon: mdi:weather-night
    entities:
      - input_button.gute_nacht
    tap_action:
      action: call-service
      service: automation.trigger
      service_data:
        entity_id: automation.gute_nacht

My question is, can I show this button only in a certain period of time for example between 22:00 - 24:00 o clock?

There is the conditional card:

But that will only look at 1 condition and not a range so you will have to work around that.
Simplest way i think would be a template binary sensor that uses time for the value template.

template:
  - binary_sensor:
    - name: "button_name_visible" 
      state:'{{ '22:00' <= states.sensor.time.state <= '23:59' }}'

Then you can use this entity as the condition for the conditional card.
You do need to use the time_date integration:

There is another way to use time but i found using this sensor so much easier to use in these kind of situations.

1 Like

Thank you, but now I have a problem with this.

I put the following in the sensor.yaml:

  - platform: time_date
    display_options:
      - 'time'

Restart my HA

Then I would like to put the following code in the cutomation.yaml:

template:
  - binary_sensor:
    - name: "button_name_visible" 
      state:'{{ '22:00' <= states.sensor.time.state <= '23:59' }}'

I got this error:
can not read an implicit mapping pair; a colon is missing at line 176, column 67
… sensor.time.state <= ‘23:59’ }}’

The line 176 is:
state:’{{ ‘22:00’ <= states.sensor.time.state <= ‘23:59’ }}’

What I make wrong?

You did nothing wrong. The code i gave you was wrong. The times should be in " instead of ’
Something i have to wrap my head around, it resolves correctly in the dev tools but fails here.

So it should be:

template:
  - binary_sensor:
    - name: 'button_name_visible'
      state: '{{ "22:00" <= states.sensor.time.state <= "23:59" }}'

Edit: I now see why its wrong. The code i gave single quoted the brackets.
So jinja saw:
'{{ '
' <= states.sensor.time.state <= '
' }}'

No problem :blush: Now it works.

Thank you so much

No problem, but you marked yourself as the solution. And that post still has the error in it :smiley:

Oh, I have correct it :blush:

Sorry I have lost my ready code and now I can’t get the code anymore.

I have this code again:

type: custom:state-switch
entity: user
default: default
states:
  Ich:
    type: button
    icon: mdi:weather-night
    entities:
      - input_button.gute_nacht
    tap_action:
      action: call-service
      service: automation.trigger
      service_data:
        entity_id: automation.gute_nacht

I have create this button:
button_anzeigen

And I don´t know to add this part into my code:

type: conditional
conditions:
  - entity: button_anzeigen
    state: "on"

You make the conditional card and in it you put the custom:state-switch card.

( rtfm :stuck_out_tongue: )

type: conditional
conditions:
  - entity: button_anzeigen
    state: "on"
card:
  type: custom:state-switch
  entity: user
  default: default
  states:
    Ich:
      type: button
      icon: mdi:weather-night
      entities:
        - input_button.gute_nacht
      tap_action:
        action: call-service
        service: automation.trigger
        service_data:
          entity_id: automation.gute_nacht

I have try many thinks, maybe I have put one space to mutch.

Thank you :blush:

But now I have one more question.

How can I add to this code that it is only displayed on Wednesday and Friday?

If your timeslot is still the same then its just a question of adding another condition to the template. In this case an AND / OR with now().weekday() that will give you the week of the day. With 0 being Monday and 6 being Sunday. So 2 and 4 for Wednesday and Friday

'{{ ("20:00" <= states.sensor.time.state <= "23:59") and (now().weekday() == 2 or now().weekday() == 4)}}'

Without the timeslot:

'{{ now().weekday() == 2 or now().weekday() == 4}}'

Ok, I have try something.

With the following code:
configuration.yaml:

template:

  • binary_sensor:
    • name: ‘button_anzeigen_fertig_schiessen’
      state: ‘{{ (“15:30” <= states.sensor.time.state <= “19:30”) and (now().weekday() == 2 or now().weekday() == 4)}}’

Button:

type: conditional
conditions:
  - entity: button_anzeigen_fertig_schiessen
    state: 'on'
card:
  type: custom:state-switch
  entity: user
  default: default
  states:
    Manuel:
      type: custom:mushroom-template-card
      primary: Bin fertig mit Schiessen
      icon: mdi:bow-arrow
      layout: vertical
      icon_color: green
      tap_action:
        action: call-service
    confirmation:
      text: Nachricht wirklkich schicken?
      service: automation.trigger
      service_data:
        entity_id: automation.sprachansage_bogenschiessen_bin_fertig

I saw the button in the edit mode. If I close the editmode the button is no longer displayed.

The state of “button_anzeigen_fertig_schiessen” is at the moment on.

What make I wrong?

Shouldnt that be something like:

  - entity: binary_sensor.button_anzeigen_fertig_schiessen

I’m really embarrassed now :see_no_evil: I’ll try that