Automated restart esphome devices

Hi, everyone!
This week I noticed, some my esphome devices with sensors has problems with cooenctivity via api with Home Assistant.
Some of them has different time of last update. Even if two sensors are on same esp, last update may differs.
For example, esp in kitchen updated temperature 30 seconds ago and humidity updated 5 (!!!) minutes ago.
Sometimes thay updated simultaneously.

I cant figure out the reason.

I’ve got an idea. How about restart ESP, if last update of sensors was more than 5 minutes ago.
I wrote automation:

alias: "1"
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.kitchen_temperature
    attribute: last-updated
    from: "5"
    to: "6"
condition: []
action:
  - service: button.press
    data: {}
    target:
      entity_id: button.kitchen_restart
  - service: button.press
    data: {}
    target:
      area_id: kitchen
      device_id: 834c0d07f1a06330b4252648602589
      entity_id: button.kitchen_restart
mode: single

I tried to use seconds instead minutes in from-to fileds, it didn’t work.

Somewhere I made mistake. Its not working. Automation never starts, even when timer last-updated far exceds ((
Need some hepl, please.

Either can we without seeing your device configuration and serial logs.

A better idea would be to fix your device.

BT W, last updated is not an attribute, it is a property. You can not access it like this:

attribute: last-updated

Here esp configuration:

esphome:
  name: kitchen
  platform: ESP8266
  board: esp01_1m

wifi:
  networks:
  - ssid: Mikrotik744
    password: password
    hidden: true
  - ssid: D-Link
    password: password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Kitchen Fallback Hotspot"
    password: "0Z8hL"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
#   password: 'welcome'
   port: 6053
   encryption:
      key: "U+vrs3tE356/WMklXOgrCBIrYUhpbcf"
ota:

switch:
  - platform: gpio
    pin: GPIO0
    name: "220 V Relay"
    inverted: true

button:
  - platform: restart
    name: "Kitchen Restart"

sensor:
  - platform: dht
    model: AUTO_DETECT
    pin: GPIO2
    temperature:
      name: "Kitchen Temperature"
    humidity:
      name: "Kitchen Humidity"
    update_interval: 60s

While I collecting logs via ESPHOME server, please advise, about automation reboot button.
Thanks.

Logs:

[11:17:40][D][dht:048]: Got Temperature=28.5°C Humidity=52.0%
[11:17:40][D][sensor:125]: 'Kitchen Temperature': Sending state 28.50000 °C with 1 decimals of accuracy
[11:17:40][D][sensor:125]: 'Kitchen Humidity': Sending state 52.00000 % with 0 decimals of accuracy
[11:18:40][D][dht:048]: Got Temperature=28.5°C Humidity=53.0%
[11:18:40][D][sensor:125]: 'Kitchen Temperature': Sending state 28.50000 °C with 1 decimals of accuracy
[11:18:40][D][sensor:125]: 'Kitchen Humidity': Sending state 53.00000 % with 0 decimals of accuracy
[11:19:40][D][dht:048]: Got Temperature=28.5°C Humidity=53.0%
[11:19:40][D][sensor:125]: 'Kitchen Temperature': Sending state 28.50000 °C with 1 decimals of accuracy
[11:19:40][D][sensor:125]: 'Kitchen Humidity': Sending state 53.00000 % with 0 decimals of accuracy
[11:20:40][D][dht:048]: Got Temperature=28.5°C Humidity=53.0%
[11:20:40][D][sensor:125]: 'Kitchen Temperature': Sending state 28.50000 °C with 1 decimals of accuracy
[11:20:40][D][sensor:125]: 'Kitchen Humidity': Sending state 53.00000 % with 0 decimals of accuracy

ESP sending data every minute.
But in web HA last update temperature and humidity was 5 minutes ago.

Last updated won’t change if the new state value is the same as the current value. See the description of last updated here: State Objects - Home Assistant

Your device is working fine. It’s sending updated that HA receives every minute.

I would recommend you change the DHT config to use the actual device model rather than auto detect though. That has caused some people issues in the past.