Skip to content

Instantly share code, notes, and snippets.

@luixal
Last active April 19, 2024 10:23
Show Gist options
  • Save luixal/be78d372eefbd0637027c2945f34c7dc to your computer and use it in GitHub Desktop.
Save luixal/be78d372eefbd0637027c2945f34c7dc to your computer and use it in GitHub Desktop.
Mushroom Countdown with Input Hours and Scheduling

Mushroom Countdown with Input Hours and Scheduling

This card shows an hours input card while charger is off and a countdown when charger is running for the hours selected.

Also, it shows a popup for scheduling running hours at certain time by long tap in the card.

Screenshots

image

image

image

Dependencies

Cards needed:

  • Mushroom cards (mushroom-number-card)
  • Timer Bar Card
  • Browser mod (for the popup)

Other things needed:

  • Input number: input_number.ev_charge_timer_input
  • Timer helper: timer.ev_charge_timer
  • Datetime helper: input_datetime.ev_charge_scheduled_init_time
  • Input number: input_number.ev_charge_scheduled_timer_input (why? so you can schedule and still use it manually in the meantime)

IMPORTANT: when creating the helpers, check the restore option or you'll be in trouble when restarting HA while this is working!

Automations

You need to create some automations to make this card work as expected. As my integrations have entity_ids and can't be used as is in other HAs, let's just explain them a little bit:

Start on input number change

When the input number is changed, the timer can be started using a template like this:

service: timer.start
data_template:
  duration: "{{ states.input_number.ev_charge_timer_input.state | int }}:00:00"
  entity_id: timer.ev_charge_timer

Toggle switch

A simple automation that turns on/off the swtich when timer events are fired, like this:

  • When timer.started --> switch on
  • When timer.finished --> switch off

Or you can just toggle the switch on any of those events.

Manually turn off

The cards allows to turn off the switch when you tap on it while running, but it doesn't finishes the timer o resets the input number. To make this work, add another automation that is fired when the switch if turned off and makes two things:

  • Sets the input number helper to zero
  • Finishes the timer helper

Scheduled Execution

For running the scheduled execution, you need to create an automation that gets fired when time is equal to the input datetime, using a trigger like this:

platform: time
at: input_datetime.ev_charge_scheduled_init_time

and does the following:

  • Checks if the init time helper has a valid value:
condition: template
value_template: "{{ states('input_datetime.ev_charge_scheduled_init_time') != '00:00:00' }}"
  • Sets the scheduled input value in the input number helper, like this:
service: input_number.set_value
data_template:
  entity_id: input_number.ev_charge_timer_input
  value: "{{ states('input_number.ev_charge_scheduled_timer_input') }}"

After this, some of the previous automation kick in and runs as if time was a manual input in the card.

Reset Scheduled Execution

This automation is run to reset the scheduled execution if stopped manually. It has no trigger as it's fired manually or by other automations and make two things:

  • Resets the number input:
service: input_number.set_value
data:
  value: 0
target:
  entity_id: input_number.ev_charge_scheduled_timer_input
  • Resets the datetime input:
service: input_datetime.set_datetime
data:
  time: "00:00:00"
target:
  entity_id: input_datetime.ev_charge_scheduled_init_time

Lovelace card config

Always visible card

- type: conditional
  conditions:
    - condition: numeric_state
      entity: input_number.ev_charge_scheduled_timer_input
      above: 0
  card:
    type: custom:mushroom-template-card
    primary: >-
      Carga programada a las {{
      states('input_datetime.ev_charge_scheduled_init_time')[:-3] }}
    secondary: >-
      La carga durará {{
      states('input_number.ev_charge_scheduled_timer_input')|int }}
      horas
    icon: mdi:timer
    icon_color: white
    fill_container: true
    multiline_secondary: false
    hold_action:
      action: none
    double_tap_action:
      action: none
    tap_action:
      action: call-service
      service: automation.trigger
      target:
        entity_id: automation.ev_charge_scheduled_reset
      data:
        skip_condition: true
    style: |
      ha-card {
        background: #4090f0;
      }
- type: conditional
  conditions:
    - entity: timer.ev_charge_timer
      state: idle
  card:
    type: custom:mushroom-number-card
    entity: input_number.ev_charge_timer_input
    name: Horas Carga
    fill_container: true
    display_mode: buttons
    layout: horizontal
    secondary_info: none
    icon_color: grey
- type: conditional
  conditions:
    - entity: timer.ev_charge_timer
      state_not: idle
  card:
    type: custom:timer-bar-card
    entity: timer.ev_charge_timer
    name: Coche Cargando...
    icon: mdi:ev-station
    mushroom:
      color: green
    tap_action:
      action: call-service
      service: timer.finish
      data:
        entity_id: timer.ev_charge_timer
    card_mod:
      style: |
        ha-card {
          background-color:#bce1b9
        }
        @media (prefers-color-scheme: dark) {
          ha-card {
            background-color:#344635
          }
        }

Pop up scheduling card

Just place this card somewhere in your dashboard. It will be shown after a long press in the other card.

- type: custom:popup-card
  entity: input_number.ev_charge_timer_input
  title: Carga Programada
  dismissable: true
  card:
    type: vertical-stack
    cards:
      - type: custom:button-text-card
        title: Carga Programada
        subtitle: Establece hora de inicio y horas de carga
        icon: mdi:car-electric
        icon_size: 34
      - type: custom:time-picker-card
        hour_mode: 24
        hour_step: 1
        minute_step: 5
        layout:
          hour_mode: double
          align_controls: right
          name: inside
          embedded: false
          thin: true
        hide:
          seconds: true
          name: false
          icon: false
        entity: input_datetime.ev_charge_scheduled_init_time
        name: Hora Inicio
        link_values: true
      - type: custom:mushroom-number-card
        entity: input_number.ev_charge_scheduled_timer_input
        display_mode: buttons
        layout: horizontal
        name: Horas Carga
        secondary_info: none
        icon_color: grey
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment