Project ESP Controle Power MacPro 2.1 (2006) Serveur

Hello,
My project is the control of the extinction as well as the start of my server.
My server is Mac Pro 2.1 (2006) run Debian 11 and Open Media Vault 6.
The services of my server are Home Assistant, Nextcloud, Ngnix, ESPHome and Minecraft server. The services are run by Docker and Docker is run by Portainer.

I not use my server all day, therefore it’s not necessary to power in permanently.

I will therefore use an ESP-01 (ESP8266) to control the starting and stopping of my server, with variable data from Home Assistant, but also Debian.

I use a clock alarm in Home Assistant, I will use the variable for execute start 1h15 before the alarm. Yes, I use another one automation. Who takes care of charging my phone 1 hour before. So I choose 1h15 to have a margin.

I use this code from active my charger 1h before my alarm, it must be modified to remove another 15 minute.
You have at good idea for the modification ?

template:
  - sensor:
      - name: "Wake Time 1 Pre"
        unique_id: sensor.wake_time_1_pre
        device_class: timestamp 
        state: >
          {% set alarm = today_at(states('input_datetime.wake_time_1')) %}
          {% set offset = 23 if now() >= alarm else -1 %}
          {{ alarm + timedelta(hours=offset) }}

In the ESP, I retrieve the time for the start of my server by this code.

binary_sensor:
  - platform: homeassistant
    name: "Input wake time Serveur ON From Home Assistant"
    entity_id: sensor.wake_time_serveur_on

The first trouble. I send 20h31 and i received 18h31. The ESP Home automatic convert to UTC time ? Because my time is UTC+2

Image
[15:02:26][W][homeassistant.binary_sensor:017]: Can't convert '2022-09-08T18:31:00+00:00' to binary state!
I add the sntp time

time: 
  - platform: sntp
    id: sntp_time

I would like to use C++ to do the start condition, because I think I know how to do it.

Of course, first I have to register my sensor.wake_time_serveur_on in a variable and write in a format equal to that of the sntp

if (id(serveur_power_on) = id(sntp_time)): {
        if (id(serveur_on) = low): {
          digitalWrite(GPIO0, HIGHT);
          delay(3000);
          digitalWrite(GPIO0, LOW);
        }
      }

I get a 3.3V electrical signal in the mac and if there is a signal, I don’t want to start the computer, because that means it is already started.

  - platform: gpio
    pin: GPIO2
    name: "Serveur ON"
    id: serveur_on
    filters:
      - delayed_off: 1000ms

I stop there for today.
If you have better ideas than mine, don’t hesitate.
I’m stuck a bit between using yaml and C++
I find it very easy to call yaml for information but for conditions, I don’t like, and I prefer C++. Except that I can’t integrate it well into ESPHome. To do well, I would like to create a second file with my conditions in C++ and integrate it into the yaml.

Thank you in advance for the help, I specify that I am not an English speaker, so there may be writing errors.

It looks like you only want to control the power button on your computer - correct?

You could then just build your self something with template buttons - little bit like this:

You can add a check if your binary sensor (serveur_on) is already on than don’t execute the action:

button:
  - platform: template
    name: Serveur Start
    on_press:
      - if:
          condition:
            - binary_sensor.is_off: serveur_on
          then:
             - button.press: gpio0
          else:
            - logger.log: "serveur is already running"

  - platform: output
    id: gpio0
    output: output1
    duration: 300ms

output:
  - platform: gpio
    pin: 0
    id: output1

The same you can do for the server stop routine.

For scheduling I would ether stick to ha or to esphome but wouldn’t try the middle way (something you try right now if I interpret it right). So you could just send a button action from ha ones the timer is ready :rocket:

For now, I’m focusing on getting started. Shutting down, I’ll do it once I can dock my server.

I can’t control the startup from Home Assistant since then the server won’t start.

That’s why I want to retrieve the time and do an action from the ESP. From the moment the ESP receives the time at which it must dock the server, the ESP must be autonomous.

But thank you for the condition. Now I have to find out how to request the mooring at a specific time or else this time can vary.

button:
  - platform: template
    name: Serveur Shut down
    on_press:
      - if:
          condition:
            - binary_sensor.is_on: serveur_on
          then:
             - button.press: gpio0
          else:
            - logger.log: "serveur is already powered off"

  - platform: output
    id: gpio0
    output: output1
    duration: 300ms

output:
  - platform: gpio
    pin: 0
    id: output1

Something like this if you can do a graceful shutdown with a button press

You are not “forced” to to control a esphome node with ha - you could also make use of the web server component for example.

As autonomous it can get. If you don’t have a RTC (or GPS) connected to your esphome node it needs at least a network connection and the possibility to reach a NTP or other time server.

time:
  - platform: sntp
    # ...
    on_time:
      # Every morning on weekdays
      - seconds: 0
        minutes: 30
        hours: 7
        days_of_week: MON-FRI
        then:
          - button.press: serveur_start

Hello,
Thanks for answer.
For now, I plan to use ntp.
And for activation with time, I think to use the proposal you make.

But where I get stuck, I get the time when, I want to start my Home Assistant server with this.

- platform: homeassistant
    name: "Input wake time Serveur ON From Home Assistant"
    entity_id: sensor.wake_time_serveur_on

Of course, I recover this before shutting down the server for the night. I then wonder if the ESP will memorize the time or if once my server is off it might forget it.

But then, I can’t transform the time I receive into anything usable for the time condition.

Should I separate minutes and hours in related variables?
To do like this.
But how to do it ?

time:
  - platform: sntp
    # ...
    on_time:
      # Every morning on weekdays
      - seconds: 0
        minutes: (varibles minutes)
        hours: (varibles hours)
        days_of_week: MON-FRI
        then:
          - button.press: serveur_start

Currently this is the code I have. I am therefore missing a part between receiving the time and using this data.
On ESPHome site Time page it talks about strftime, I feel like it can help me solve my problem.(Time — ESPHome)

binary_sensor:
# Server start time recovery
  - platform: homeassistant
    name: "Input wake time Serveur ON From Home Assistant"
    entity_id: sensor.wake_time_serveur_on

# Physical recovery from powering on the server. In GPIO2 3.3V front front panel MacPro
  - platform: gpio
    pin: GPIO2
    name: "Serveur ON"
    id: serveur_on
    filters:
      - delayed_off: 1000ms

# Configuration NTP
time: 
  - platform: sntp
    id: sntp_time
    timezone: Europe/Brussels

# Time condition to start the server
  - platform: sntp
    on_time:
      - seconds: 0
        minutes: 47
        hours: 18
        then: 
          - logger.log: "Serveur Start"
          - button.press: serveur_start

# Physical verification that the server is not already on.
button:
  - platform: template
    name: Serveur Start
    id: serveur_start
    on_press:
      - if:
          condition:
            - binary_sensor.is_off: serveur_on
          then:
             - button.press: gpio0
             - logger.log: "Serveur is starting"
          else:
            - logger.log: "serveur is already running"

# GPIO0 activation button
  - platform: output
    id: gpio0
    output: output1
    duration: 500ms

# GPIO0 to activate the server via a relay or other (define)
output:
  - platform: gpio
    pin: GPIO0
    id: output1

Thanks for the helps