[HELP] Set time format

Hello , newbie here
I am trying to build 7 segment clock , everything is working but i am not able to change time format from 24 hour to 12 hour

substitutions:
  device_name: led_clock

esphome:
  name: $device_name
  platform: ESP8266
  board: esp01_1m
  on_boot:
    - light.turn_on:
        id: led_strip
        brightness: 100%
        red: 0
        green: 0
        blue: 0
        effect: "${device_name} Time effect"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "${device_name} Fallback Hotspot"
    password: !secret wifi_fallback_api_password

logger:
  level: DEBUG
# Enable Home Assistant API
api:
  password: !secret hassio_api_password

ota:
  password: !secret ota_password

globals:
  - id: hours_red_value
    type: int
    restore_value: yes
    initial_value: '0'

  - id: hours_green_value
    type: int
    restore_value: yes
    initial_value: '0'

  - id: hours_blue_value
    type: int
    restore_value: yes
    initial_value: '0'

  - id: minutes_red_value
    type: int
    restore_value: yes
    initial_value: '0'

  - id: minutes_green_value
    type: int
    restore_value: yes
    initial_value: '0'

  - id: minutes_blue_value
    type: int
    restore_value: yes
    initial_value: '0'

  - id: dots_red_value
    type: int
    restore_value: yes
    initial_value: '0'

  - id: dots_green_value
    type: int
    restore_value: yes
    initial_value: '0'

  - id: dots_blue_value
    type: int
    restore_value: yes
    initial_value: '0'

output:
  #======== Hours ============
  - platform: template
    id: hours_red_output
    type: float
    write_action:
      lambda: |-
        id(hours_red_value) = 255.0 * state;

  - platform: template
    id: hours_green_output
    type: float
    write_action:
      - lambda: |-
          id(hours_green_value) = 255.0 * state;

  - platform: template
    id: hours_blue_output
    type: float
    write_action:
      lambda: |-
        id(hours_blue_value) = 255.0 * state;

  #========= Minutes ===========
  - platform: template
    id: minutes_red_output
    type: float
    write_action:
      lambda: |-
        id(minutes_red_value) = 255.0 * state;

  - platform: template
    id: minutes_green_output
    type: float
    write_action:
      lambda: |-
        id(minutes_green_value) = 255.0 * state;

  - platform: template
    id: minutes_blue_output
    type: float
    write_action:
      lambda: |-
        id(minutes_blue_value) = 255.0 * state;

  #========= dots ===========
  - platform: template
    id: dots_red_output
    type: float
    write_action:
      lambda: |-
        id(dots_red_value) = 255.0 * state;

  - platform: template
    id: dots_green_output
    type: float
    write_action:
      lambda: |-
        id(dots_green_value) = 255.0 * state;

  - platform: template
    id: dots_blue_output
    type: float
    write_action:
      lambda: |-
        id(dots_blue_value) = 255.0 * state;

time:
  - platform: sntp
    id: sntp_time
    timezone: "Europe/Moscow"
    servers:
      - 0.pool.ntp.org
      - 1.pool.ntp.org
      - 2.pool.ntp.org      

light:
  - platform: rgb
    name: "${device_name} hours lights"
    id: 'hours_lights'
    red: hours_red_output
    green: hours_green_output
    blue: hours_blue_output

  - platform: rgb
    name: "${device_name} minutes lights"
    id: 'minutes_lights'
    red: minutes_red_output
    green: minutes_green_output
    blue: minutes_blue_output

  - platform: rgb
    name: "${device_name} dots lights"
    id: 'dots_lights'
    red: dots_red_output
    green: dots_green_output
    blue: dots_blue_output

  #--------- LED strip ----------------
  - platform: fastled_clockless
    id: led_strip
    name: "Led strip"
    internal: True
    pin: GPIO3
    num_leds: 30
    chipset: WS2812B
    rgb_order: GRB

    effects:
    - addressable_lambda:
        name: "${device_name} Time effect"
        update_interval: 200ms
        lambda: |-
          const int ledsInDigitCount = 7;
          const int digitsCount = 4;

          int digitsLeds[10][ledsInDigitCount] = {
            {1,1,0,1,1,1,1},
            {0,0,0,1,0,0,1},
            {1,1,1,1,1,0,0},
            {1,0,1,1,1,0,1},
            {0,0,1,1,0,1,1},
            {1,0,1,0,1,1,1},
            {1,1,1,0,1,1,1},
            {0,0,0,1,1,0,1},
            {1,1,1,1,1,1,1},
            {1,0,1,1,1,1,1}
          };

          int ledOffsets[digitsCount] = {23 , 16, 7, 0};

          auto time = id(sntp_time).now();
          int colors[4][3] = {
            {id(hours_red_value), id(hours_green_value), id(hours_blue_value)},
            {id(hours_red_value), id(hours_green_value), id(hours_blue_value)},
            {id(minutes_red_value), id(minutes_green_value), id(minutes_blue_value)},
            {id(minutes_red_value), id(minutes_green_value), id(minutes_blue_value)}
          };

          int values[digitsCount] = {};
          values[0] = time.hour / 10;
          values[1] = time.hour % 10;
          values[2] = time.minute / 10;
          values[3] = time.minute % 10;

          it.all() = Color(0,0,0);

          if ((time.second % 2) > 0) {
            it[14] = Color(id(dots_red_value), id(dots_green_value), id(dots_blue_value));
            it[15] = Color(id(dots_red_value), id(dots_green_value), id(dots_blue_value));
          }

          for (int valueI = 0; valueI < digitsCount; valueI++) {
            int ledsOffset = ledOffsets[valueI];
            int timeValue = values[valueI];
            int *color = colors[valueI];
            int *leds = digitsLeds[timeValue];

            for (int ledI = 0; ledI < ledsInDigitCount; ledI++) {
              if(leds[ledI] > 0) {
                int ledIndex = ledI + ledsOffset;
                it[ledIndex] = Color(color[0], color[1], color[2]);
              }
            }
          }

code is taken from Alex18881

I have tried to change
auto time = id(sntp_time).now().strftime("%H:%M:%S - %d-%m-%Y");
but getting error

error -

/config/esphome/time.yaml: In lambda function:
/config/esphome/time.yaml:252:24: error: ‘class std::basic_string’ has no member named ‘hour’
values[0] = time.hour / 10;
^
/config/esphome/time.yaml:253:24: error: ‘class std::basic_string’ has no member named ‘hour’
values[1] = time.hour % 10;
^
/config/esphome/time.yaml:254:24: error: ‘class std::basic_string’ has no member named ‘minute’
values[2] = time.minute / 10;
^
/config/esphome/time.yaml:255:24: error: ‘class std::basic_string’ has no member named ‘minute’
values[3] = time.minute % 10;
^
/config/esphome/time.yaml:259:17: error: ‘class std::basic_string’ has no member named ‘second’
if ((time.second % 2) > 0) {
^

This line

auto time = id(sntp_time).now().strftime("%H:%M:%S - %d-%m-%Y");

returns an object of type ‘string’ which is not a time object, hence the errors complaining about it not having no member function like .hour because it’s just a string.

But in your code you appear to have correctly changed it to

auto time = id(sntp_time).now();

which should be returning a ‘time’ object for which the calls to .hour and so forth should work.
Is the code shown in your post the same code that produced the list of error messages?

The code you posted compiles fine for me.
Your 12/24 hour problem might be solved by changing the two lines:

values[0] = time.hour / 10;
values[1] = time.hour % 10;

to

values[0] = (time.hour % 12) / 10;
values[1] = (time.hour % 12) % 10;

Perfect !
Thank you so much

If its not much then can you please help to turn off “0” digit in hour segment ?
eg - “01:00” then it should turn “0” off and display “1:00” only
I’ve tried adding {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}and changed

values[0] = (time.hour % 12) / 11;
values[1] = (time.hour % 12) % 11;

but it didn’t worked out

You can’t make that happen by altering the math. The divide-by-ten relates to the fact that these are decimal numbers, so you’ll probably want to change that back to /10’s.
The place to blank a digit is in the logic where the digit is selected.
So you were correct to add an 11th element to digitsLeds, an 11th that has all 0’s so no segments light.
Then, in the part of the code where it’s assigning offsets into digitsLeds to the display, you test for “is this the first digit, and is it 0, if so, assign 11” instead of the mathematically-computed choice.
It might work, then, to replace this line:

values[0] = (time.hour % 12) / 10;

with

((time.hour % 12) / 10) == 0 ? 11 : (time.hour % 12) / 10

or something more readable than that:

if ( (time.hour % 12) / 10 == 0) {
  values[0] = 11; // the eleventh item in the array digitsLeds that has all-0's
} else {
  values[0] = (time.hour % 12) / 10; // the math that you have now.
}

I haven’t had my coffee yet, so not thinking well enough.

1 Like

oh, thank you for your time
i am getting error while compiling

1-

/config/esphome/time.yaml: In lambda function:
/config/esphome/time.yaml:284:40: error: expected ‘;’ before ‘)’ token
values[0] = (time.hour % 12) / 10) == 0 ? 11 : (time.hour % 12) / 10)

without value

/config/esphome/time.yaml: In lambda function:
/config/esphome/time.yaml:284:28: error: expected ‘;’ before ‘)’ token
(time.hour % 12) / 10) == 0 ? 11 : (time.hour % 12) / 10)
^
/config/esphome/time.yaml:284:24: warning: statement has no effect [-Wunused-value]
(time.hour % 12) / 10) == 0 ? 11 : (time.hour % 12) / 10)

2-

/config/esphome/time.yaml: In lambda function:
/config/esphome/time.yaml:284:27: error: expected primary-expression before ‘/’ token
if (time.hour % 12) / 10 == 0) {
^
/config/esphome/time.yaml:284:36: error: expected ‘;’ before ‘)’ token
if (time.hour % 12) / 10 == 0) {
^
/config/esphome/time.yaml:287:35: error: expected ‘;’ before ‘)’ token
values[0] = time.hour % 12) / 10; // the math that you have now.

I had made some dumb syntax errors in the example code I dashed out before coffee.
I’ve edited them above. I’m sure you’ll be able to puzzle it out from here - these just give you the general idea of how to solve the problem, getting the syntax correct is what remains.

yeah noticed that later in code editor
both worked flawlessly , thank you once again :smiley:

@evil_shadow I am also building the 7 segment clock,
Could you maybe share your current working code with the fixes / adjustments? :slight_smile:

thnx in advance.