Put state of current active relay in the LCD on 1 spot

Dear people,

Can anyone help me to write a peace for my script , on how do I put the state of the current active relays (1 to 4) in the LCD on 1 spot, or off.

My idea was to put the current active value in a variable and set that state in the LCD:
With something like: it.printf(0, 0, id(Kraan_selector).state ? “:%s”);

I have a 4x20 i2c LCD and 4 relay’s from where only 1 can be active at the same time (garden sprinker valves).

For what I would like to do, is to put the state of the current relay on 1 spot on the display.
Right now every relay has 1 line, so it takes 4 lines. Good, but can be better!

Thanc you in Advance!!!

Here is my current working script:

esphome:

name: "kas"

  platform: ESP32

  board: esp-wrover-kit

ethernet:

  type: LAN8720

  mdc_pin: GPIO23

  mdio_pin: GPIO18

  clk_mode: GPIO0_IN

  phy_addr: 1

  power_pin: GPIO16

# Enable logging

logger:

# Enable Home Assistant API

api:

web_server:

  port: 80

ota:

  password: "boeba tie boeb "

#I2C config

i2c:

  sda: 4

  scl: 2

  scan: True

###################Globals#########################

globals:

- id: zone_start_time

  type: int

  restore_value: no

  initial_value: '0'

- id: zone_run_time

  type: int

  restore_value: no

  initial_value: '0'

 

##################Lambda timer script##################

interval:

  - interval: 1s

    then:

      - if:

          condition:

            lambda: |-

              return id(zone_start_time) != 0;

          then:

          - lambda: |-

              id(zone_run_time) = id(homeassistant_time).now().timestamp - id(zone_start_time);

          - logger.log:

              format: "Zone run time %.1d"

              args: [ 'id(zone_run_time)' ]

          else:

          - lambda: |-

              id(zone_run_time) = 0;

###################Countdown timer################

      - if:

          condition:

            for:

              time: 20min

              condition:

              -  switch.is_on: KAS_RELAY_1

          then:

            - switch.turn_off: KAS_RELAY_1

      - if:

          condition:

            for:

              time: 20min

              condition:

              -  switch.is_on: KAS_RELAY_2

          then:

            - switch.turn_off: KAS_RELAY_2

      - if:

          condition:

            for:

              time: 20min

              condition:

              -  switch.is_on: KAS_RELAY_3

          then:

            - switch.turn_off: KAS_RELAY_3

      - if:

          condition:

            for:

              time: 20min

              condition:

                - switch.is_on: KAS_RELAY_4

          then:

            - switch.turn_off: KAS_RELAY_4

     

######################PCF HUBS####################

pcf8574:

  - id: 'pcf8575_1'

    address: 0x20

    pcf8575: False

   

  - id: 'pcf8575_2'

    address: 0x21

    pcf8575: False

   

  - id: 'pcf8575_3'

    address: 0x22

    pcf8575: False    

###################pcf8575 Relay#################

switch:

###

  - platform: restart

    name: "KAS_RESET"

###

  - platform: gpio

    name: "KAS_RELAY_1"

    id: "KAS_RELAY_1"

    interlock: [KAS_RELAY_2 ,KAS_RELAY_3, KAS_RELAY_4]

    pin:

      pcf8574: pcf8575_1

      number: 0

      mode: OUTPUT

      inverted: True

    on_turn_on:

        - switch.turn_on: KAS_LED_1_BLINK

        - lambda: |-

              id(zone_start_time) = id(homeassistant_time).now().timestamp;

    on_turn_off:

        - switch.turn_off: KAS_LED_1_BLINK

        - lambda: |-

              id(zone_start_time) = id(homeassistant_time).now().timestamp;

  - platform: gpio

    name: "KAS_RELAY_2"

    id: "KAS_RELAY_2"

    interlock: [KAS_RELAY_1 ,KAS_RELAY_3, KAS_RELAY_4]

    pin:

      pcf8574: pcf8575_1

      number: 1

      mode: OUTPUT

      inverted: True

    on_turn_on:

        - switch.turn_on: KAS_LED_2_BLINK

        - lambda: |-

              id(zone_start_time) = id(homeassistant_time).now().timestamp;        

    on_turn_off:

        - switch.turn_off: KAS_LED_2_BLINK

        - lambda: |-

              id(zone_start_time) = id(homeassistant_time).now().timestamp;

  - platform: gpio

    name: "KAS_RELAY_3"

    id: "KAS_RELAY_3"

    interlock: [KAS_RELAY_1 ,KAS_RELAY_2, KAS_RELAY_4]

    pin:

      pcf8574: pcf8575_1

      number: 2

      mode: OUTPUT

      inverted: True

    on_turn_on:

        - switch.turn_on: KAS_LED_3_BLINK

        - lambda: |-

              id(zone_start_time) = id(homeassistant_time).now().timestamp;

    on_turn_off:

        - switch.turn_off: KAS_LED_3_BLINK

        - lambda: |-

              id(zone_start_time) = id(homeassistant_time).now().timestamp;

  - platform: gpio

    name: "KAS_RELAY_4"

    id: "KAS_RELAY_4"

    interlock: [KAS_RELAY_1 ,KAS_RELAY_2, KAS_RELAY_3]

    pin:

      pcf8574: pcf8575_1

      number: 3

      mode: OUTPUT

      inverted: True

    on_turn_on:

        - switch.turn_on: KAS_LED_4_BLINK

        - lambda: |-

              id(zone_start_time) = id(homeassistant_time).now().timestamp;

    on_turn_off:

        - switch.turn_off: KAS_LED_4_BLINK

        - lambda: |-

              id(zone_start_time) = id(homeassistant_time).now().timestamp;

     

  - platform: gpio

    name: "KAS_RELAY_5"

    id: "KAS_RELAY_5"

    pin:

      pcf8574: pcf8575_1

      number: 4

      mode: OUTPUT

      inverted: True

    on_turn_on:

        - switch.turn_on: KAS_LED_5

    on_turn_off:

        - switch.turn_off: KAS_LED_5

     

  - platform: gpio

    name: "KAS_RELAY_6"

    pin:

      pcf8574: pcf8575_1

      number: 5

      mode: OUTPUT

      inverted: True

  - platform: gpio

    name: "KAS_RELAY_7"

    pin:

      pcf8574: pcf8575_1

      number: 6

      mode: OUTPUT

      inverted: True

     

  - platform: gpio

    name: "KAS_RELAY_8"

    pin:

      pcf8574: pcf8575_1

      number: 7

      mode: OUTPUT

      inverted: True

###################pcf8575 LED#################

  - platform: gpio

    name: "KAS_LED_1"

    id: KAS_LED_1

    pin:

      pcf8574: pcf8575_2

      number: 2

      mode: OUTPUT

      inverted: False

  - platform: template

    name: "KAS_LED_1_BLINK"

    optimistic: yes

    id: KAS_LED_1_BLINK

    turn_on_action:

    - while:

        condition:

          lambda: 'return true;'

        then:

        - switch.turn_on: KAS_LED_1

        - delay: 500ms

        - switch.turn_off: KAS_LED_1

        - delay: 500ms

    turn_off_action:

      - switch.turn_off: KAS_LED_1

#####

  - platform: gpio

    name: "KAS_LED_2"

    id: KAS_LED_2

    pin:

      pcf8574: pcf8575_2

      number: 1

      mode: OUTPUT

      inverted: False

  - platform: template

    name: "KAS_LED_2_BLINK"

    optimistic: yes

    id: KAS_LED_2_BLINK

    turn_on_action:

    - while:

        condition:

          lambda: 'return true;'

        then:

        - switch.turn_on: KAS_LED_2

        - delay: 500ms

        - switch.turn_off: KAS_LED_2

        - delay: 500ms

    turn_off_action:

      - switch.turn_off: KAS_LED_2

#####

  - platform: gpio

    name: "KAS_LED_3"

    id: KAS_LED_3

    pin:

      pcf8574: pcf8575_2

      number: 0

      mode: OUTPUT

      inverted: False

  - platform: template

    name: "KAS_LED_3_BLINK"

    optimistic: yes

    id: KAS_LED_3_BLINK

    turn_on_action:

    - while:

        condition:

          lambda: 'return true;'

        then:

        - switch.turn_on: KAS_LED_3

        - delay: 500ms

        - switch.turn_off: KAS_LED_3

        - delay: 500ms

    turn_off_action:

      - switch.turn_off: KAS_LED_3

#####

  - platform: gpio

    name: "KAS_LED_4"

    id: KAS_LED_4

    pin:

      pcf8574: pcf8575_2

      number: 3

      mode: OUTPUT

      inverted: False

  - platform: template

    name: "KAS_LED_4_BLINK"

    optimistic: yes

    id: KAS_LED_4_BLINK

    turn_on_action:

    - while:

        condition:

          lambda: 'return true;'

        then:

        - switch.turn_on: KAS_LED_4

        - delay: 500ms

        - switch.turn_off: KAS_LED_4

        - delay: 500ms

    turn_off_action:

      - switch.turn_off: KAS_LED_4

#####

  - platform: gpio

    name: "KAS_LED_5"

    id: KAS_LED_5

    pin:

      pcf8574: pcf8575_2

      number: 4

      mode: OUTPUT

      inverted: true

###################pcf8575 KNOP#################

binary_sensor:

  - platform: gpio

    name: "KAS_KNOP_1"

    pin:

      pcf8574: pcf8575_3

      number: 3

      mode: INPUT

      inverted: True

    on_press:

      then:

        - switch.toggle: KAS_RELAY_1

  - platform: gpio

    name: "KAS_KNOP_2"

    pin:

      pcf8574: pcf8575_3

      number: 0

      mode: INPUT

      inverted: True

    on_press:

      then:

        - switch.toggle: KAS_RELAY_2

     

  - platform: gpio

    name: "KAS_KNOP_3"

    pin:

      pcf8574: pcf8575_3

      number: 1

      mode: INPUT

      inverted: True

    on_press:

      then:

        - switch.toggle: KAS_RELAY_3

  - platform: gpio

    name: "KAS_KNOP_4"

    pin:

      pcf8574: pcf8575_3

      number: 2

      mode: INPUT

      inverted: True

    on_press:

      then:

        - switch.toggle: KAS_RELAY_4

     

  - platform: gpio

    name: "KAS_KNOP_5"

    pin:

      pcf8574: pcf8575_3

      number: 4

      mode: INPUT

      inverted: True

    on_press:

      then:

        - switch.toggle: KAS_RELAY_5

###################Omgevings sensors##############

sensor:

##Baro/Druk/Vocht##

  - platform: bme280

    address: 0x76

    update_interval: 60s

    temperature:

      name: "KAS_BME280-Temp"

      oversampling: 16x

      id: Temp

    pressure:

      name: "KAS_BME280-Pres"

      id: Pres

    humidity:

      name: "KAS_BME280-Hum"

      id: Hum

##Licht sensor##

  - platform: tsl2561

    name: "KAS_TSL2561_Amb-Light"

    id: Light

    address: 0x39

    update_interval: 60s

   

####################LCD##########################

display:

  - platform: lcd_pcf8574

    dimensions: 20x4

    address: 0x27

    lambda: |-

      it.printf(0, 0, "Kas  "":%s", id( KAS_RELAY_1 ).state ? "ON" : "OFF");

      it.printf(0, 1, "Krn.1"":%s", id( KAS_RELAY_2 ).state ? "ON" : "OFF");

      it.printf(0, 2, "Krn.2"":%s", id( KAS_RELAY_3 ).state ? "ON" : "OFF");

      it.printf(0, 3, "Krn.3"":%s", id( KAS_RELAY_4 ).state ? "ON" : "OFF");

      it.printf(10, 0, "Tmp"":%.1f""C", id(Temp).state);

      it.printf(10, 1, "Hum"":%.1f""%", id(Hum).state);

time:

  - platform: homeassistant

    id: homeassistant_time