TM1637 expose a switch for turning off the display (Software)

Hello,
I have searched a lot and tried different things but couldn’t manage to do it.
I want to expose a switch to Home Assistant from ESPhome to turn on and off the TM1637.
I can do this by soldering a transistor to 5V and GPIO, then exposing this to Home Assistant. But this is too much trouble. Is there a way to do this using only software?
I have tried below config but apparently library doesn’t support this:

display:
    platform: tm1637
    id: tm1637_display
    clk_pin: 25
    dio_pin: 26
    intensity: 0
    update_interval: 5000ms
    lambda: |-
      static int i = 0;
      i++;
      if ((i % 2) == 0)
        it.printf("%3.0fP", id(humd).state);
      else
        it.printf("%.1f~", id(temp).state);
      

switch:
  - platform: template
    name: "Display Switch"
    lambda: |-
      if (id(tm1637_display).state) {
        return true;
      } else {
        return false;
      }
    turn_on_action:
      - lambda: id(tm1637_display).turn_on();
    turn_off_action:
      - lambda: id(tm1637_display).turn_off(); 

I’d set up a global variable that you set in your turn_off_action that you check in your lambda for display and know if you should output a value or just a blank string.

Damn , thanks for the help. It was spot on. For future reference below is the code:

globals:
  - id: display_status
    type: bool
    restore_value: no
    initial_value: 'true'

display:
    platform: tm1637
    id: tm1637_display
    clk_pin: 25
    dio_pin: 26
    intensity: 0
    update_interval: 5000ms
    lambda: |-
      if (id(display_status) == true) {
        static int i = 0;
        i++;
        if ((i % 2) == 0)
          it.printf("%3.0fP", id(humd).state);
        else
          it.printf("%.1f~", id(temp).state);
      } else {
          printf("    ");
        }

switch:
  - platform: template
    name: "Display"
    lambda: |-
      if (id(display_status) == true) {
        return true;
      } else {
        return false;
      }
    turn_on_action:
      - globals.set:
          id: display_status
          value: 'true'
    turn_off_action:
      - globals.set:
          id: display_status
          value: 'false'
2 Likes

Thanks a lot for the above. I hope I can get an answer to my question:

Got my TM1637 up and running but I would like to dim the display intensity according to the local time. During the day the display intensity should be 7, during the evening and night the intensity should be 1.

Is this even possible? I hope that someone or maybe @laxarus or @pcon can help me out :slight_smile: !

How I would do it is to create a template binary_sensor in HA that consults the current time and compares it against two input_datetime fields to have a binary_sensor.dim_display. Then import that into ESPHome.

After you have that in your device, you can create a lambda for intensity to return the right value based on the on/off state of your binary_sensor.dim_display.

Alternatively, you could have HA generate a template sensor that just returns the intensity you want based on time of day, sun position, etc and just pipe that directly into your intensity lambda.

You can expose the intensity to HA by using global variables and create an automation in HA to adjust the intensity.
I use this to achieve a similar effect.
Something like this:

globals:
  - id: display_status
    type: bool
    restore_value: true
    initial_value: 'true'

  - id: display_mode_global
    type: int
    restore_value: true
    initial_value: '1'

number:
  - platform: template
    id: display_intensity 
    name: "Intensity (0-7)"
    icon: mdi:brightness-5
    optimistic: true
    initial_value: 0
    min_value: 0
    max_value: 7
    step: 1
    update_interval: 4000ms
    restore_value: true
    on_value:
      then:
        - component.update: tm1637_display
display:
    platform: tm1637
    id: tm1637_display
    clk_pin: 25
    dio_pin: 26
    update_interval: 500ms
    lambda: |-
      it.set_intensity(id(display_intensity).state);
      if (id(display_status) == false) {
        it.printf("    ");
      }
      else {
        static int i = 0;
        static int j = 0;
        i++;
        j++;
        switch (id(display_mode_global)) {
          case 1:
            it.printf("%.1f~", id(temp_s).state);
            break;
          case 2:
            it.printf("%3.0fP", id(humd_s).state);
            break;
          case 3:
            if ((i % 20) > 10)
              it.printf("%3.0fP", id(humd_s).state);
            else
              it.printf("%.1f~", id(temp_s).state);
            break;
          case 4:
            if ((i % 2) == 0)
              it.strftime("%H.%M", id(homeassistant_time).now());
            else
              it.strftime("%H%M", id(homeassistant_time).now());
            break;
          case 5:
            if ((i % 20) > 10)
              if ((j % 2) == 0)
                it.strftime("%H.%M", id(homeassistant_time).now());
              else
                it.strftime("%H%M", id(homeassistant_time).now());
            else
              it.printf("%.1f~", id(temp_s).state);
            break;
          default:
            if ((i % 20) > 10)
              it.printf("%3.0fP", id(humd_s).state);
            else
              it.printf("%.1f~", id(temp_s).state);
        }
      }

the cases defined in the display block is for selecting the different modes. Don’t let them confuse you.

it.set_intensity(id(display_intensity).state);

lets you select the intensity based on the global variable

This way when you add the esp device to HA, you will have a select switch ready to adjust intensity whenever you want.

Guys, @pcon @laxarus you’re the best! Did get it to work like I would. Thank very much!!

Got me thinking… I already added a switch to turn on and of the display like so:

switch:
  - platform: template
    name: "Klok"
    icon: "mdi:clock-digital"
    lambda: |-
      if (id(display_status) == true) {
        return true;
      } else {
        return false;
      }
    turn_on_action:
      - globals.set:
          id: display_status
          value: 'true'
          
    turn_off_action:
      - globals.set:
          id: display_status
          value: 'false'

Is it also posible to add this switch as an light in HA with a dimmable function? I’m asking this because it would be nice to get the clock “Klok” into HomeKit as a light from where I could dim it.

It should be possible to use template light to create a light entity from HA using the values from the intensity.

Though I am not sure how to do that purely using the esphome.

Haha yes, it should be possible… To be honest, i tried but I realy don’t know how.

Hi, interesting code!
How to you switch between the different modes ? “Case”