If then on interval in ESPHome

Good morning,
In ESPHome, I have a 64*64 MATRIX screen and I have configured 2 display pages on it.
I have configured an interval of 5 seconds for the display so that each page is displayed alternately but I would like to put a condition on it.
That is to say, activate the interval sequence only if printing is in progress, otherwise only display the first page.
Below is my current code:

esphome:
  name: "esp-matrix"
  friendly_name: ESP-Matrix

external_components:
  - source: github://TillFleisch/ESPHome-HUB75-MatrixDisplayWrapper@main

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "***************"

ota:
  password: "****************"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  manual_ip:
    static_ip: 192.168.1.121
    gateway: 192.168.1.1
    subnet: 255.255.255.0
    dns1: 192.168.1.1

  ap:
    ssid: "Esp-Test Fallback Hotspot"
    password: "************"

captive_portal:
    
# codage
sensor:
  - platform: wifi_signal
    name: "WiFi Signal"
    update_interval: 10s

  - platform: homeassistant
    id: meteo
    entity_id: weather.chelles
    attribute: temperature

  - platform: homeassistant
    id: x1c_pourcentage
    entity_id: sensor.x1c_00m09c422100198_progression_de_l_impression

  - platform: homeassistant
    id: etat_impression
    entity_id: sensor.x1c_00m09c422100198_etat_de_l_impression

time:
  - platform: homeassistant
    id: esptime

text_sensor:
  - platform: homeassistant
    id: temps
    entity_id: weather.chelles

  - platform: homeassistant
    id: finish_print_date
    entity_id: sensor.impression_3d_5

  - platform: homeassistant
    id: finish_print_heure
    entity_id: sensor.impression_3d_6


font: 
  - file: "/config/esphome/materialdesignicons-webfont.ttf"
    id: icon
    size: 15
    glyphs: &mdi-weather-glyphs
      - "\U000F0590" # mdi-weather-cloudy
      - "\U000F0F2F" # mdi-weather-cloudy-alert
      - "\U000F0E6E" # mdi-weather-cloudy-arrow-right
      - "\U000F0591" # mdi-weather-fog
      - "\U000F0592" # mdi-weather-hail
      - "\U000F0F30" # mdi-weather-hazy
      - "\U000F0898" # mdi-weather-hurricane
      - "\U000F0593" # mdi-weather-lightning
      - "\U000F067E" # mdi-weather-lightning-rainy
      - "\U000F0594" # mdi-weather-night
      - "\U000F0F31" # mdi-weather-night-partly-cloudy
      - "\U000F0595" # mdi-weather-partly-cloudy
      - "\U000F0F32" # mdi-weather-partly-lightning
      - "\U000F0F33" # mdi-weather-partly-rainy
      - "\U000F0F34" # mdi-weather-partly-snowy
      - "\U000F0F35" # mdi-weather-partly-snowy-rainy
      - "\U000F0596" # mdi-weather-pouring
      - "\U000F0597" # mdi-weather-rainy
      - "\U000F0598" # mdi-weather-snowy
      - "\U000F0F36" # mdi-weather-snowy-heavy
      - "\U000F067F" # mdi-weather-snowy-rainy
      - "\U000F0599" # mdi-weather-sunny
      - "\U000F0F37" # mdi-weather-sunny-alert
      - "\U000F14E4" # mdi-weather-sunny-off
      - "\U000F059A" # mdi-weather-sunset
      - "\U000F059B" # mdi-weather-sunset-down
      - "\U000F059C" # mdi-weather-sunset-up
      - "\U000F0F38" # mdi-weather-tornado
      - "\U000F059D" # mdi-weather-windy
      - "\U000F059E" # mdi-weather-windy-variant

      - "\U000F00E9" # mdi-cake
      - "\U000F0A70" # mdi-silverware-fork-knife

      - "\U000F1A74" # mdi-solar-power-variant-outline
      - "\U000F1904" # mdi-home-lightning-bolt-outline

      - "\U000F05C3" # mdi-youtube

      - "\U000F05D6" # mdi-alert-circle-outline

  - file: "/config/esphome/Roboto.ttf"
    id: roboto6
    size: 6

  - file: "/config/esphome/Roboto.ttf"
    id: roboto8
    size: 8

  - file: "/config/esphome/Roboto.ttf"
    id: roboto12
    size: 12

  - file: "/config/esphome/FreeSansBold.ttf"
    id: freesb12
    size: 12

  - file: "/config/esphome/FreeSansBold.ttf"
    id: freesb8
    size: 8

display:
  - platform: hub75_matrix_display
    id: matrix
    width: 64
    height: 64
    R1_pin: 25
    G1_pin: 26
    B1_pin: 27
    R2_pin: 14
    G2_pin: 12
    B2_pin: 13
    A_pin: 23
    B_pin: 19
    C_pin: 5
    D_pin: 17
    E_pin: 18
    LAT_pin: 22
    OE_pin: 15
    CLK_pin: 16
    driver: MBI5124
    i2sspeed: HZ_20M
    latch_blanking: 4
    clock_phase: false
    update_interval: 8 ms

    pages:
      - id: page1
        lambda: |-
          std::map<std::string, std::string> weather_icon_map
            {
              {"cloudy", "\U000F0590"},
              {"cloudy-alert", "\U000F0F2F"},
              {"cloudy-arrow-right", "\U000F0E6E"},
              {"fog", "\U000F0591"},
              {"hail", "\U000F0592"},
              {"hazy", "\U000F0F30"},
              {"hurricane", "\U000F0898"},
              {"lightning", "\U000F0593"},
              {"lightning-rainy", "\U000F067E"},
              {"night", "\U000F0594"},
              {"night-partly-cloudy", "\U000F0F31"},
              {"partlycloudy", "\U000F0595"},
              {"partly-lightning", "\U000F0F32"},
              {"partly-rainy", "\U000F0F33"},
              {"partly-snowy", "\U000F0F34"},
              {"partly-snowy-rainy", "\U000F0F35"},
              {"pouring", "\U000F0596"},
              {"rainy", "\U000F0597"},
              {"snowy", "\U000F0598"},
              {"snowy-heavy", "\U000F0F36"},
              {"snowy-rainy", "\U000F067F"},
              {"sunny", "\U000F0599"},
              {"sunny-alert", "\U000F0F37"},
              {"sunny-off", "\U000F14E4"},
              {"sunset", "\U000F059A"},
              {"sunset-down", "\U000F059B"},
              {"sunset-up", "\U000F059C"},
              {"tornado", "\U000F0F38"},
              {"windy", "\U000F059D"},
              {"windy-variant", "\U000F059E"},
            };

          std::map<std::string, std::string> weather_traduction
            {
              {"cloudy", "Nuageux"},
              {"cloudy-alert", "Alerte"},
              {"cloudy-arrow-right", "--"},
              {"fog", "Brouillard"},
              {"hail", "GrĂŞle"},
              {"hazy", "Brume"},
              {"hurricane", "Ouragan"},
              {"lightning", "Orage"},
              {"lightning-rainy", "Orageux"},
              {"night", "Nuit"},
              {"night-partly-cloudy", "Nuageux"},
              {"partlycloudy", "Nuageux"},
              {"partly-lightning", "Orageux"},
              {"partly-rainy", "Pluvieux"},
              {"partly-snowy", "Neigeux"},
              {"partly-snowy-rainy", "Neigeux"},
              {"pouring", "Ă€ verse"},
              {"rainy", "Pluvieux"},
              {"snowy", "Neigeux"},
              {"snowy-heavy", "Neigeux"},
              {"snowy-rainy", "Neigeux"},
              {"sunny", "Ensoleillé"},
              {"sunny-alert", "Alerte"},
              {"sunny-off", "--"},
              {"sunset", "Crépuscule"},
              {"sunset-down", "Crépuscule"},
              {"sunset-up", "Aube"},
              {"tornado", "Tornade"},
              {"windy", "Venteux"},
              {"windy-variant", "Venteux"},
            };

          it.strftime(32, 0, id(freesb8), TextAlign::TOP_CENTER, "%d-%m-%Y", id(esptime).now());
          it.strftime(32, 10, id(freesb12), TextAlign::TOP_CENTER, "%H:%M:%S", id(esptime).now());
          it.printf(32, 26, id(freesb12), TextAlign::TOP_CENTER, "%.1f°C", id(meteo).state);
          it.printf(32, 44, id(icon), TextAlign::TOP_CENTER, "%s", weather_icon_map[id(temps).state].c_str());

      - id: page2
        lambda: |-
          it.print(32, 0, id(roboto8), TextAlign::TOP_CENTER, "IMPRESSION");
          it.printf(32, 8, id(freesb12), TextAlign::TOP_CENTER, "%.2f %%", id(x1c_pourcentage).state);
          it.print(32, 22, id(roboto8), TextAlign::TOP_CENTER, "Fini le :");
          it.printf(32, 32, id(roboto8), TextAlign::TOP_CENTER, id(finish_print_date).state.c_str());
          it.print(32, 42, id(roboto8), TextAlign::TOP_CENTER, "a :");
          it.printf(32, 50, id(roboto8), TextAlign::TOP_CENTER, id(finish_print_heure).state.c_str());
          

interval:
  - interval: 5s
    then:
      - display.page.show_next: matrix
      - component.update: matrix
   

switch:
  - platform: hub75_matrix_display
    matrix_id: matrix
    name: "Power"
    id: power
    restore_mode: ALWAYS_ON

number:
  - platform: hub75_matrix_display
    matrix_id: matrix
    name: "Brightness"

Bonjour,

10

1 Like

There is an example in the Docs for this exact scenario.

Good morning,
Thank you for the feedback but in fact I want to activate the interval only if the condition is true, not display a specific page.

Try with condition

 - interval: 5s
    then:
      if:
        condition:
          your-condition
        then:
         - display.page.show_next: matrix
         - component.update: matrix
1 Like

Thanks for your return but when i’m trying to put the code, i’ve got a error message :
image

I don’t know how i can declare a condition.

I’m not sure what component your condition is related to… (I don’t use Home Assistant)

normally condition is declared like this:


binary_sensor.is_on: my_binary_sensor

or

text_sensor.state:
          id: my_text_sensor
          state: 'MyState'

or try it with lambda:

lambda: |-
            return id(some_sensor).state < 10;
1 Like

Thats why there is documentation. None of us knew how at one point either but, we read and used the documentation to learn. Did you not know of this and all the examples it provides? Simple stuff like this, you can usually find a copy/paste example that will do exactly what you need.

The error you’re getting tells you exactly what the problem is. Theres no condition “entity_id”. That is the wrong syntax and if you use the documentation like I keep telling you, you’d quickly see how it needs to be formatted.

1 Like

The first link I sent you, it shows you in the examples. Here though, neither example does what you want but with a little thought you can use parts from both of them to create the automation you need.

This swaps pages like you want but not exactly. It does it on a 5s interval no matter what. So, you just need to add an if-else condition in it.

interval:
  - interval: 5s
    then:
      - display.page.show_next: my_display
      - component.update: my_display

If you scroll down a little bit on that documentation page there are more examples right under it that show you how to format an if-then condition.

Im not a fan of just writing people’s code for them to just copy/paste because I dont think it helps anyone.

Ill help you but, try to see if you can work it yourself now. Ive basically given you all the pieces, you just need to put it together.

1 Like

You’re right :wink:
Thanks for your time, i’m going to read the documentation.

People probably get annoyed with me always saying thay but, it will help you in many ways. Going and looking at supported devices/sensors can give you tons of future project ideas, help you better understand what is possible to do with esphome(a lot!). Most importantly then how to do it.

The other bonus is you likely wont be one of the people who go buy stuff online and then go around asking if/how to use it with esphome… you’ll know which sensors are supported or at the very least where to go check if one is or isnt.

FYI…
Is supported is much easier than not supported.